Skip to content

Commit

Permalink
Leverage PHP 8.1 features (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Sep 25, 2022
1 parent 2a09afe commit fb068b2
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 78 deletions.
27 changes: 12 additions & 15 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
};
2 changes: 1 addition & 1 deletion src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions src/Compactor/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/ComposerOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 2 additions & 6 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/PhpScoper/ExcludedFilesScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private static function collectComposerLockExtensionRequirements(array $composer
if (1 === preg_match('/symfony\/polyfill-(?<extension>.+)/', (string) $packageInfo['name'], $matches)) {
$extension = $matches['extension'];

if (!str_starts_with($extension, 'php')) {
if (!str_starts_with((string) $extension, 'php')) {
$polyfills[$extension] = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Compactor/BaseCompactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Compactor/FileExtensionCompactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}
};

Expand Down
49 changes: 28 additions & 21 deletions tests/Configuration/ConfigurationFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());

Expand All @@ -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);
Expand Down Expand Up @@ -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());

Expand All @@ -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());

Expand Down
4 changes: 1 addition & 3 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions tests/Console/Command/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -462,7 +460,7 @@ public function test_it_can_limit_the_tree_depth(
$signature['hash_type'],
$signature['hash'],
],
$expected,
(string) $expected,
);

$input = [
Expand Down
2 changes: 1 addition & 1 deletion tests/DocumentationSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions tests/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion vendor-bin/rector/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require-dev": {
"rector/rector": "^0.12.16"
"rector/rector": "^0.14"
}
}

0 comments on commit fb068b2

Please sign in to comment.