From 1854c96dea2ec1fc8d0d7d80de7cabbaee69c4a2 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 19 Mar 2024 10:43:38 +0100 Subject: [PATCH 1/2] Make implicit nullable types explicit --- src/Cache.php | 2 +- src/Command/UpdateRecipesCommand.php | 2 +- src/Configurator/EnvConfigurator.php | 2 +- src/Downloader.php | 2 +- src/Flex.php | 4 ++-- src/Options.php | 2 +- src/ParallelDownloader.php | 2 +- src/ScriptExecutor.php | 2 +- src/TruncatedComposerRepository.php | 2 +- src/Unpacker.php | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Cache.php b/src/Cache.php index 6ecca1590..f861e4d96 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -30,7 +30,7 @@ class Cache extends BaseCache private $downloader; private $io; - public function setSymfonyRequire(string $symfonyRequire, RootPackageInterface $rootPackage, Downloader $downloader, IOInterface $io = null) + public function setSymfonyRequire(string $symfonyRequire, RootPackageInterface $rootPackage, Downloader $downloader, ?IOInterface $io = null) { $this->versionParser = new VersionParser(); $this->symfonyRequire = $symfonyRequire; diff --git a/src/Command/UpdateRecipesCommand.php b/src/Command/UpdateRecipesCommand.php index c75507eca..15296efc1 100644 --- a/src/Command/UpdateRecipesCommand.php +++ b/src/Command/UpdateRecipesCommand.php @@ -262,7 +262,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - private function getRecipe(PackageInterface $package, string $recipeRef = null, string $recipeVersion = null): ?Recipe + private function getRecipe(PackageInterface $package, ?string $recipeRef = null, ?string $recipeVersion = null): ?Recipe { $operation = new InformationOperation($package); if (null !== $recipeRef) { diff --git a/src/Configurator/EnvConfigurator.php b/src/Configurator/EnvConfigurator.php index 9fd81bd30..97c62d6f0 100644 --- a/src/Configurator/EnvConfigurator.php +++ b/src/Configurator/EnvConfigurator.php @@ -177,7 +177,7 @@ private function unconfigurePhpUnit(Recipe $recipe, $vars) * If $originalValue is passed, and the value contains an expression. * the $originalValue is used. */ - private function evaluateValue($value, string $originalValue = null) + private function evaluateValue($value, ?string $originalValue = null) { if ('%generate(secret)%' === $value) { if (null !== $originalValue) { diff --git a/src/Downloader.php b/src/Downloader.php index d03f6b8b4..fe8aa90e7 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -101,7 +101,7 @@ public function getSessionId(): string return $this->sess; } - public function setFlexId(string $id = null) + public function setFlexId(?string $id = null) { // No-op to support downgrading to v1.12.x } diff --git a/src/Flex.php b/src/Flex.php index a305d386f..a45ce90df 100644 --- a/src/Flex.php +++ b/src/Flex.php @@ -584,7 +584,7 @@ function ($value) { $this->finish($rootDir, $originalComposerJsonHash); } - public function finish(string $rootDir, string $originalComposerJsonHash = null): void + public function finish(string $rootDir, ?string $originalComposerJsonHash = null): void { $this->synchronizePackageJson($rootDir); $this->lock->write(); @@ -934,7 +934,7 @@ private function formatOrigin(Recipe $recipe): string return sprintf('%s (>=%s): From %s', $matches[1], $matches[2], 'auto-generated recipe' === $matches[3] ? ''.$matches[3].'' : $matches[3]); } - private function shouldRecordOperation(OperationInterface $operation, bool $isDevMode, Composer $composer = null): bool + private function shouldRecordOperation(OperationInterface $operation, bool $isDevMode, ?Composer $composer = null): bool { if ($this->dryRun || $this->reinstall) { return false; diff --git a/src/Options.php b/src/Options.php index f0872b510..3e41f2296 100644 --- a/src/Options.php +++ b/src/Options.php @@ -23,7 +23,7 @@ class Options private $writtenFiles = []; private $io; - public function __construct(array $options = [], IOInterface $io = null) + public function __construct(array $options = [], ?IOInterface $io = null) { $this->options = $options; $this->io = $io; diff --git a/src/ParallelDownloader.php b/src/ParallelDownloader.php index 5452df6c3..bfa95d305 100644 --- a/src/ParallelDownloader.php +++ b/src/ParallelDownloader.php @@ -216,7 +216,7 @@ public function callbackGet($notificationCode, $severity, $message, $messageCode /** * {@inheritdoc} */ - protected function getRemoteContents($originUrl, $fileUrl, $context, array &$responseHeaders = null, $maxFileSize = null) + protected function getRemoteContents($originUrl, $fileUrl, $context, ?array &$responseHeaders = null, $maxFileSize = null) { if (isset(self::$cache[$fileUrl])) { self::$cacheNext = false; diff --git a/src/ScriptExecutor.php b/src/ScriptExecutor.php index d61694735..9aa3327f5 100644 --- a/src/ScriptExecutor.php +++ b/src/ScriptExecutor.php @@ -31,7 +31,7 @@ class ScriptExecutor private $options; private $executor; - public function __construct(Composer $composer, IOInterface $io, Options $options, ProcessExecutor $executor = null) + public function __construct(Composer $composer, IOInterface $io, Options $options, ?ProcessExecutor $executor = null) { $this->composer = $composer; $this->io = $io; diff --git a/src/TruncatedComposerRepository.php b/src/TruncatedComposerRepository.php index 82485aeaf..ddf77eb87 100644 --- a/src/TruncatedComposerRepository.php +++ b/src/TruncatedComposerRepository.php @@ -23,7 +23,7 @@ */ class TruncatedComposerRepository extends BaseComposerRepository { - public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null) + public function __construct(array $repoConfig, IOInterface $io, Config $config, ?EventDispatcher $eventDispatcher = null, ?RemoteFilesystem $rfs = null) { parent::__construct($repoConfig, $io, $config, $eventDispatcher, $rfs); diff --git a/src/Unpacker.php b/src/Unpacker.php index 97482929a..ddc0cb189 100644 --- a/src/Unpacker.php +++ b/src/Unpacker.php @@ -42,7 +42,7 @@ public function __construct(Composer $composer, PackageResolver $resolver, bool $this->versionParser = new VersionParser(); } - public function unpack(Operation $op, Result $result = null, &$links = [], bool $devRequire = false): Result + public function unpack(Operation $op, ?Result $result = null, &$links = [], bool $devRequire = false): Result { if (null === $result) { $result = new Result(); From 20810768a00d5c4e44859876140cd2ca24ff4fcf Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 16 Apr 2024 10:54:33 +0200 Subject: [PATCH 2/2] More explicit nullable types --- .php-cs-fixer.dist.php | 1 + src/Downloader.php | 2 +- tests/Configurator/AddLinesConfiguratorTest.php | 4 ++-- tests/FlexTest.php | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index e1a10d362..a965a7b45 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -12,6 +12,7 @@ 'array_syntax' => ['syntax' => 'short'], 'ordered_imports' => true, 'php_unit_no_expectation_annotation' => false, // part of `PHPUnitXYMigration:risky` ruleset, to be enabled when PHPUnit 4.x support will be dropped, as we don't want to rewrite exceptions handling twice + 'nullable_type_declaration_for_default_null_value' => true, 'protected_to_private' => false, )) ->setRiskyAllowed(true) diff --git a/src/Downloader.php b/src/Downloader.php index fe8aa90e7..2cdf74682 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -52,7 +52,7 @@ class Downloader private $enabled = true; private $composer; - public function __construct(Composer $composer, IoInterface $io, $rfs) + public function __construct(Composer $composer, IOInterface $io, $rfs) { if (getenv('SYMFONY_CAFILE')) { $this->caFile = getenv('SYMFONY_CAFILE'); diff --git a/tests/Configurator/AddLinesConfiguratorTest.php b/tests/Configurator/AddLinesConfiguratorTest.php index 07f4aefa9..43306947e 100644 --- a/tests/Configurator/AddLinesConfiguratorTest.php +++ b/tests/Configurator/AddLinesConfiguratorTest.php @@ -568,7 +568,7 @@ public function getUpdateTests() ]; } - private function runConfigure(array $config, Composer $composer = null) + private function runConfigure(array $config, ?Composer $composer = null) { $configurator = $this->createConfigurator($composer); @@ -586,7 +586,7 @@ private function runUnconfigure(array $config) $configurator->unconfigure($recipe, $config, $lock); } - private function createConfigurator(Composer $composer = null) + private function createConfigurator(?Composer $composer = null) { return new AddLinesConfigurator( $composer ?: $this->getMockBuilder(Composer::class)->getMock(), diff --git a/tests/FlexTest.php b/tests/FlexTest.php index ab8fe4af4..070454609 100644 --- a/tests/FlexTest.php +++ b/tests/FlexTest.php @@ -375,7 +375,7 @@ private function mockPackageEvent(Package $package): PackageEvent return $event; } - private function mockConfigurator(Recipe $recipe = null): Configurator + private function mockConfigurator(?Recipe $recipe = null): Configurator { $configurator = $this->getMockBuilder(Configurator::class)->disableOriginalConstructor()->getMock(); @@ -406,7 +406,7 @@ private function mockLocker(array $lockData = []): Locker return $locker; } - private function mockComposer(Locker $locker, RootPackageInterface $package, Config $config = null): Composer + private function mockComposer(Locker $locker, RootPackageInterface $package, ?Config $config = null): Composer { if (null === $config) { $config = $this->getMockBuilder(Config::class)->getMock(); @@ -474,7 +474,7 @@ private function mockFlexCustom(BufferIO $io, Composer $composer, Configurator $ }, null, Flex::class)->__invoke(); } - private function mockFlex(BufferIO $io, RootPackageInterface $package, Recipe $recipe = null, array $recipes = [], array $lockerData = []): Flex + private function mockFlex(BufferIO $io, RootPackageInterface $package, ?Recipe $recipe = null, array $recipes = [], array $lockerData = []): Flex { $composer = $this->mockComposer($this->mockLocker($lockerData), $package);