From 288be71bae2ebc88676f5d3a03d23f70b278fcc1 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 25 Nov 2023 17:57:46 +0100 Subject: [PATCH 1/3] Remove obsolete PHP version checks --- Extractor/ReflectionExtractor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Extractor/ReflectionExtractor.php b/Extractor/ReflectionExtractor.php index ff86ef8..e6069e0 100644 --- a/Extractor/ReflectionExtractor.php +++ b/Extractor/ReflectionExtractor.php @@ -364,7 +364,7 @@ public function getWriteInfo(string $class, string $property, array $context = [ if ($reflClass->hasProperty($property) && ($reflClass->getProperty($property)->getModifiers() & $this->propertyReflectionFlags)) { $reflProperty = $reflClass->getProperty($property); - if (\PHP_VERSION_ID < 80100 || !$reflProperty->isReadOnly()) { + if (!$reflProperty->isReadOnly()) { return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, $this->getWriteVisiblityForProperty($reflProperty), $reflProperty->isStatic()); } @@ -578,7 +578,7 @@ private function isAllowedProperty(string $class, string $property, bool $writeA try { $reflectionProperty = new \ReflectionProperty($class, $property); - if (\PHP_VERSION_ID >= 80100 && $writeAccessRequired && $reflectionProperty->isReadOnly()) { + if ($writeAccessRequired && $reflectionProperty->isReadOnly()) { return false; } From eaf3b4fc5bfbec6dbc4689eafa59bebe5eea9a0f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 23 Jan 2024 14:51:25 +0100 Subject: [PATCH 2/3] Apply php-cs-fixer fix --rules nullable_type_declaration_for_default_null_value --- Extractor/PhpDocExtractor.php | 2 +- Extractor/PhpStanExtractor.php | 2 +- Extractor/ReflectionExtractor.php | 2 +- PhpStan/NameScopeFactory.php | 2 +- PropertyWriteInfo.php | 2 +- Tests/Extractor/PhpDocExtractorTest.php | 10 +++++----- Tests/Extractor/PhpStanExtractorTest.php | 10 +++++----- Tests/Extractor/ReflectionExtractorTest.php | 16 ++++++++-------- Type.php | 2 +- Util/PhpDocTypeHelper.php | 2 +- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Extractor/PhpDocExtractor.php b/Extractor/PhpDocExtractor.php index 2cecfcf..ff23bb4 100644 --- a/Extractor/PhpDocExtractor.php +++ b/Extractor/PhpDocExtractor.php @@ -57,7 +57,7 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property * @param string[]|null $accessorPrefixes * @param string[]|null $arrayMutatorPrefixes */ - public function __construct(DocBlockFactoryInterface $docBlockFactory = null, array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null) + public function __construct(?DocBlockFactoryInterface $docBlockFactory = null, ?array $mutatorPrefixes = null, ?array $accessorPrefixes = null, ?array $arrayMutatorPrefixes = null) { if (!class_exists(DocBlockFactory::class)) { throw new \LogicException(sprintf('Unable to use the "%s" class as the "phpdocumentor/reflection-docblock" package is not installed. Try running composer require "phpdocumentor/reflection-docblock".', __CLASS__)); diff --git a/Extractor/PhpStanExtractor.php b/Extractor/PhpStanExtractor.php index a964b50..2f16969 100644 --- a/Extractor/PhpStanExtractor.php +++ b/Extractor/PhpStanExtractor.php @@ -58,7 +58,7 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc * @param list|null $accessorPrefixes * @param list|null $arrayMutatorPrefixes */ - public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null) + public function __construct(?array $mutatorPrefixes = null, ?array $accessorPrefixes = null, ?array $arrayMutatorPrefixes = null) { if (!class_exists(ContextFactory::class)) { throw new \LogicException(sprintf('Unable to use the "%s" class as the "phpdocumentor/type-resolver" package is not installed. Try running composer require "phpdocumentor/type-resolver".', __CLASS__)); diff --git a/Extractor/ReflectionExtractor.php b/Extractor/ReflectionExtractor.php index 4da5912..29a91f9 100644 --- a/Extractor/ReflectionExtractor.php +++ b/Extractor/ReflectionExtractor.php @@ -83,7 +83,7 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp * @param string[]|null $accessorPrefixes * @param string[]|null $arrayMutatorPrefixes */ - public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null, bool $enableConstructorExtraction = true, int $accessFlags = self::ALLOW_PUBLIC, InflectorInterface $inflector = null, int $magicMethodsFlags = self::ALLOW_MAGIC_GET | self::ALLOW_MAGIC_SET) + public function __construct(?array $mutatorPrefixes = null, ?array $accessorPrefixes = null, ?array $arrayMutatorPrefixes = null, bool $enableConstructorExtraction = true, int $accessFlags = self::ALLOW_PUBLIC, ?InflectorInterface $inflector = null, int $magicMethodsFlags = self::ALLOW_MAGIC_GET | self::ALLOW_MAGIC_SET) { $this->mutatorPrefixes = $mutatorPrefixes ?? self::$defaultMutatorPrefixes; $this->accessorPrefixes = $accessorPrefixes ?? self::$defaultAccessorPrefixes; diff --git a/PhpStan/NameScopeFactory.php b/PhpStan/NameScopeFactory.php index 32f2f33..3ec00e6 100644 --- a/PhpStan/NameScopeFactory.php +++ b/PhpStan/NameScopeFactory.php @@ -20,7 +20,7 @@ */ final class NameScopeFactory { - public function create(string $calledClassName, string $declaringClassName = null): NameScope + public function create(string $calledClassName, ?string $declaringClassName = null): NameScope { $declaringClassName = $declaringClassName ?? $calledClassName; diff --git a/PropertyWriteInfo.php b/PropertyWriteInfo.php index b4e33b2..ce3d8e0 100644 --- a/PropertyWriteInfo.php +++ b/PropertyWriteInfo.php @@ -38,7 +38,7 @@ final class PropertyWriteInfo private $removerInfo; private $errors = []; - public function __construct(string $type = self::TYPE_NONE, string $name = null, string $visibility = null, bool $static = null) + public function __construct(string $type = self::TYPE_NONE, ?string $name = null, ?string $visibility = null, ?bool $static = null) { $this->type = $type; $this->name = $name; diff --git a/Tests/Extractor/PhpDocExtractorTest.php b/Tests/Extractor/PhpDocExtractorTest.php index b3489d9..f737416 100644 --- a/Tests/Extractor/PhpDocExtractorTest.php +++ b/Tests/Extractor/PhpDocExtractorTest.php @@ -41,7 +41,7 @@ protected function setUp(): void /** * @dataProvider typesProvider */ - public function testExtract($property, array $type = null, $shortDescription, $longDescription) + public function testExtract($property, ?array $type = null, $shortDescription, $longDescription) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property)); $this->assertSame($shortDescription, $this->extractor->getShortDescription('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property)); @@ -76,7 +76,7 @@ public function testInvalid($property, $shortDescription, $longDescription) /** * @dataProvider typesWithNoPrefixesProvider */ - public function testExtractTypesWithNoPrefixes($property, array $type = null) + public function testExtractTypesWithNoPrefixes($property, ?array $type = null) { $noPrefixExtractor = new PhpDocExtractor(null, [], [], []); @@ -157,7 +157,7 @@ public static function typesProvider() /** * @dataProvider provideCollectionTypes */ - public function testExtractCollection($property, array $type = null, $shortDescription, $longDescription) + public function testExtractCollection($property, ?array $type = null, $shortDescription, $longDescription) { if (!class_exists(Collection::class)) { $this->markTestSkipped('Collections are not implemented in current phpdocumentor/type-resolver version'); @@ -223,7 +223,7 @@ public static function provideCollectionTypes() /** * @dataProvider typesWithCustomPrefixesProvider */ - public function testExtractTypesWithCustomPrefixes($property, array $type = null) + public function testExtractTypesWithCustomPrefixes($property, ?array $type = null) { $customExtractor = new PhpDocExtractor(null, ['add', 'remove'], ['is', 'can']); @@ -432,7 +432,7 @@ protected static function isPhpDocumentorV5() /** * @dataProvider constructorTypesProvider */ - public function testExtractConstructorTypes($property, array $type = null) + public function testExtractConstructorTypes($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypesFromConstructor('Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummy', $property)); } diff --git a/Tests/Extractor/PhpStanExtractorTest.php b/Tests/Extractor/PhpStanExtractorTest.php index c607f2a..d8fa9b9 100644 --- a/Tests/Extractor/PhpStanExtractorTest.php +++ b/Tests/Extractor/PhpStanExtractorTest.php @@ -49,7 +49,7 @@ protected function setUp(): void /** * @dataProvider typesProvider */ - public function testExtract($property, array $type = null) + public function testExtract($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property)); } @@ -80,7 +80,7 @@ public function testInvalid($property) /** * @dataProvider typesWithNoPrefixesProvider */ - public function testExtractTypesWithNoPrefixes($property, array $type = null) + public function testExtractTypesWithNoPrefixes($property, ?array $type = null) { $noPrefixExtractor = new PhpStanExtractor([], [], []); @@ -135,7 +135,7 @@ public static function typesProvider() /** * @dataProvider provideCollectionTypes */ - public function testExtractCollection($property, array $type = null) + public function testExtractCollection($property, ?array $type = null) { $this->testExtract($property, $type); } @@ -191,7 +191,7 @@ public static function provideCollectionTypes() /** * @dataProvider typesWithCustomPrefixesProvider */ - public function testExtractTypesWithCustomPrefixes($property, array $type = null) + public function testExtractTypesWithCustomPrefixes($property, ?array $type = null) { $customExtractor = new PhpStanExtractor(['add', 'remove'], ['is', 'can']); @@ -349,7 +349,7 @@ public static function propertiesParentTypeProvider(): array /** * @dataProvider constructorTypesProvider */ - public function testExtractConstructorTypes($property, array $type = null) + public function testExtractConstructorTypes($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypesFromConstructor('Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummy', $property)); } diff --git a/Tests/Extractor/ReflectionExtractorTest.php b/Tests/Extractor/ReflectionExtractorTest.php index c8bb275..d3d5751 100644 --- a/Tests/Extractor/ReflectionExtractorTest.php +++ b/Tests/Extractor/ReflectionExtractorTest.php @@ -208,7 +208,7 @@ public function testGetPropertiesWithNoPrefixes() /** * @dataProvider typesProvider */ - public function testExtractors($property, array $type = null) + public function testExtractors($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property, [])); } @@ -235,7 +235,7 @@ public static function typesProvider() /** * @dataProvider php7TypesProvider */ - public function testExtractPhp7Type(string $class, string $property, array $type = null) + public function testExtractPhp7Type(string $class, string $property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes($class, $property, [])); } @@ -256,7 +256,7 @@ public static function php7TypesProvider() /** * @dataProvider php71TypesProvider */ - public function testExtractPhp71Type($property, array $type = null) + public function testExtractPhp71Type($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy', $property, [])); } @@ -277,7 +277,7 @@ public static function php71TypesProvider() * * @requires PHP 8 */ - public function testExtractPhp80Type($property, array $type = null) + public function testExtractPhp80Type($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, [])); } @@ -301,7 +301,7 @@ public static function php80TypesProvider() * * @requires PHP 8.1 */ - public function testExtractPhp81Type($property, array $type = null) + public function testExtractPhp81Type($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy', $property, [])); } @@ -327,7 +327,7 @@ public function testReadonlyPropertiesAreNotWriteable() * * @requires PHP 8.2 */ - public function testExtractPhp82Type($property, array $type = null) + public function testExtractPhp82Type($property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php82Dummy', $property, [])); } @@ -472,7 +472,7 @@ public static function getInitializableProperties(): array /** * @dataProvider constructorTypesProvider */ - public function testExtractTypeConstructor(string $class, string $property, array $type = null) + public function testExtractTypeConstructor(string $class, string $property, ?array $type = null) { /* Check that constructor extractions works by default, and if passed in via context. Check that null is returned if constructor extraction is disabled */ @@ -663,7 +663,7 @@ public function testGetWriteInfoReadonlyProperties() /** * @dataProvider extractConstructorTypesProvider */ - public function testExtractConstructorTypes(string $property, array $type = null) + public function testExtractConstructorTypes(string $property, ?array $type = null) { $this->assertEquals($type, $this->extractor->getTypesFromConstructor('Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummy', $property)); } diff --git a/Type.php b/Type.php index 5c1b5c1..44fa36f 100644 --- a/Type.php +++ b/Type.php @@ -76,7 +76,7 @@ class Type * * @throws \InvalidArgumentException */ - public function __construct(string $builtinType, bool $nullable = false, string $class = null, bool $collection = false, $collectionKeyType = null, $collectionValueType = null) + public function __construct(string $builtinType, bool $nullable = false, ?string $class = null, bool $collection = false, $collectionKeyType = null, $collectionValueType = null) { if (!\in_array($builtinType, self::$builtinTypes)) { throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP type.', $builtinType)); diff --git a/Util/PhpDocTypeHelper.php b/Util/PhpDocTypeHelper.php index 44a4614..6020be0 100644 --- a/Util/PhpDocTypeHelper.php +++ b/Util/PhpDocTypeHelper.php @@ -102,7 +102,7 @@ public function getTypes(DocType $varType): array /** * Creates a {@see Type} from a PHPDoc type. */ - private function createType(DocType $type, bool $nullable, string $docType = null): ?Type + private function createType(DocType $type, bool $nullable, ?string $docType = null): ?Type { $docType = $docType ?? (string) $type; From 6d91c6fbcaa65c14c9fb2cb5fca44f82f2cd3523 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 23 Jan 2024 15:23:03 +0100 Subject: [PATCH 3/3] Fix implicitly-required parameters --- Tests/Extractor/PhpDocExtractorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Extractor/PhpDocExtractorTest.php b/Tests/Extractor/PhpDocExtractorTest.php index f737416..02f81b3 100644 --- a/Tests/Extractor/PhpDocExtractorTest.php +++ b/Tests/Extractor/PhpDocExtractorTest.php @@ -157,7 +157,7 @@ public static function typesProvider() /** * @dataProvider provideCollectionTypes */ - public function testExtractCollection($property, ?array $type = null, $shortDescription, $longDescription) + public function testExtractCollection($property, ?array $type, $shortDescription, $longDescription) { if (!class_exists(Collection::class)) { $this->markTestSkipped('Collections are not implemented in current phpdocumentor/type-resolver version');