From 843ebbb1d889ac4d9a152a52f1f1ee9690d2e721 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 2 Apr 2022 20:58:03 +0200 Subject: [PATCH] Fix of weird bug --- src/Type/IntersectionType.php | 5 ++-- src/Type/UnionType.php | 2 +- .../Rules/Methods/data/call-methods.php | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Type/IntersectionType.php b/src/Type/IntersectionType.php index b79a6d6c17..4fcee89956 100644 --- a/src/Type/IntersectionType.php +++ b/src/Type/IntersectionType.php @@ -22,6 +22,7 @@ use PHPStan\Type\Generic\TemplateType; use PHPStan\Type\Generic\TemplateTypeMap; use PHPStan\Type\Generic\TemplateTypeVariance; +use PHPStan\Type\Generic\TemplateUnionType; use PHPStan\Type\Traits\NonGeneralizableTypeTrait; use PHPStan\Type\Traits\NonRemoveableTypeTrait; use function array_map; @@ -131,8 +132,8 @@ public function isSubTypeOf(Type $otherType): TrinaryLogic public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic { - if ($acceptingType instanceof self || $acceptingType instanceof UnionType) { - return $acceptingType->isSuperTypeOf($this); + if ($acceptingType instanceof self || ($acceptingType instanceof UnionType && !$acceptingType instanceof TemplateUnionType)) { + return $acceptingType->accepts($this, $strictTypes); } $results = []; diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index 225e0b40da..3de5d2b1b9 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -95,7 +95,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic return TrinaryLogic::createYes(); } - if ($type instanceof CompoundType && !$type instanceof CallableType && !$type instanceof TemplateType) { + if ($type instanceof CompoundType && !$type instanceof CallableType && !$type instanceof TemplateType && !$type instanceof IntersectionType) { return $type->isAcceptedBy($this, $strictTypes); } diff --git a/tests/PHPStan/Rules/Methods/data/call-methods.php b/tests/PHPStan/Rules/Methods/data/call-methods.php index 081b913ea1..8c733ac96e 100644 --- a/tests/PHPStan/Rules/Methods/data/call-methods.php +++ b/tests/PHPStan/Rules/Methods/data/call-methods.php @@ -1802,3 +1802,30 @@ public function test(): void $this->foo('Newark Liberty International'); } } + +class WeirdArrayBug +{ + + /** @param string[] $strings */ + public function doBar($m, array $a, bool $b, array $strings) + { + $needles = [$m]; + foreach ($a as $v) { + if ($b) { + $needles = array_merge($needles, $strings); + } + } + + $this->doFoo($needles); + } + + /** + * @param array|string $strings + * @return void + */ + public function doFoo($strings) + { + + } + +}