Skip to content

Commit

Permalink
remove UnionTypeHelper usages
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 21, 2022
1 parent 085713b commit 62f2afa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 70 deletions.
29 changes: 25 additions & 4 deletions src/Type/IntersectionType.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
42 changes: 38 additions & 4 deletions src/Type/UnionType.php
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
62 changes: 0 additions & 62 deletions src/Type/UnionTypeHelper.php
Expand Up @@ -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;
Expand All @@ -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<ArrayType>
*/
public static function getArrays(array $types): array
{
$arrays = [];
foreach ($types as $type) {
$arrays[] = $type->getArrays();
}

return array_merge(...$arrays);
}

/**
* @param Type[] $types
* @return list<ConstantArrayType>
*/
public static function getConstantArrays(array $types): array
{
$constantArrays = [];
foreach ($types as $type) {
$constantArrays[] = $type->getConstantArrays();
}

return array_merge(...$constantArrays);
}

/**
* @param Type[] $types
* @return list<ConstantStringType>
*/
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[]
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/bug-8568.php
Expand Up @@ -15,4 +15,12 @@ public function get(): ?int
{
return rand() ? 5 : null;
}

/**
* @param numeric-string $numericS
*/
public function intersections($numericS): void {
assertType('non-falsy-string', 'a'. $numericS);
assertType('numeric-string', (string) $numericS);
}
}

0 comments on commit 62f2afa

Please sign in to comment.