Skip to content

Commit c734845

Browse files
committed
remove accessory array type, to allow array type fill
1 parent 3db92ed commit c734845

4 files changed

Lines changed: 50 additions & 89 deletions

File tree

rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/FixtureIntersection/cover_intersection.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/FixtureIntersection/cover_intersection_with_docblock.php.inc

Lines changed: 0 additions & 53 deletions
This file was deleted.

rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
use PHPStan\Type\ObjectType;
2222
use PHPStan\Type\ThisType;
2323
use PHPStan\Type\Type;
24-
use PHPStan\Type\TypeCombinator;
25-
use PHPStan\Type\TypeUtils;
2624
use PHPStan\Type\UnionType;
2725
use Rector\NodeTypeResolver\NodeTypeResolver;
2826
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
@@ -94,15 +92,6 @@ private function resolveStrictArgValueType(Arg $arg): Type
9492
{
9593
$argValueType = $this->nodeTypeResolver->getNativeType($arg->value);
9694

97-
if ($argValueType instanceof IntersectionType) {
98-
dump($argValueType);
99-
100-
$argValueType = TypeCombinator::remove($argValueType, new AccessoryArrayListType());
101-
102-
dump($argValueType);
103-
die;
104-
}
105-
10695
return $this->normalizeType($argValueType);
10796
}
10897

@@ -245,4 +234,26 @@ private function isArrayMixedMixedType(Type $type): bool
245234

246235
return $type->getKeyType() instanceof MixedType || $type->getKeyType() instanceof NeverType;
247236
}
237+
238+
private function cleanArrayIntersectionType(Type $type): Type
239+
{
240+
if (! $type instanceof IntersectionType) {
241+
return $type;
242+
}
243+
244+
$cleanTypes = [];
245+
foreach ($type->getTypes() as $intersectionType) {
246+
if ($intersectionType instanceof AccessoryArrayListType) {
247+
continue;
248+
}
249+
250+
$cleanTypes[] = $intersectionType;
251+
}
252+
253+
if (count($cleanTypes) === 1) {
254+
return $cleanTypes[0];
255+
}
256+
257+
return new IntersectionType($cleanTypes);
258+
}
248259
}

src/NodeTypeResolver/NodeTypeResolver.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
use PHPStan\Reflection\ClassReflection;
3030
use PHPStan\Reflection\Native\NativeFunctionReflection;
3131
use PHPStan\Reflection\ReflectionProvider;
32+
use PHPStan\Type\Accessory\AccessoryArrayListType;
3233
use PHPStan\Type\ArrayType;
3334
use PHPStan\Type\Constant\ConstantArrayType;
3435
use PHPStan\Type\Constant\ConstantBooleanType;
3536
use PHPStan\Type\Constant\ConstantStringType;
3637
use PHPStan\Type\ErrorType;
38+
use PHPStan\Type\IntersectionType;
3739
use PHPStan\Type\MixedType;
3840
use PHPStan\Type\NeverType;
3941
use PHPStan\Type\NullType;
@@ -279,6 +281,10 @@ public function getNativeType(Expr $expr): Type
279281
$type = $this->resolveArrayDimFetchType($expr, $scope, $type);
280282
}
281283

284+
if ($type instanceof IntersectionType) {
285+
$type = $this->cleanArrayIntersectionType($type);
286+
}
287+
282288
if (! $type instanceof UnionType) {
283289
if ($this->isAnonymousObjectType($type)) {
284290
return new ObjectWithoutClassType();
@@ -675,4 +681,26 @@ private function isSubstrOnPHP74(FuncCall $funcCall): bool
675681

676682
return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersion::PHP_80);
677683
}
684+
685+
private function cleanArrayIntersectionType(Type $type): Type
686+
{
687+
if (! $type instanceof IntersectionType) {
688+
return $type;
689+
}
690+
691+
$cleanTypes = [];
692+
foreach ($type->getTypes() as $intersectionType) {
693+
if ($intersectionType instanceof AccessoryArrayListType) {
694+
continue;
695+
}
696+
697+
$cleanTypes[] = $intersectionType;
698+
}
699+
700+
if (count($cleanTypes) === 1) {
701+
return $cleanTypes[0];
702+
}
703+
704+
return new IntersectionType($cleanTypes);
705+
}
678706
}

0 commit comments

Comments
 (0)