From 7c2cb01cb08da027f6910b1725eb0a106cfbea3f Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sat, 10 Jun 2023 14:34:39 +0700 Subject: [PATCH 1/5] Upgrade minimal PHP version from 7.4 to 8.1. --- .github/workflows/ci.yaml | 6 +++--- Makefile | 4 ++-- composer.json | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9e860a5..b12023b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.4', '8.0', '8.1'] + php-version: ['8.1', '8.2'] steps: - uses: actions/checkout@v2 @@ -39,12 +39,12 @@ jobs: - name: Run tests run: | - if [[ ${{ matrix.php-version }} == '7.4' ]]; then + if [[ ${{ matrix.php-version }} == '8.1' ]]; then export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml else vendor/bin/phpunit fi - name: Submit code coverage - if: matrix.php-version == '7.4' + if: matrix.php-version == '8.1' uses: codecov/codecov-action@v1 diff --git a/Makefile b/Makefile index d1b2801..64ca9cb 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ -PHP?=php7.4 +PHP?=php8.1 COMPOSER=/usr/local/bin/composer php-info: - @echo "Default PHP version: $(PHP) (Run with custom PHP version: make install PHP=php8.0).\n"; + @echo "Default PHP version: $(PHP) (Run with custom PHP version: make install PHP=php8.2).\n"; list: php-info ## List @sed -rn 's/^([a-zA-Z_-]+):.*?## (.*)$$/"\1" "\2"/p' < $(MAKEFILE_LIST) | xargs printf "%-20s%s\n" diff --git a/composer.json b/composer.json index afa06f5..95edaa0 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ } }, "require": { - "php": ">=7.4.0", + "php": ">=8.1.0", "ext-json": "*", "psr/container": "^1.1", "symfony/mailer": "^5.4" @@ -55,7 +55,7 @@ }, "extra": { "platform": { - "php": "7.4.*" + "php": "8.1.*" } }, "config": { From 51a109d8ab566f73a86d323bc65b9f5e8d2224a7 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sat, 10 Feb 2024 12:29:04 +0700 Subject: [PATCH 2/5] Added Rector. --- .gitattributes | 1 + composer.json | 3 ++- rector.php | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/.gitattributes b/.gitattributes index fee66c5..f055917 100644 --- a/.gitattributes +++ b/.gitattributes @@ -15,3 +15,4 @@ /phpunit.xml.dist export-ignore /psalm.xml.dist export-ignore /codecov.yml export-ignore +/rector.php export-ignore diff --git a/composer.json b/composer.json index 95edaa0..ad93a84 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,8 @@ "phpmd/phpmd": "^1.5", "sebastian/phpcpd": "^6.0", "phploc/phploc": "^7.0", - "php-parallel-lint/php-parallel-lint": "^1.3" + "php-parallel-lint/php-parallel-lint": "^1.3", + "rector/rector": "^1.0" }, "extra": { "platform": { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..af80c4a --- /dev/null +++ b/rector.php @@ -0,0 +1,25 @@ +paths([ + __DIR__ . '/src' + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_81, + //SetList::PHP_81, + //SetList::CODE_QUALITY, + //SetList::TYPE_DECLARATION_STRICT, + //SetList::DEAD_CODE, + ]); +}; From 06e6ca4a8ff0a254eb41255fc3eafb53bd6b184e Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sat, 10 Feb 2024 12:35:55 +0700 Subject: [PATCH 3/5] Upgrade code to PHP 8.1. --- src/Build/BuildMetaWriterInterface.php | 5 +-- src/CommandExecutorInterface.php | 5 +-- src/ParameterBag.php | 5 +-- src/ParameterBagInterface.php | 4 +- src/Plugin/PathResolver.php | 28 ++++---------- src/Plugin/Plugin.php | 53 ++++++-------------------- src/View/ViewInterface.php | 5 +-- tests/src/Plugin/PluginTest.php | 4 +- 8 files changed, 24 insertions(+), 85 deletions(-) diff --git a/src/Build/BuildMetaWriterInterface.php b/src/Build/BuildMetaWriterInterface.php index f02637e..4fc6899 100644 --- a/src/Build/BuildMetaWriterInterface.php +++ b/src/Build/BuildMetaWriterInterface.php @@ -12,13 +12,10 @@ */ interface BuildMetaWriterInterface { - /** - * @param mixed $value - */ public function write( BuildInterface $build, ?string $plugin, string $key, - $value + mixed $value ): void; } diff --git a/src/CommandExecutorInterface.php b/src/CommandExecutorInterface.php index 9dcf5b6..d7eee88 100644 --- a/src/CommandExecutorInterface.php +++ b/src/CommandExecutorInterface.php @@ -14,10 +14,7 @@ */ interface CommandExecutorInterface { - /** - * @param mixed ...$params - */ - public function executeCommand(...$params): bool; + public function executeCommand(mixed ...$params): bool; public function enableCommandOutput(): void; diff --git a/src/ParameterBag.php b/src/ParameterBag.php index 5facda6..7787643 100644 --- a/src/ParameterBag.php +++ b/src/ParameterBag.php @@ -12,11 +12,8 @@ */ class ParameterBag implements ParameterBagInterface, \IteratorAggregate, \Countable { - protected array $parameters; - - public function __construct(array $parameters = []) + public function __construct(protected array $parameters = []) { - $this->parameters = $parameters; } /** diff --git a/src/ParameterBagInterface.php b/src/ParameterBagInterface.php index 0249354..a49d541 100644 --- a/src/ParameterBagInterface.php +++ b/src/ParameterBagInterface.php @@ -13,11 +13,9 @@ interface ParameterBagInterface { /** - * @param mixed $default - * * @return mixed|null */ - public function get(string $key, $default = null); + public function get(string $key, mixed $default = null); public function has(string $key): bool; diff --git a/src/Plugin/PathResolver.php b/src/Plugin/PathResolver.php index 4f0ddc9..8a6e7a6 100644 --- a/src/Plugin/PathResolver.php +++ b/src/Plugin/PathResolver.php @@ -17,12 +17,6 @@ */ class PathResolver implements PathResolverInterface { - private BuildInterface $build; - - private BuildLoggerInterface $buildLogger; - - private VariableInterpolatorInterface $variableInterpolator; - protected ParameterBag $buildSettings; private ?string $buildDirectory = null; @@ -35,15 +29,11 @@ class PathResolver implements PathResolverInterface private ?array $buildIgnores = null; public function __construct( - BuildInterface $build, - BuildLoggerInterface $buildLogger, - VariableInterpolatorInterface $variableInterpolator, + private readonly BuildInterface $build, + private readonly BuildLoggerInterface $buildLogger, + private readonly VariableInterpolatorInterface $variableInterpolator, array $projectConfig = [] ) { - $this->build = $build; - $this->buildLogger = $buildLogger; - $this->variableInterpolator = $variableInterpolator; - $this->initBuildSettings($projectConfig); } @@ -98,9 +88,7 @@ public function resolveIgnores(array $pluginIgnores, bool $onlyInBuildPath = tru if ($pluginIgnores) { $ignores = \array_merge( $ignores, - \array_filter($pluginIgnores, function ($item) { - return !empty($item); - }) + \array_filter($pluginIgnores, fn($item) => !empty($item)) ); } @@ -120,7 +108,7 @@ public function resolveIgnores(array $pluginIgnores, bool $onlyInBuildPath = tru foreach ($ignores as $index => $ignore) { if ( !$ignore || - ($onlyInBuildPath && '/' === \substr($ignore, 0, 1)) + ($onlyInBuildPath && \str_starts_with((string) $ignore, '/')) ) { unset($ignores[$index]); } @@ -134,7 +122,7 @@ public function resolveIgnores(array $pluginIgnores, bool $onlyInBuildPath = tru private function getRealPath(string $path, bool $isFile = false): string { $path = \rtrim( - \preg_replace( + (string) \preg_replace( '#[/]{2,}#', '/', \str_replace(['/', '\\'], '/', $path) @@ -231,9 +219,7 @@ private function getBuildIgnores(): array if ($buildSettingsIgnores) { $this->buildIgnores = \array_filter( $buildSettingsIgnores, - function (string $item) { - return !empty($item); - } + fn(string $item) => !empty($item) ); } } diff --git a/src/Plugin/Plugin.php b/src/Plugin/Plugin.php index a1b079a..d566919 100644 --- a/src/Plugin/Plugin.php +++ b/src/Plugin/Plugin.php @@ -23,26 +23,6 @@ */ abstract class Plugin implements PluginInterface { - protected BuildInterface $build; - - protected ProjectInterface $project; - - protected ApplicationInterface $application; - - protected BuildLoggerInterface $buildLogger; - - protected BuildErrorWriterInterface $buildErrorWriter; - - protected BuildMetaWriterInterface $buildMetaWriter; - - protected CommandExecutorInterface $commandExecutor; - - protected VariableInterpolatorInterface $variableInterpolator; - - protected PathResolverInterface $pathResolver; - - protected ContainerInterface $container; - protected ParameterBag $options; protected ParameterBag $buildSettings; @@ -82,28 +62,17 @@ abstract class Plugin implements PluginInterface * @throws \Throwable */ public function __construct( - BuildInterface $build, - ProjectInterface $project, - BuildLoggerInterface $buildLogger, - BuildErrorWriterInterface $buildErrorWriter, - BuildMetaWriterInterface $buildMetaWriter, - CommandExecutorInterface $commandExecutor, - VariableInterpolatorInterface $variableInterpolator, - PathResolverInterface $pathResolver, - ApplicationInterface $application, - ContainerInterface $container + protected BuildInterface $build, + protected ProjectInterface $project, + protected BuildLoggerInterface $buildLogger, + protected BuildErrorWriterInterface $buildErrorWriter, + protected BuildMetaWriterInterface $buildMetaWriter, + protected CommandExecutorInterface $commandExecutor, + protected VariableInterpolatorInterface $variableInterpolator, + protected PathResolverInterface $pathResolver, + protected ApplicationInterface $application, + protected ContainerInterface $container ) { - $this->build = $build; - $this->project = $project; - $this->buildLogger = $buildLogger; - $this->buildErrorWriter = $buildErrorWriter; - $this->buildMetaWriter = $buildMetaWriter; - $this->commandExecutor = $commandExecutor; - $this->variableInterpolator = $variableInterpolator; - $this->pathResolver = $pathResolver; - $this->application = $application; - $this->container = $container; - $this->initOptions(); $this->initBuildSettings(); @@ -186,7 +155,7 @@ protected function initOptions(): void { $projectConfig = $this->project->getBuildConfig(); - $pluginName = $this->getName(); + $pluginName = static::getName(); $pluginOptionsArray = []; if (!empty($projectConfig[$pluginName])) { $pluginOptionsArray = $projectConfig[$pluginName]; diff --git a/src/View/ViewInterface.php b/src/View/ViewInterface.php index 22d0eaf..1c290cf 100644 --- a/src/View/ViewInterface.php +++ b/src/View/ViewInterface.php @@ -19,10 +19,7 @@ public function hasVariable(string $key): bool; */ public function getVariable(string $key); - /** - * @param mixed $value - */ - public function setVariable(string $key, $value): bool; + public function setVariable(string $key, mixed $value): bool; public function setVariables(array $values): bool; diff --git a/tests/src/Plugin/PluginTest.php b/tests/src/Plugin/PluginTest.php index 621e703..73dee38 100644 --- a/tests/src/Plugin/PluginTest.php +++ b/tests/src/Plugin/PluginTest.php @@ -174,9 +174,7 @@ public function setUp(): void $this->variableInterpolator = $this->createMock(VariableInterpolatorInterface::class); $this->variableInterpolator ->method('interpolate') - ->willReturnCallback(function ($string) { - return \str_replace(['%BUILD_PATH%'], $this->buildPath, $string); - }); + ->willReturnCallback(fn($string) => \str_replace(['%BUILD_PATH%'], $this->buildPath, (string) $string)); $this->buildLogger = $this->createMock(BuildLoggerInterface::class); $this->buildErrorWriter = $this->createMock(BuildErrorWriterInterface::class); From 8316fe9dad8e41ace37782b20d44287f3f1e8cef Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sat, 10 Feb 2024 15:57:50 +0700 Subject: [PATCH 4/5] Added Rector to Makefile. --- Makefile | 5 ++++- rector.php | 17 +++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 64ca9cb..b73c270 100644 --- a/Makefile +++ b/Makefile @@ -31,5 +31,8 @@ code-style-fix: php-info install ## Fix code style psalm: php-info install ## Run Psalm check $(PHP) vendor/bin/psalm --config=psalm.xml.dist --threads=4 --show-snippet=true --show-info=true -.PHONY: php-info list install install-force update test test-coverage mutation-test code-style-fix psalm +rector: php-info install ## Run Rector + $(PHP) vendor/bin/rector process --clear-cache --dry-run + +.PHONY: php-info list install install-force update test test-coverage mutation-test code-style-fix psalm rector .DEFAULT_GOAL := list diff --git a/rector.php b/rector.php index af80c4a..bf7a4d5 100644 --- a/rector.php +++ b/rector.php @@ -5,21 +5,26 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Set\ValueObject\LevelSetList; -use Rector\Set\ValueObject\SetList; +//use Rector\Set\ValueObject\SetList; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ - __DIR__ . '/src' + __DIR__ . '/src', + __DIR__ . '/tests', ]); + $rectorConfig->skip([]); + + $rectorConfig->importNames(); + $rectorConfig->importShortClasses(false); + // register a single rule $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); $rectorConfig->sets([ LevelSetList::UP_TO_PHP_81, - //SetList::PHP_81, - //SetList::CODE_QUALITY, - //SetList::TYPE_DECLARATION_STRICT, - //SetList::DEAD_CODE, +/* SetList::PHP_81, + SetList::CODE_QUALITY, + SetList::DEAD_CODE,*/ ]); }; From 2d9084c0f343bb84d1ecae3fdeaa1af45645208f Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sun, 11 Feb 2024 22:51:43 +0700 Subject: [PATCH 5/5] Fixed Makefile task for Rector. --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b73c270..9074700 100644 --- a/Makefile +++ b/Makefile @@ -34,5 +34,8 @@ psalm: php-info install ## Run Psalm check rector: php-info install ## Run Rector $(PHP) vendor/bin/rector process --clear-cache --dry-run -.PHONY: php-info list install install-force update test test-coverage mutation-test code-style-fix psalm rector +rector-fix: php-info install ## Run Rector + $(PHP) vendor/bin/rector process --clear-cache + +.PHONY: php-info list install install-force update test test-coverage mutation-test code-style-fix psalm rector rector-fix .DEFAULT_GOAL := list