Skip to content

Commit

Permalink
Add rector (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz authored Oct 3, 2022
1 parent bff68ec commit 9870fe7
Show file tree
Hide file tree
Showing 32 changed files with 158 additions and 140 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
on:
push:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.env.example'
- '.gitattributes'
- 'infection.json.dist'
- 'phpunit.xml.dist'

name: rector

jobs:
rector:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
tools: composer:v2
coverage: none

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v2
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Update composer
run: composer self-update

- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- run: vendor/bin/rector process --ansi

- name: Check for Rector modified files
id: rector-git-check
run: echo ::set-output name=modified::$(if git diff --exit-code --no-patch; then echo "false"; else echo "true"; fi)

- name: Git config
if: steps.rector-git-check.outputs.modified == 'true'
run: |
git config --global user.name 'rector-bot'
git config --global user.email 'rector@yiiframework.com'
- name: Commit Rector changes
if: steps.rector-git-check.outputs.modified == 'true'
run: git commit -am "[rector] Apply fixes"

- name: Push changes
if: steps.rector-git-check.outputs.modified == 'true'
run: git push

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"yiisoft/files": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions config/replicate/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,20 @@
'psalm.xml',
],
],
'rector' => [
'source' => 'package-template',
'packages' => [
'include' => ['*'],
'exclude' => [
'docs',
'yii-docker',
'yii-debug-frontend',
'yii-gii-frontend',
],
],
'files' => [
'.github/workflows/rector.yml',
'rector.php',
],
],
];
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);
};
5 changes: 1 addition & 4 deletions src/App/Command/Composer/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ final class UpdateCommand extends PackageCommand

private array $additionalComposerUpdateOptions = [];

private PackageService $packageService;

public function __construct(PackageService $packageService, string $name = null)
public function __construct(private PackageService $packageService, string $name = null)
{
$this->packageService = $packageService;
parent::__construct($name);
}

Expand Down
5 changes: 1 addition & 4 deletions src/App/Command/Git/CloneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ final class CloneCommand extends PackageCommand
protected static $defaultName = 'git/clone';
protected static $defaultDescription = 'Package repositories cloning';

private PackageService $packageService;

