Skip to content

Commit

Permalink
[PHPStanStaticTypeMapper] Add UnionTypeAnalyzer::isNullable() (#987)
Browse files Browse the repository at this point in the history
* [PHPStanStaticTypeMapper] Add UnionTypeAnalyzer isNullable

* check 2 types

* [ci-review] Rector Rectify

* use in StaticTypeAnalyzer

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 12, 2021
1 parent 5f915d8 commit 90c1d09
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 47 deletions.
23 changes: 7 additions & 16 deletions packages/NodeTypeResolver/PHPStan/Type/StaticTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;

final class StaticTypeAnalyzer
{
public function __construct(
private UnionTypeAnalyzer $unionTypeAnalyzer
) {
}

public function isAlwaysTruableType(Type $type): bool
{
if ($type instanceof MixedType) {
Expand All @@ -33,7 +39,7 @@ public function isAlwaysTruableType(Type $type): bool
return $this->isAlwaysTruableArrayType($type);
}

if ($this->isNullable($type)) {
if ($type instanceof UnionType && $this->unionTypeAnalyzer->isNullable($type)) {
return false;
}

Expand All @@ -53,21 +59,6 @@ public function isAlwaysTruableType(Type $type): bool
return $this->isAlwaysTruableUnionType($type);
}

private function isNullable(Type $type): bool
{
if (! $type instanceof UnionType) {
return false;
}

foreach ($type->getTypes() as $unionedType) {
if ($unionedType instanceof NullType) {
return true;
}
}

return false;
}

private function isScalarType(Type $type): bool
{
if ($type instanceof NullType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,21 @@ public function isScalar(UnionType $unionType): bool

return true;
}

public function isNullable(UnionType $unionType, bool $checkTwoTypes = false): bool
{
$types = $unionType->getTypes();

if ($checkTwoTypes && count($types) > 2) {
return false;
}

foreach ($types as $type) {
if ($type instanceof NullType) {
return true;
}
}

return false;
}
}
13 changes: 1 addition & 12 deletions packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,11 @@ private function matchPhpParserUnionType(UnionType $unionType, TypeKind $typeKin
return new PhpParserUnionType($phpParserUnionedTypes);
}

private function isNullable(UnionType $unionType): bool
{
foreach ($unionType->getTypes() as $type) {
if ($type instanceof NullType) {
return true;
}
}

return false;
}

private function resolveCompatibleObjectCandidate(UnionType $unionType): UnionType|TypeWithClassName|null
{
if ($this->doctrineTypeAnalyzer->isDoctrineCollectionWithIterableUnionType($unionType)) {
$objectType = new ObjectType('Doctrine\Common\Collections\Collection');
return $this->isNullable($unionType)
return $this->unionTypeAnalyzer->isNullable($unionType)
? new UnionType([new NullType(), $objectType])
: $objectType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\TypeDeclaration\NodeTypeAnalyzer\DetailedTypeAnalyzer;

final class GenericClassStringTypeNormalizer
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private DetailedTypeAnalyzer $detailedTypeAnalyzer
private DetailedTypeAnalyzer $detailedTypeAnalyzer,
private UnionTypeAnalyzer $unionTypeAnalyzer
) {
}

Expand All @@ -49,7 +50,7 @@ public function normalize(Type $type): ArrayType | UnionType | Type
return $this->resolveStringType($value);
});

if ($type instanceof UnionType && ! $this->isNullableType($type)) {
if ($type instanceof UnionType && ! $this->unionTypeAnalyzer->isNullable($type, true)) {
return $this->resolveClassStringInUnionType($type);
}

Expand All @@ -73,22 +74,6 @@ public function isAllGenericClassStringType(UnionType $unionType): bool
return true;
}

private function isNullableType(UnionType $unionType): bool
{
$types = $unionType->getTypes();
if (count($types) > 2) {
return false;
}

foreach ($types as $type) {
if ($type instanceof NullType) {
return true;
}
}

return false;
}

private function resolveArrayTypeWithUnionKeyType(ArrayType $arrayType): ArrayType
{
$itemType = $arrayType->getItemType();
Expand Down

0 comments on commit 90c1d09

Please sign in to comment.