From d1958cddb4f7ce3f5a961c9b6f3d614747805058 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 21 Dec 2022 14:43:19 +0100 Subject: [PATCH] remove UnionTypeHelper usages --- src/Type/IntersectionType.php | 29 +++++++++++++--- src/Type/UnionType.php | 42 +++++++++++++++++++++--- src/Type/UnionTypeHelper.php | 62 ----------------------------------- 3 files changed, 63 insertions(+), 70 deletions(-) diff --git a/src/Type/IntersectionType.php b/src/Type/IntersectionType.php index cfd5f3a03c3..52826a1fbd3 100644 --- a/src/Type/IntersectionType.php +++ b/src/Type/IntersectionType.php @@ -27,6 +27,7 @@ use PHPStan\Type\Traits\NonGeneralizableTypeTrait; use PHPStan\Type\Traits\NonRemoveableTypeTrait; use function array_map; +use function array_merge; use function count; use function implode; use function in_array; @@ -98,22 +99,42 @@ public function inferTemplateTypesOn(Type $templateType): TemplateTypeMap */ public function getReferencedClasses(): array { - return UnionTypeHelper::getReferencedClasses($this->types); + $classes = []; + foreach ($this->types as $type) { + $classes[] = $type->getReferencedClasses(); + } + + return array_merge(...$classes); } public function getArrays(): array { - return UnionTypeHelper::getArrays($this->getTypes()); + $arrays = []; + foreach ($this->types as $type) { + $arrays[] = $type->getArrays(); + } + + return array_merge(...$arrays); } public function getConstantArrays(): array { - return UnionTypeHelper::getConstantArrays($this->getTypes()); + $constantArrays = []; + foreach ($this->types as $type) { + $constantArrays[] = $type->getConstantArrays(); + } + + return array_merge(...$constantArrays); } public function getConstantStrings(): array { - return UnionTypeHelper::getConstantStrings($this->getTypes()); + $strings = []; + foreach ($this->types as $type) { + $strings[] = $type->getConstantStrings(); + } + + return array_merge(...$strings); } public function accepts(Type $otherType, bool $strictTypes): TrinaryLogic diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index fdf585d0f7c..1f54a943710 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -16,6 +16,7 @@ use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; +use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\Generic\GenericClassStringType; @@ -26,6 +27,7 @@ use PHPStan\Type\Generic\TemplateUnionType; use PHPStan\Type\Traits\NonGeneralizableTypeTrait; use function array_map; +use function array_merge; use function count; use function implode; use function sprintf; @@ -121,22 +123,54 @@ private function getSortedTypes(): array */ public function getReferencedClasses(): array { - return UnionTypeHelper::getReferencedClasses($this->getTypes()); + $classes = []; + foreach ($this->types as $type) { + $classes[] = $type->getReferencedClasses(); + } + + return array_merge(...$classes); } public function getArrays(): array { - return UnionTypeHelper::getArrays($this->getTypes()); + $arrays = []; + foreach ($this->types as $type) { + $arrays[] = $type->getArrays(); + } + + if ($arrays === []) { + return []; + } + + return array_merge(...$arrays); } public function getConstantArrays(): array { - return UnionTypeHelper::getConstantArrays($this->getTypes()); + $constantArrays = []; + foreach ($this->getTypesOfClass(ConstantArrayType::class) as $type) { + $constantArrays[] = $type->getConstantArrays(); + } + + if ($constantArrays === []) { + return []; + } + + return array_merge(...$constantArrays); } public function getConstantStrings(): array { - return UnionTypeHelper::getConstantStrings($this->getTypesOfClass(ConstantStringType::class)); + $strings = []; + foreach ($this->getTypesOfClass(ConstantStringType::class) as $type) { + $strings[] = $type->getConstantStrings(); + } + + if ($strings === []) { + return []; + } + + return array_merge(...$strings); } public function accepts(Type $type, bool $strictTypes): TrinaryLogic diff --git a/src/Type/UnionTypeHelper.php b/src/Type/UnionTypeHelper.php index e0fed341f1c..db153a6a01b 100644 --- a/src/Type/UnionTypeHelper.php +++ b/src/Type/UnionTypeHelper.php @@ -3,12 +3,10 @@ namespace PHPStan\Type; use PHPStan\Type\Accessory\AccessoryType; -use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\Constant\ConstantFloatType; use PHPStan\Type\Constant\ConstantIntegerType; use PHPStan\Type\Constant\ConstantStringType; -use function array_merge; use function count; use function strcasecmp; use function usort; @@ -17,66 +15,6 @@ class UnionTypeHelper { - /** - * @param Type[] $types - * @return string[] - */ - public static function getReferencedClasses(array $types): array - { - $referencedClasses = []; - foreach ($types as $type) { - $referencedClasses[] = $type->getReferencedClasses(); - } - - return array_merge(...$referencedClasses); - } - - /** - * @param Type[] $types - * @return list - */ - public static function getArrays(array $types): array - { - $arrays = []; - foreach ($types as $type) { - $arrays[] = $type->getArrays(); - } - - return array_merge(...$arrays); - } - - /** - * @param Type[] $types - * @return list - */ - public static function getConstantArrays(array $types): array - { - $constantArrays = []; - foreach ($types as $type) { - $constantArrays[] = $type->getConstantArrays(); - } - - return array_merge(...$constantArrays); - } - - /** - * @param Type[] $types - * @return list - */ - public static function getConstantStrings(array $types): array - { - $strings = []; - foreach ($types as $type) { - $strings[] = $type->getConstantStrings(); - } - - if ($strings === []) { - return []; - } - - return array_merge(...$strings); - } - /** * @param Type[] $types * @return Type[]