From 9870fe7fc87598e11baeb5b48090179c8a75cfa1 Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Tue, 4 Oct 2022 01:10:51 +0300 Subject: [PATCH] Add rector (#232) --- .github/workflows/rector.yml | 65 +++++++++++++++++++ composer.json | 3 +- config/replicate/files.php | 16 +++++ rector.php | 22 +++++++ src/App/Command/Composer/UpdateCommand.php | 5 +- src/App/Command/Git/CloneCommand.php | 5 +- .../Github/ForksRepositoriesCommand.php | 2 +- .../Command/Github/ProtectBranchCommand.php | 2 +- src/App/Command/InstallCommand.php | 5 +- src/App/Command/Release/MakeCommand.php | 2 +- src/App/Command/Release/WhatCommand.php | 4 +- src/App/Command/Stats/ContributorsCommand.php | 4 +- src/App/Command/TestCommand.php | 4 +- src/App/Command/UpdateCommand.php | 5 +- src/App/Component/Console/OutputManager.php | 5 +- src/App/Component/Console/PackageCommand.php | 10 +-- src/App/Component/Package/Package.php | 6 +- src/App/Component/Package/PackageError.php | 9 +-- src/App/Component/Package/ReplicationSet.php | 11 +--- src/Infrastructure/Changelog.php | 16 ++--- src/Infrastructure/CodeUsage/CodeUsage.php | 6 +- .../NamespaceUsageFinderNameResolver.php | 10 +-- .../Composer/ComposerInstallation.php | 6 +- .../Composer/ComposerPackage.php | 6 +- .../Composer/ComposerPackageUsageAnalyzer.php | 7 +- .../Composer/Config/ComposerConfig.php | 11 ++-- .../ComposerConfigDependenciesModifier.php | 8 +-- .../Dependency/ComposerConfigDependency.php | 13 +--- src/Infrastructure/Version.php | 7 +- .../Fixture/Custom/lib/Non-PSR/VarStorage.php | 5 +- .../Fixture/Production/src/Config/Config.php | 4 +- .../CodeUsage/NamespaceUsageFinderTest.php | 14 ++-- 32 files changed, 158 insertions(+), 140 deletions(-) create mode 100644 .github/workflows/rector.yml create mode 100644 rector.php diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml new file mode 100644 index 00000000..b235fb04 --- /dev/null +++ b/.github/workflows/rector.yml @@ -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 + diff --git a/composer.json b/composer.json index 0aeeac58..d195d451 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/replicate/files.php b/config/replicate/files.php index 191493cd..a1ba8886 100644 --- a/config/replicate/files.php +++ b/config/replicate/files.php @@ -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', + ], + ], ]; diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..63713ce9 --- /dev/null +++ b/rector.php @@ -0,0 +1,22 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80, + ]); +}; diff --git a/src/App/Command/Composer/UpdateCommand.php b/src/App/Command/Composer/UpdateCommand.php index 18842092..23be479a 100644 --- a/src/App/Command/Composer/UpdateCommand.php +++ b/src/App/Command/Composer/UpdateCommand.php @@ -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); } diff --git a/src/App/Command/Git/CloneCommand.php b/src/App/Command/Git/CloneCommand.php index d97f4691..90e4be32 100644 --- a/src/App/Command/Git/CloneCommand.php +++ b/src/App/Command/Git/CloneCommand.php @@ -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); } diff --git a/src/App/Command/Github/ForksRepositoriesCommand.php b/src/App/Command/Github/ForksRepositoriesCommand.php index 731c717e..1d5cc5db 100644 --- a/src/App/Command/Github/ForksRepositoriesCommand.php +++ b/src/App/Command/Github/ForksRepositoriesCommand.php @@ -17,7 +17,7 @@ final class ForksRepositoriesCommand extends Command { - private ?OutputManager $io; + private ?OutputManager $io = null; protected function initialize(InputInterface $input, OutputInterface $output) { diff --git a/src/App/Command/Github/ProtectBranchCommand.php b/src/App/Command/Github/ProtectBranchCommand.php index 04636093..d7031abf 100644 --- a/src/App/Command/Github/ProtectBranchCommand.php +++ b/src/App/Command/Github/ProtectBranchCommand.php @@ -14,7 +14,7 @@ final class ProtectBranchCommand extends PackageCommand { - private ?string $branch; + private ?string $branch = null; protected function configure() { diff --git a/src/App/Command/InstallCommand.php b/src/App/Command/InstallCommand.php index b9f7ab32..95aef417 100644 --- a/src/App/Command/InstallCommand.php +++ b/src/App/Command/InstallCommand.php @@ -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); } diff --git a/src/App/Command/Release/MakeCommand.php b/src/App/Command/Release/MakeCommand.php index 97718683..ce529acd 100644 --- a/src/App/Command/Release/MakeCommand.php +++ b/src/App/Command/Release/MakeCommand.php @@ -22,7 +22,7 @@ final class MakeCommand extends PackageCommand { - private ?string $tag; + private ?string $tag = null; private const MAIN_BRANCHES = ['master', 'main']; diff --git a/src/App/Command/Release/WhatCommand.php b/src/App/Command/Release/WhatCommand.php index 0ced64b1..afeb9aef 100644 --- a/src/App/Command/Release/WhatCommand.php +++ b/src/App/Command/Release/WhatCommand.php @@ -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() { diff --git a/src/App/Command/Stats/ContributorsCommand.php b/src/App/Command/Stats/ContributorsCommand.php index 6bdca442..504df816 100644 --- a/src/App/Command/Stats/ContributorsCommand.php +++ b/src/App/Command/Stats/ContributorsCommand.php @@ -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() { diff --git a/src/App/Command/TestCommand.php b/src/App/Command/TestCommand.php index 69aeb605..e7963774 100644 --- a/src/App/Command/TestCommand.php +++ b/src/App/Command/TestCommand.php @@ -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 { @@ -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'); } } diff --git a/src/App/Command/UpdateCommand.php b/src/App/Command/UpdateCommand.php index 211bb18e..88d6fd02 100644 --- a/src/App/Command/UpdateCommand.php +++ b/src/App/Command/UpdateCommand.php @@ -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); } diff --git a/src/App/Component/Console/OutputManager.php b/src/App/Component/Console/OutputManager.php index 7fe8fb53..f3b267ef 100644 --- a/src/App/Component/Console/OutputManager.php +++ b/src/App/Component/Console/OutputManager.php @@ -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 @@ -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. * diff --git a/src/App/Component/Console/PackageCommand.php b/src/App/Component/Console/PackageCommand.php index 24c5776f..4409c79c 100644 --- a/src/App/Component/Console/PackageCommand.php +++ b/src/App/Component/Console/PackageCommand.php @@ -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; @@ -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([ diff --git a/src/App/Component/Package/Package.php b/src/App/Component/Package/Package.php index db2c80b5..c466aa5f 100644 --- a/src/App/Component/Package/Package.php +++ b/src/App/Component/Package/Package.php @@ -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 { @@ -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.'); diff --git a/src/App/Component/Package/PackageError.php b/src/App/Component/Package/PackageError.php index 5f28c0ac..46ad4b2b 100644 --- a/src/App/Component/Package/PackageError.php +++ b/src/App/Component/Package/PackageError.php @@ -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 diff --git a/src/App/Component/Package/ReplicationSet.php b/src/App/Component/Package/ReplicationSet.php index c7a48e6c..e98cc5d6 100644 --- a/src/App/Component/Package/ReplicationSet.php +++ b/src/App/Component/Package/ReplicationSet.php @@ -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 diff --git a/src/Infrastructure/Changelog.php b/src/Infrastructure/Changelog.php index f1ff7c7b..043f4ddc 100644 --- a/src/Infrastructure/Changelog.php +++ b/src/Infrastructure/Changelog.php @@ -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 @@ -67,8 +64,6 @@ public function close(Version $version): void } /** - * @param Version $version - * * @return string[] */ public function getReleaseNotes(Version $version): array @@ -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; } @@ -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)) { diff --git a/src/Infrastructure/CodeUsage/CodeUsage.php b/src/Infrastructure/CodeUsage/CodeUsage.php index 3ea6fed5..777b16a7 100644 --- a/src/Infrastructure/CodeUsage/CodeUsage.php +++ b/src/Infrastructure/CodeUsage/CodeUsage.php @@ -8,8 +8,6 @@ class CodeUsage { - private string $identifier; - /** * @var string[] */ @@ -19,7 +17,7 @@ class CodeUsage * @param string $identifier Unique identifier of code usage: namespace, package name, etc. * @param string|string[] $environments Environment(s) in which the code is used. */ - public function __construct(string $identifier, $environments) + public function __construct(private string $identifier, $environments) { $environments = (array) $environments; @@ -28,8 +26,6 @@ public function __construct(string $identifier, $environments) throw new InvalidArgumentException('Each environment must be a string.'); } } - - $this->identifier = $identifier; $this->environments = $environments; } diff --git a/src/Infrastructure/CodeUsage/NamespaceUsageFinderNameResolver.php b/src/Infrastructure/CodeUsage/NamespaceUsageFinderNameResolver.php index b243e52a..0b58d677 100644 --- a/src/Infrastructure/CodeUsage/NamespaceUsageFinderNameResolver.php +++ b/src/Infrastructure/CodeUsage/NamespaceUsageFinderNameResolver.php @@ -10,18 +10,12 @@ class NamespaceUsageFinderNameResolver extends NameResolver { - protected string $environment; - protected NamespaceUsageFinder $namespaceUsageFinder; - public function __construct( - NamespaceUsageFinder $namespaceUsageFinder, - string $environment, + protected NamespaceUsageFinder $namespaceUsageFinder, + protected string $environment, ErrorHandler $errorHandler = null, array $options = [] ) { - $this->environment = $environment; - $this->namespaceUsageFinder = $namespaceUsageFinder; - parent::__construct($errorHandler, $options); } diff --git a/src/Infrastructure/Composer/ComposerInstallation.php b/src/Infrastructure/Composer/ComposerInstallation.php index a9324106..4ffb1cc8 100644 --- a/src/Infrastructure/Composer/ComposerInstallation.php +++ b/src/Infrastructure/Composer/ComposerInstallation.php @@ -8,13 +8,11 @@ class ComposerInstallation { - private ComposerPackage $rootPackage; private array $installedDependencies; private array $notInstalledDependencies; - public function __construct(ComposerPackage $rootPackage) + public function __construct(private ComposerPackage $rootPackage) { - $this->rootPackage = $rootPackage; $this->analyzeDependencies(); } @@ -40,8 +38,6 @@ public function getInstalledDependencyPackages(): array } /** - * @param string $section - * * @return ComposerPackage[] */ public function getDependencyPackages(string $section): array diff --git a/src/Infrastructure/Composer/ComposerPackage.php b/src/Infrastructure/Composer/ComposerPackage.php index b4bae3f6..6f90552b 100644 --- a/src/Infrastructure/Composer/ComposerPackage.php +++ b/src/Infrastructure/Composer/ComposerPackage.php @@ -9,14 +9,10 @@ class ComposerPackage { - private string $name; - private string $path; private ?ComposerConfig $config = null; - public function __construct(string $name, string $path) + public function __construct(private string $name, private string $path) { - $this->name = $name; - $this->path = $path; } public function getName(): string diff --git a/src/Infrastructure/Composer/ComposerPackageUsageAnalyzer.php b/src/Infrastructure/Composer/ComposerPackageUsageAnalyzer.php index 06fd4359..7ef4a317 100644 --- a/src/Infrastructure/Composer/ComposerPackageUsageAnalyzer.php +++ b/src/Infrastructure/Composer/ComposerPackageUsageAnalyzer.php @@ -57,7 +57,7 @@ public function analyze(): void foreach ($this->packages as $package) { foreach ($package->getPSRNamespaces() as $packageNamespace) { foreach ($this->namespaceUsages as $namespaceUsage) { - if (strpos($namespaceUsage->getIdentifier(), "\\$packageNamespace") === 0) { + if (str_starts_with($namespaceUsage->getIdentifier(), "\\$packageNamespace")) { $this->registerPackageUsage($package->getName(), $namespaceUsage->getEnvironments()); } } @@ -66,8 +66,6 @@ public function analyze(): void } /** - * @param string $environment - * * @return string[] array of package names. */ public function getNamesOfPackagesUsedInSpecifiedEnvironment(string $environment): array @@ -84,8 +82,6 @@ public function getNamesOfPackagesUsedInSpecifiedEnvironment(string $environment } /** - * @param string $environment - * * @return string[] array of package names. */ public function getNamesOfPackagesUsedOnlyInSpecifiedEnvironment(string $environment): array @@ -151,7 +147,6 @@ public function getUnusedPackageNames(): array } /** - * @param string $packageName * @param string[] $environments */ private function registerPackageUsage(string $packageName, array $environments): void diff --git a/src/Infrastructure/Composer/Config/ComposerConfig.php b/src/Infrastructure/Composer/Config/ComposerConfig.php index eec6f55d..a9cf486f 100644 --- a/src/Infrastructure/Composer/Config/ComposerConfig.php +++ b/src/Infrastructure/Composer/Config/ComposerConfig.php @@ -16,11 +16,8 @@ class ComposerConfig public const SECTION_REQUIRE = 'require'; public const SECTION_REQUIRE_DEV = 'require-dev'; - private array $data; - - private function __construct(array $data) + private function __construct(private array $data) { - $this->data = $data; } public static function getAllDependencySections(): array @@ -101,15 +98,15 @@ public function getPSRNamespaces(): array } if (isset($this->data['autoload-dev']['psr-4'])) { - $namespaces = array_merge($namespaces, array_keys($this->data['autoload-dev']['psr-4'])); + $namespaces = [...$namespaces, ...array_keys($this->data['autoload-dev']['psr-4'])]; } if (isset($this->data['autoload']['psr-0'])) { - $namespaces = array_merge($namespaces, array_keys($this->data['autoload']['psr-0'])); + $namespaces = [...$namespaces, ...array_keys($this->data['autoload']['psr-0'])]; } if (isset($this->data['autoload-dev']['psr-0'])) { - $namespaces = array_merge($namespaces, array_keys($this->data['autoload-dev']['psr-0'])); + $namespaces = [...$namespaces, ...array_keys($this->data['autoload-dev']['psr-0'])]; } return $namespaces; diff --git a/src/Infrastructure/Composer/Config/ComposerConfigDependenciesModifier.php b/src/Infrastructure/Composer/Config/ComposerConfigDependenciesModifier.php index 5ffaf503..09d83266 100644 --- a/src/Infrastructure/Composer/Config/ComposerConfigDependenciesModifier.php +++ b/src/Infrastructure/Composer/Config/ComposerConfigDependenciesModifier.php @@ -8,14 +8,8 @@ class ComposerConfigDependenciesModifier { - /** - * @var ComposerConfig - */ - private ComposerConfig $config; - - public function __construct(ComposerConfig $config) + public function __construct(private ComposerConfig $config) { - $this->config = $config; } /** diff --git a/src/Infrastructure/Composer/Config/Dependency/ComposerConfigDependency.php b/src/Infrastructure/Composer/Config/Dependency/ComposerConfigDependency.php index d4a9e12f..32ff6686 100644 --- a/src/Infrastructure/Composer/Config/Dependency/ComposerConfigDependency.php +++ b/src/Infrastructure/Composer/Config/Dependency/ComposerConfigDependency.php @@ -11,13 +11,8 @@ class ComposerConfigDependency */ private const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit|-ipv6|-zts|-debug)?|hhvm|(?:ext|lib)-[a-z0-9](?:[_.-]?[a-z0-9]+)*|composer-(?:plugin|runtime)-api)$}iD'; - private string $packageName; - private string $constraint; - - public function __construct(string $packageName, string $constraint) + public function __construct(private string $packageName, private string $constraint) { - $this->packageName = $packageName; - $this->constraint = $constraint; } public function getPackageName(): string @@ -32,13 +27,11 @@ public function getConstraint(): string /** * @param string[] $flags - * - * @return bool */ public function constraintContainsAnyOfStabilityFlags(array $flags): bool { foreach ($flags as $flag) { - if (strpos($this->constraint, $flag) !== false) { + if (str_contains($this->constraint, $flag)) { return true; } } @@ -62,7 +55,7 @@ public function getPriority(): int return 0; } - if (strpos($name, 'ext-') === 0) { + if (str_starts_with($name, 'ext-')) { return 1; } diff --git a/src/Infrastructure/Version.php b/src/Infrastructure/Version.php index 73ec486d..9583782b 100644 --- a/src/Infrastructure/Version.php +++ b/src/Infrastructure/Version.php @@ -4,7 +4,7 @@ namespace Yiisoft\YiiDevTool\Infrastructure; -final class Version +final class Version implements \Stringable { public const TYPE_MAJOR = 'Major - Incompatible API changes.'; public const TYPE_MINOR = 'Minor - Add functionality (backwards-compatible).'; @@ -12,11 +12,8 @@ final class Version public const TYPES = [self::TYPE_PATCH, self::TYPE_MINOR, self::TYPE_MAJOR]; - private string $version; - - public function __construct(string $version) + public function __construct(private string $version) { - $this->version = $version; } public function __toString(): string diff --git a/tests/Infrastructure/CodeUsage/Fixture/Custom/lib/Non-PSR/VarStorage.php b/tests/Infrastructure/CodeUsage/Fixture/Custom/lib/Non-PSR/VarStorage.php index 53049248..bd2c3b07 100644 --- a/tests/Infrastructure/CodeUsage/Fixture/Custom/lib/Non-PSR/VarStorage.php +++ b/tests/Infrastructure/CodeUsage/Fixture/Custom/lib/Non-PSR/VarStorage.php @@ -8,10 +8,7 @@ class VarStorage { - private array $vars; - - public function __construct(array $vars = []) + public function __construct(private array $vars = []) { - $this->vars = $vars; } } diff --git a/tests/Infrastructure/CodeUsage/Fixture/Production/src/Config/Config.php b/tests/Infrastructure/CodeUsage/Fixture/Production/src/Config/Config.php index d55e31e6..a00af511 100644 --- a/tests/Infrastructure/CodeUsage/Fixture/Production/src/Config/Config.php +++ b/tests/Infrastructure/CodeUsage/Fixture/Production/src/Config/Config.php @@ -20,13 +20,11 @@ function count($array) class Config extends \Production\NonPSRNamespace\Config { - private array $data; private int $size; private int $readTime; - public function __construct(array $data = []) + public function __construct(private array $data = []) { - $this->data = $data; $this->size = count($data); /** @noinspection PhpFullyQualifiedNameUsageInspection */ diff --git a/tests/Infrastructure/CodeUsage/NamespaceUsageFinderTest.php b/tests/Infrastructure/CodeUsage/NamespaceUsageFinderTest.php index 232a6dec..00257a92 100644 --- a/tests/Infrastructure/CodeUsage/NamespaceUsageFinderTest.php +++ b/tests/Infrastructure/CodeUsage/NamespaceUsageFinderTest.php @@ -31,15 +31,15 @@ public function testGetUsages() $expectedResults = [ // Used in custom environment only - '\Custom\NonPSRNamespace\VarStorage' => ['custom-environment'], - '\Custom\Storage\VarStorage' => ['custom-environment'], - '\SplStack' => ['custom-environment'], + '\\' . \Custom\NonPSRNamespace\VarStorage::class => ['custom-environment'], + '\\' . \Custom\Storage\VarStorage::class => ['custom-environment'], + '\\' . \SplStack::class => ['custom-environment'], // Used in production environment only - '\Production\Config\Config' => [CodeUsageEnvironment::PRODUCTION], - '\Production\NonPSRNamespace\Config' => [CodeUsageEnvironment::PRODUCTION], - '\Production\Spl\SplFixedArray' => [CodeUsageEnvironment::PRODUCTION], - '\SplFixedArray' => [CodeUsageEnvironment::PRODUCTION], + '\\' . \Production\Config\Config::class => [CodeUsageEnvironment::PRODUCTION], + '\\' . \Production\NonPSRNamespace\Config::class => [CodeUsageEnvironment::PRODUCTION], + '\\' . \Production\Spl\SplFixedArray::class => [CodeUsageEnvironment::PRODUCTION], + '\\' . \SplFixedArray::class => [CodeUsageEnvironment::PRODUCTION], // Used in both environments '\time' => [CodeUsageEnvironment::PRODUCTION, 'custom-environment'],