public function __construct(PackageService $packageService, string $name = null)
public function __construct(private PackageService $packageService, string $name = null)
{
$this->packageService = $packageService;
parent::__construct($name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/App/Command/Github/ForksRepositoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

final class ForksRepositoriesCommand extends Command
{
private ?OutputManager $io;
private ?OutputManager $io = null;

protected function initialize(InputInterface $input, OutputInterface $output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/App/Command/Github/ProtectBranchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class ProtectBranchCommand extends PackageCommand
{
private ?string $branch;
private ?string $branch = null;

protected function configure()
{
Expand Down
5 changes: 1 addition & 4 deletions src/App/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ final class InstallCommand extends PackageCommand

private array $additionalComposerInstallOptions = [];

private PackageService $packageService;

public function __construct(PackageService $packageService, string $name = null)
public function __construct(private PackageService $packageService, string $name = null)
{
$this->packageService = $packageService;
parent::__construct($name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/App/Command/Release/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

final class MakeCommand extends PackageCommand
{
private ?string $tag;
private ?string $tag = null;

private const MAIN_BRANCHES = ['master', 'main'];

Expand Down
4 changes: 2 additions & 2 deletions src/App/Command/Release/WhatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

final class WhatCommand extends Command
{
private ?OutputManager $io;
private ?PackageList $packageList;
private ?OutputManager $io = null;
private ?PackageList $packageList = null;

protected function configure()
{
Expand Down
4 changes: 2 additions & 2 deletions src/App/Command/Stats/ContributorsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

final class ContributorsCommand extends Command
{
private ?OutputManager $io;
private ?PackageList $packageList;
private ?OutputManager $io = null;
private ?PackageList $packageList = null;

protected function configure()
{
Expand Down
4 changes: 2 additions & 2 deletions src/App/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class TestCommand extends PackageCommand
protected static $defaultName = 'test';
protected static $defaultDescription = 'Test packages';

private ?string $filter;
private ?string $filter = null;

protected function configure(): void
{
Expand Down Expand Up @@ -82,6 +82,6 @@ protected function processPackage(Package $package): void

private function isComposerTestNotImplemented(Process $process): bool
{
return strpos($process->getErrorOutput(), 'Command "test" is not defined') !== false;
return str_contains($process->getErrorOutput(), 'Command "test" is not defined');
}
}
5 changes: 1 addition & 4 deletions src/App/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ final class UpdateCommand extends PackageCommand

private array $additionalComposerUpdateOptions = [];

private PackageService $packageService;

public function __construct(PackageService $packageService, string $name = null)
public function __construct(private PackageService $packageService, string $name = null)
{
$this->packageService = $packageService;
parent::__construct($name);
}

Expand Down
5 changes: 1 addition & 4 deletions src/App/Component/Console/OutputManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
*/
class OutputManager
{
private YiiDevToolStyle $io;
private ?string $preparedPackageHeader = null;
private bool $nextMessageIsImportant = false;
private bool $outputDone = false;

public function __construct(YiiDevToolStyle $io)
public function __construct(private YiiDevToolStyle $io)
{
$this->io = $io;
}

public function hasColorSupport(): bool
Expand Down Expand Up @@ -48,7 +46,6 @@ public function setVerbosity(int $level): void
* The header will be automatically displayed later before the first message that will require output.
* If a console command operates in a verbose mode, a detailed header will be prepared, otherwise a short one.
*
* @param Package $package
* @param string $header A detailed version of header.
* Substring '{package}' will be replaced by the name of the package.
*
Expand Down
10 changes: 5 additions & 5 deletions src/App/Component/Console/PackageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
*/
class PackageCommand extends Command
{
private ?OutputManager $io;
private ?PackageList $packageList;
private ?PackageErrorList $errorList;
private ?bool $targetPackagesSpecifiedExplicitly;
private ?OutputManager $io = null;
private ?PackageList $packageList = null;
private ?PackageErrorList $errorList = null;
private ?bool $targetPackagesSpecifiedExplicitly = null;

/** @var Package[]|null */
private ?array $targetPackages;
Expand Down Expand Up @@ -314,7 +314,7 @@ private function showPackageErrors(): void
{
$io = $this->getIO();

if (count($this->errorList) > 0) {
if (($this->errorList === null ? 0 : count($this->errorList)) > 0) {
$io
->important()
->info([
Expand Down
6 changes: 2 additions & 4 deletions src/App/Component/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class Package
{
private static ?GitWrapper $gitWrapper = null;
private string $id;
private ?string $configuredRepositoryUrl;
private ?string $configuredRepositoryUrl = null;
private string $path;
private ?GitWorkingCopy $gitWorkingCopy = null;
private string $owner;

private static function getGitWrapper(): GitWrapper
{
Expand All @@ -30,14 +29,13 @@ private static function getGitWrapper(): GitWrapper
return static::$gitWrapper;
}

public function __construct(string $id, $config, string $owner, string $packagesRootDir)
public function __construct(string $id, $config, private string $owner, string $packagesRootDir)
{
if (!preg_match('|^[a-z0-9_.-]+$|i', $id)) {
throw new InvalidArgumentException('Package ID can contain only symbols [a-z0-9_.-].');
}

$this->id = $id;
$this->owner = $owner;

if (!is_bool($config) && !is_string($config)) {
throw new InvalidArgumentException('Package config must contain a boolean or a string.');
Expand Down
9 changes: 1 addition & 8 deletions src/App/Component/Package/PackageError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

class PackageError
{
private Package $package;
private string $message;
private string $during;

public function __construct(Package $package, string $message, string $during)
public function __construct(private Package $package, private string $message, private string $during)
{
$this->package = $package;
$this->message = $message;
$this->during = $during;
}

public function getPackage(): Package
Expand Down
11 changes: 1 addition & 10 deletions src/App/Component/Package/ReplicationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@

class ReplicationSet
{
private string $sourcePackage;
private array $files;
private array $includedPackages;
private array $excludedPackages;

public function __construct(string $sourcePackage, array $files, array $includedPackages, array $excludedPackages)
public function __construct(private string $sourcePackage, private array $files, private array $includedPackages, private array $excludedPackages)
{
$this->sourcePackage = $sourcePackage;
$this->files = $files;
$this->includedPackages = $includedPackages;
$this->excludedPackages = $excludedPackages;
}

public function getSourcePackage(): string
Expand Down
16 changes: 5 additions & 11 deletions src/Infrastructure/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

final class Changelog
{
private string $path;

public function __construct(string $path)
public function __construct(private string $path)
{
$this->path = $path;
}

public function resort(Version $version): void
Expand Down Expand Up @@ -67,8 +64,6 @@ public function close(Version $version): void
}

/**
* @param Version $version
*
* @return string[]
*/
public function getReleaseNotes(Version $version): array
Expand Down Expand Up @@ -108,13 +103,12 @@ private function splitChangelog(?string $version = null): array
) {
$state = 'changelog';
}
if ($state === 'changelog' && isset($lines[$lineNumber + 1]) && strncmp($lines[$lineNumber + 1], '## ', 3) === 0) {
if ($state === 'changelog' && isset($lines[$lineNumber + 1]) && str_starts_with($lines[$lineNumber + 1], '## ')) {
$state = 'end';
}
// add continued lines to the last item to keep them together
if (!empty(${$state}) && trim($line) !== '' && strncmp($line, '- ', 2) !== 0) {
end(${$state});
${$state}[key(${$state})] .= "\n" . $line;
if (!empty(${$state}) && trim($line) !== '' && !str_starts_with($line, '- ')) {
${$state}[array_key_last(${$state})] .= "\n" . $line;
} else {
${$state}[] = $line;
}
Expand All @@ -141,7 +135,7 @@ private function splitChangelog(?string $version = null): array
* @throws InvalidArgumentException if the $direction or $sortFlag parameters do not have
* correct number of elements as that of $key.
*/
private function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR): void
private function multisort(&$array, array|\Closure|string $key, array|int $direction = SORT_ASC, array|int $sortFlag = SORT_REGULAR): void
{
$keys = is_array($key) ? $key : [$key];
if (empty($keys) || empty($array)) {
Expand Down
Loading

0 comments on commit 9870fe7

Please sign in to comment.