Skip to content

Commit

Permalink
Revert "Get rid of instanceof ArrayType"
Browse files Browse the repository at this point in the history
This reverts commit 5137bc8.
  • Loading branch information
ondrejmirtes committed Dec 13, 2022
1 parent c760b1f commit b7441d6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -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();
}
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
16 changes: 8 additions & 8 deletions src/Type/TypehintHelper.php
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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(),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Expand Up @@ -5898,7 +5898,7 @@ public function dataIterable(): array
'$mixed',
],
[
'iterable<(int|string), Iterables\Bar>',
'iterable<Iterables\Bar>',
'$iterableWithConcreteTypehint',
],
[
Expand All @@ -5914,7 +5914,7 @@ public function dataIterable(): array
'$this->doBar()',
],
[
'iterable<(int|string), Iterables\Baz>',
'iterable<Iterables\Baz>',
'$this->doBaz()',
],
[
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Expand Up @@ -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<int, null> given.',
'Parameter #1 $ids of method CallMethodsIterables\Uuid::bar() expects iterable<CallMethodsIterables\Uuid>, array<int, null> given.',
14,
],
[
Expand All @@ -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<CallMethodsIterables\Bar>, int given.',
62,
],
[
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Expand Up @@ -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<int, int>.',
'Method ReturnTypesIterable\Foo::stringIterable() should return iterable<string> but returns array<int, int>.',
27,
],
[
Expand Down

0 comments on commit b7441d6

Please sign in to comment.