From fb068b24718a703e86dd380bd63963add34cac58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Sun, 25 Sep 2022 15:56:19 +0200 Subject: [PATCH] Leverage PHP 8.1 features (#704) --- rector.php | 27 +++++----- src/Box.php | 2 +- src/Compactor/Php.php | 10 ++-- src/Composer/ComposerOrchestrator.php | 2 +- src/Console/Application.php | 8 +-- src/Console/Command/Compile.php | 5 +- src/PhpScoper/ExcludedFilesScoper.php | 10 ++-- .../AppRequirementsFactory.php | 2 +- tests/Compactor/BaseCompactorTest.php | 2 +- .../Compactor/FileExtensionCompactorTest.php | 2 +- tests/Configuration/ConfigurationFileTest.php | 49 +++++++++++-------- tests/Configuration/ConfigurationTest.php | 4 +- tests/Console/Command/CompileTest.php | 2 +- tests/Console/Command/InfoTest.php | 6 +-- tests/DocumentationSchemaTest.php | 2 +- tests/FunctionsTest.php | 4 +- tests/Json/JsonTest.php | 4 +- vendor-bin/rector/composer.json | 2 +- 18 files changed, 65 insertions(+), 78 deletions(-) diff --git a/rector.php b/rector.php index 940b16ff5..9f19a2ac1 100644 --- a/rector.php +++ b/rector.php @@ -2,26 +2,23 @@ declare(strict_types=1); -use Rector\Core\Configuration\Option; +use Rector\Config\RectorConfig; use Rector\Set\ValueObject\LevelSetList; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -return static function (ContainerConfigurator $containerConfigurator): void { - $parameters = $containerConfigurator->parameters(); - - $parameters->set( - Option::PATHS, - [ - __DIR__ . '/src', - __DIR__ . '/tests', - ], - ); +return static function (RectorConfig $rectorConfig): void { + $rectorConfig->paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); - $parameters->set(Option::AUTOLOAD_PATHS, [ + $rectorConfig->autoloadPaths([ __DIR__ . '/vendor/autoload.php', __DIR__ . '/vendor-bin/rector/vendor/autoload.php', ]); - // Define what rule sets will be applied - $containerConfigurator->import(LevelSetList::UP_TO_PHP_81); + $rectorConfig->importNames(); + + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_81, + ]); }; diff --git a/src/Box.php b/src/Box.php index b21b86de8..0b709845a 100644 --- a/src/Box.php +++ b/src/Box.php @@ -115,7 +115,7 @@ public function endBuffering(?callable $dumpAutoload): void { Assert::true($this->buffering, 'The buffering must be started before ending it'); - $dumpAutoload = $dumpAutoload ?? static fn () => null; + $dumpAutoload ??= static fn () => null; $cwd = getcwd(); $tmp = make_tmp_dir('box', self::class); diff --git a/src/Compactor/Php.php b/src/Compactor/Php.php index 1c50c6385..9eb82a4d5 100644 --- a/src/Compactor/Php.php +++ b/src/Compactor/Php.php @@ -48,13 +48,11 @@ */ final class Php extends FileExtensionCompactor { - private DocblockAnnotationParser $annotationParser; - - public function __construct(DocblockAnnotationParser $annotationParser, array $extensions = ['php']) - { + public function __construct( + private DocblockAnnotationParser $annotationParser, + array $extensions = ['php'], + ) { parent::__construct($extensions); - - $this->annotationParser = $annotationParser; } protected function compactContent(string $contents): string diff --git a/src/Composer/ComposerOrchestrator.php b/src/Composer/ComposerOrchestrator.php index 6a0f13f05..fc288ac76 100644 --- a/src/Composer/ComposerOrchestrator.php +++ b/src/Composer/ComposerOrchestrator.php @@ -71,7 +71,7 @@ public static function dumpAutoload( bool $excludeDevFiles, IO $io = null, ): void { - $io = $io ?? IO::createNull(); + $io ??= IO::createNull(); $logger = new CompilerLogger($io); diff --git a/src/Console/Application.php b/src/Console/Application.php index 7f1f28af4..efe475d1b 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -26,21 +26,17 @@ final class Application implements FidryApplication { private string $version; private string $releaseDate; - private bool $autoExit; - private bool $catchExceptions; private string $header; public function __construct( private string $name = 'Box', ?string $version = null, string $releaseDate = '@release-date@', - bool $autoExit = true, - bool $catchExceptions = true, + private bool $autoExit = true, + private bool $catchExceptions = true, ) { $this->version = $version ?? get_box_version(); $this->releaseDate = !str_contains($releaseDate, '@') ? $releaseDate : ''; - $this->autoExit = $autoExit; - $this->catchExceptions = $catchExceptions; } public function getName(): string diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index b75f71325..5836ff79d 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -105,11 +105,8 @@ final class Compile implements CommandAware private const DEBUG_DIR = '.box_dump'; - private string $header; - - public function __construct(string $header) + public function __construct(private string $header) { - $this->header = $header; } public function getConfiguration(): CommandConfiguration diff --git a/src/PhpScoper/ExcludedFilesScoper.php b/src/PhpScoper/ExcludedFilesScoper.php index 7a51ac170..a7982455e 100644 --- a/src/PhpScoper/ExcludedFilesScoper.php +++ b/src/PhpScoper/ExcludedFilesScoper.php @@ -14,19 +14,19 @@ namespace KevinGH\Box\PhpScoper; +use function array_flip; use function array_key_exists; use function func_get_args; use Humbug\PhpScoper\Scoper\Scoper as PhpScoperScoper; -use function Safe\array_flip; final class ExcludedFilesScoper implements PhpScoperScoper { - private PhpScoperScoper $decoratedScoper; private array $excludedFilePathsAsKeys; - public function __construct(PhpScoperScoper $decoratedScoper, string ...$excludedFilePaths) - { - $this->decoratedScoper = $decoratedScoper; + public function __construct( + private PhpScoperScoper $decoratedScoper, + string ...$excludedFilePaths, + ) { $this->excludedFilePathsAsKeys = array_flip($excludedFilePaths); } diff --git a/src/RequirementChecker/AppRequirementsFactory.php b/src/RequirementChecker/AppRequirementsFactory.php index 4a7d7558f..47e8f8d40 100644 --- a/src/RequirementChecker/AppRequirementsFactory.php +++ b/src/RequirementChecker/AppRequirementsFactory.php @@ -220,7 +220,7 @@ private static function collectComposerLockExtensionRequirements(array $composer if (1 === preg_match('/symfony\/polyfill-(?.+)/', (string) $packageInfo['name'], $matches)) { $extension = $matches['extension']; - if (!str_starts_with($extension, 'php')) { + if (!str_starts_with((string) $extension, 'php')) { $polyfills[$extension] = true; } } diff --git a/tests/Compactor/BaseCompactorTest.php b/tests/Compactor/BaseCompactorTest.php index ff96a80d4..35417af31 100644 --- a/tests/Compactor/BaseCompactorTest.php +++ b/tests/Compactor/BaseCompactorTest.php @@ -32,7 +32,7 @@ public function test_it_returns_the_contents_unchanged_if_does_not_support_the_f $compactor = new class() extends BaseCompactor { protected function compactContent(string $contents): string { - throw UnsupportedMethodCall::forMethod(__CLASS__, __METHOD__); + throw UnsupportedMethodCall::forMethod(self::class, __METHOD__); } protected function supports(string $file): bool diff --git a/tests/Compactor/FileExtensionCompactorTest.php b/tests/Compactor/FileExtensionCompactorTest.php index 61f52bb77..e0781333f 100644 --- a/tests/Compactor/FileExtensionCompactorTest.php +++ b/tests/Compactor/FileExtensionCompactorTest.php @@ -32,7 +32,7 @@ public function test_it_does_not_support_files_with_unknown_extension(): void $compactor = new class([]) extends FileExtensionCompactor { protected function compactContent(string $contents): string { - throw UnsupportedMethodCall::forMethod(__CLASS__, __METHOD__); + throw UnsupportedMethodCall::forMethod(self::class, __METHOD__); } }; diff --git a/tests/Configuration/ConfigurationFileTest.php b/tests/Configuration/ConfigurationFileTest.php index a26025e5b..ec4404a22 100644 --- a/tests/Configuration/ConfigurationFileTest.php +++ b/tests/Configuration/ConfigurationFileTest.php @@ -1118,10 +1118,8 @@ public function test_a_recommendation_is_given_if_the_blacklist_is_set_with_its_ /** * @dataProvider jsonValidNonStringArrayProvider - * - * @param mixed $value */ - public function test_blacklist_value_must_be_an_array_of_strings($value): void + public function test_blacklist_value_must_be_an_array_of_strings(mixed $value): void { try { $this->setConfig([ @@ -1178,10 +1176,8 @@ public function test_the_blacklist_input_may_refer_to_non_existent_paths(): void /** * @dataProvider jsonValidNonStringArrayProvider - * - * @param mixed $value */ - public function test_files_value_must_be_an_array_of_strings($value): void + public function test_files_value_must_be_an_array_of_strings(mixed $value): void { try { $this->setConfig([ @@ -1212,10 +1208,8 @@ public function test_a_recommendation_is_given_when_the_files_are_set_to_an_empt /** * @dataProvider jsonValidNonStringArrayProvider - * - * @param mixed $value */ - public function test_bin_files_value_must_be_an_array_of_strings($value): void + public function test_bin_files_value_must_be_an_array_of_strings(mixed $value): void { try { $this->setConfig([ @@ -1259,10 +1253,8 @@ public function test_the_files_and_bin_files_input_is_normalized(): void /** * @dataProvider jsonValidNonStringArrayProvider - * - * @param mixed $value */ - public function test_directories_value_must_be_an_array_of_strings($value): void + public function test_directories_value_must_be_an_array_of_strings(mixed $value): void { try { $this->setConfig([ @@ -1293,10 +1285,8 @@ public function test_a_recommendation_is_given_when_an_emtpy_array_is_given_for_ /** * @dataProvider jsonValidNonStringArrayProvider - * - * @param mixed $value */ - public function test_bin_directories_value_must_be_an_array_of_strings($value): void + public function test_bin_directories_value_must_be_an_array_of_strings(mixed $value): void { try { $this->setConfig([ @@ -1743,7 +1733,7 @@ public function test_no_warning_is_given_when_no_installed_json_no_composer_lock { $config = Configuration::create( $configPath = self::FIXTURES_DIR.'/dir000/box.json', - json_decode(file_get_contents($configPath), false), + json_decode(file_get_contents($configPath)), ); $this->assertSame([], $config->getRecommendations()); @@ -1754,7 +1744,7 @@ public function test_no_warning_is_given_when_the_installed_json_and_composer_lo { $config = Configuration::create( $configPath = self::FIXTURES_DIR.'/dir001/box.json', - json_decode(file_get_contents($configPath), false), + json_decode(file_get_contents($configPath)), ); $this->assertTrue($config->dumpAutoload()); @@ -1765,7 +1755,12 @@ public function test_no_warning_is_given_when_the_installed_json_and_composer_lo public function test_no_warning_is_given_when_the_installed_json_is_found_and_the_composer_lock_is_not_when_the_composer_autoloader_is_not_dumped(): void { - $config = Configuration::create($configPath = self::FIXTURES_DIR.'/dir002/box.json', json_decode(file_get_contents($configPath))); + $configPath = self::FIXTURES_DIR.'/dir002/box.json'; + + $config = Configuration::create( + $configPath, + json_decode(file_get_contents($configPath)), + ); $this->assertFalse($config->dumpAutoload()); @@ -1775,7 +1770,9 @@ public function test_no_warning_is_given_when_the_installed_json_is_found_and_th public function test_a_warning_is_given_when_no_installed_json_is_found_and_the_composer_lock_is_when_the_composer_autoloader_is_dumped(): void { - $decodedConfig = json_decode(file_get_contents($configPath = self::FIXTURES_DIR.'/dir002/box.json')); + $configPath = self::FIXTURES_DIR.'/dir002/box.json'; + + $decodedConfig = json_decode(file_get_contents($configPath)); $decodedConfig->{'dump-autoload'} = true; $config = Configuration::create($configPath, $decodedConfig); @@ -1809,7 +1806,12 @@ public function test_no_warning_is_given_when_the_installed_json_is_found_and_th public function test_a_warning_is_given_when_the_installed_json_is_found_and_the_composer_lock_is_not(): void { - $config = Configuration::create($configPath = self::FIXTURES_DIR.'/dir003/box.json', json_decode(file_get_contents($configPath))); + $configPath = self::FIXTURES_DIR.'/dir003/box.json'; + + $config = Configuration::create( + $configPath, + json_decode(file_get_contents($configPath)), + ); $this->assertFalse($config->dumpAutoload()); @@ -1825,7 +1827,12 @@ public function test_a_warning_is_given_when_the_installed_json_is_found_and_the public function test_no_warning_is_given_when_the_installed_json_is_found_and_the_composer_lock_is_not_and_the_dump_autoload_disabled(): void { - $config = Configuration::create($configPath = self::FIXTURES_DIR.'/dir004/box.json', json_decode(file_get_contents($configPath))); + $configPath = self::FIXTURES_DIR.'/dir004/box.json'; + + $config = Configuration::create( + $configPath, + json_decode(file_get_contents($configPath)), + ); $this->assertFalse($config->dumpAutoload()); diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php index d351de091..b62436912 100644 --- a/tests/Configuration/ConfigurationTest.php +++ b/tests/Configuration/ConfigurationTest.php @@ -779,10 +779,8 @@ public function test_the_compression_algorithm_with_a_string(): void /** * @dataProvider invalidCompressionAlgorithmsProvider - * - * @param mixed $compression */ - public function test_the_compression_algorithm_cannot_be_an_invalid_algorithm($compression, string $errorMessage): void + public function test_the_compression_algorithm_cannot_be_an_invalid_algorithm(mixed $compression, string $errorMessage): void { try { $this->setConfig([ diff --git a/tests/Console/Command/CompileTest.php b/tests/Console/Command/CompileTest.php index 8954ae4f7..2a593cd7c 100644 --- a/tests/Console/Command/CompileTest.php +++ b/tests/Console/Command/CompileTest.php @@ -3097,7 +3097,7 @@ private function retrievePharFiles(Phar $phar, ?Traversable $traversable = null) foreach ($traversable as $fileInfo) { /** @var PharFileInfo $fileInfo */ - $fileInfo = $phar[str_replace($root, '', $fileInfo->getPathname())]; + $fileInfo = $phar[str_replace($root, '', (string) $fileInfo->getPathname())]; $path = substr($fileInfo->getPathname(), strlen($root) - 1); diff --git a/tests/Console/Command/InfoTest.php b/tests/Console/Command/InfoTest.php index 3b43bb6da..6845742ed 100644 --- a/tests/Console/Command/InfoTest.php +++ b/tests/Console/Command/InfoTest.php @@ -437,13 +437,11 @@ public function test_it_provides_a_phar_info_with_the_tree_of_the_content_includ /** * @dataProvider treeDepthProvider - * - * @param mixed $expected */ public function test_it_can_limit_the_tree_depth( string $pharPath, ?string $depth, - $expected, + mixed $expected, ): void { $pharPath = self::FIXTURES.'/tree-phar.phar'; $phar = new Phar($pharPath); @@ -462,7 +460,7 @@ public function test_it_can_limit_the_tree_depth( $signature['hash_type'], $signature['hash'], ], - $expected, + (string) $expected, ); $input = [ diff --git a/tests/DocumentationSchemaTest.php b/tests/DocumentationSchemaTest.php index 56f97fc4d..fd7e142b3 100644 --- a/tests/DocumentationSchemaTest.php +++ b/tests/DocumentationSchemaTest.php @@ -107,7 +107,7 @@ private function retrieveDocSchemaKeys(): array ), ); - return array_keys(json_decode($matches['schema'], true, 512, JSON_THROW_ON_ERROR)); + return array_keys(json_decode((string) $matches['schema'], true, 512, JSON_THROW_ON_ERROR)); } /** diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 079412b38..823185b18 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -38,10 +38,8 @@ public function test_it_can_provide_the_phars_algorithms(): void /** * @dataProvider pharCompressionAlgorithmProvider - * - * @param mixed $expected */ - public function test_it_can_provide_the_phars_algorithm_extensions(int $algorithm, $expected): void + public function test_it_can_provide_the_phars_algorithm_extensions(int $algorithm, mixed $expected): void { try { $actual = get_phar_compression_algorithm_extension($algorithm); diff --git a/tests/Json/JsonTest.php b/tests/Json/JsonTest.php index f3be5a8e3..edb526529 100644 --- a/tests/Json/JsonTest.php +++ b/tests/Json/JsonTest.php @@ -69,10 +69,8 @@ public function test_it_can_lint_a_json_string(string $json, ?Throwable $expecte /** * @dataProvider jsonToDecodeProvider - * - * @param mixed $expected */ - public function test_it_can_decode_a_json_string(string $json, bool $assoc, $expected, ?Throwable $expectedThrowable): void + public function test_it_can_decode_a_json_string(string $json, bool $assoc, mixed $expected, ?Throwable $expectedThrowable): void { if (null === $expected) { Assert::notNull($expectedThrowable); diff --git a/vendor-bin/rector/composer.json b/vendor-bin/rector/composer.json index e59249142..80a436601 100644 --- a/vendor-bin/rector/composer.json +++ b/vendor-bin/rector/composer.json @@ -1,5 +1,5 @@ { "require-dev": { - "rector/rector": "^0.12.16" + "rector/rector": "^0.14" } }