From b7441d66b0e3a3f810de21b780744799171f4ca1 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 13 Dec 2022 10:50:01 +0100 Subject: [PATCH] Revert "Get rid of instanceof ArrayType" This reverts commit 5137bc86b5af039bd634ea091893d78a14dd2bc1. --- src/Reflection/InitializerExprTypeResolver.php | 6 +++--- src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php | 7 ++++--- src/Type/TypehintHelper.php | 16 ++++++++-------- .../Analyser/LegacyNodeScopeResolverTest.php | 4 ++-- .../Rules/Methods/CallMethodsRuleTest.php | 4 ++-- .../PHPStan/Rules/Methods/ReturnTypeRuleTest.php | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index 2b3332e32b..b3d49df16e 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -1475,9 +1475,9 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri $types = TypeCombinator::union($leftType, $rightType); if ( - $leftType->isArray()->yes() - || $rightType->isArray()->yes() - || $types->isArray()->yes() + $leftType instanceof ArrayType + || $rightType instanceof ArrayType + || $types instanceof ArrayType ) { return new ErrorType(); } diff --git a/src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php b/src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php index ebfdba824f..e9f413a9f2 100644 --- a/src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php +++ b/src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php @@ -12,6 +12,7 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; +use PHPStan\Type\ArrayType; use PHPStan\Type\FileTypeMapper; use PHPStan\Type\Generic\TemplateType; use PHPStan\Type\Type; @@ -94,10 +95,10 @@ public function processNode(Node $node, Scope $scope): array if ( $phpDocParamTag instanceof ParamTag && $phpDocParamTag->isVariadic() - && $phpDocParamType->isArray()->yes() - && $nativeParamType->isArray()->no() + && $phpDocParamType instanceof ArrayType + && !$nativeParamType instanceof ArrayType ) { - $phpDocParamType = $phpDocParamType->getIterableValueType(); + $phpDocParamType = $phpDocParamType->getItemType(); } $isParamSuperType = $nativeParamType->isSuperTypeOf($phpDocParamType); diff --git a/src/Type/TypehintHelper.php b/src/Type/TypehintHelper.php index c3b2370d7f..1b2229eb90 100644 --- a/src/Type/TypehintHelper.php +++ b/src/Type/TypehintHelper.php @@ -83,8 +83,8 @@ public static function decideTypeFromReflection( ): Type { if ($reflectionType === null) { - if ($isVariadic && $phpDocType !== null && $phpDocType->isArray()->yes()) { - $phpDocType = $phpDocType->getIterableValueType(); + if ($isVariadic && $phpDocType instanceof ArrayType) { + $phpDocType = $phpDocType->getItemType(); } return $phpDocType ?? new MixedType(); } @@ -164,20 +164,20 @@ public static function decideType( if ($phpDocType instanceof UnionType) { $innerTypes = []; foreach ($phpDocType->getTypes() as $innerType) { - if ($innerType->isArray()->yes()) { + if ($innerType instanceof ArrayType) { $innerTypes[] = new IterableType( - $innerType->getIterableKeyType(), - $innerType->getIterableValueType(), + $innerType->getKeyType(), + $innerType->getItemType(), ); } else { $innerTypes[] = $innerType; } } $phpDocType = new UnionType($innerTypes); - } elseif ($phpDocType->isArray()->yes()) { + } elseif ($phpDocType instanceof ArrayType) { $phpDocType = new IterableType( - $phpDocType->getIterableKeyType(), - $phpDocType->getIterableValueType(), + $phpDocType->getKeyType(), + $phpDocType->getItemType(), ); } } diff --git a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php index f5b1cc8285..5c32c80d7d 100644 --- a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php @@ -5898,7 +5898,7 @@ public function dataIterable(): array '$mixed', ], [ - 'iterable<(int|string), Iterables\Bar>', + 'iterable', '$iterableWithConcreteTypehint', ], [ @@ -5914,7 +5914,7 @@ public function dataIterable(): array '$this->doBar()', ], [ - 'iterable<(int|string), Iterables\Baz>', + 'iterable', '$this->doBaz()', ], [ diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 6ead94c47d..826e1df3c6 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -1285,7 +1285,7 @@ public function testIterables(bool $checkNullables): void $this->checkUnionTypes = true; $this->analyse([__DIR__ . '/data/call-methods-iterable.php'], [ [ - 'Parameter #1 $ids of method CallMethodsIterables\Uuid::bar() expects iterable<(int|string), CallMethodsIterables\Uuid>, array given.', + 'Parameter #1 $ids of method CallMethodsIterables\Uuid::bar() expects iterable, array given.', 14, ], [ @@ -1305,7 +1305,7 @@ public function testIterables(bool $checkNullables): void 62, ], [ - 'Parameter #3 $iterableWithConcreteTypehint of method CallMethodsIterables\Foo::doFoo() expects iterable<(int|string), CallMethodsIterables\Bar>, int given.', + 'Parameter #3 $iterableWithConcreteTypehint of method CallMethodsIterables\Foo::doFoo() expects iterable, int given.', 62, ], [ diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index 2e0da14472..1d57172152 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -324,7 +324,7 @@ public function testReturnIterable(): void { $this->analyse([__DIR__ . '/data/returnTypes-iterable.php'], [ [ - 'Method ReturnTypesIterable\Foo::stringIterable() should return iterable<(int|string), string> but returns array.', + 'Method ReturnTypesIterable\Foo::stringIterable() should return iterable but returns array.', 27, ], [