Skip to content

Commit

Permalink
Remove TypeNormalizer as type comparison is handled well by PHPStan (#…
Browse files Browse the repository at this point in the history
…6570)

* try running without type narrower

* Remove type normalizer, as no longer needed

* Fix array type using isArray()
  • Loading branch information
TomasVotruba authored Dec 12, 2024
1 parent ff49b20 commit 6fce22d
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class FalseBoolDocblock
/**
* @return array|bool some description
*/
public function run(): array|false
public function run(): false|array
{
if (rand(0, 1)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use stdClass;

final class UnionArray
{
public function run($a, $b): \DateTime|array|null
public function run($a, $b): null|\DateTime|array
{
if ($a) {
return null;
Expand Down
46 changes: 0 additions & 46 deletions rules-tests/TypeDeclaration/TypeNormalizerTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private function processAddArrayReturnType(
ClassMethod|Function_|Closure $node,
Type $returnType
): ClassMethod|Function_|Closure|null {
if (! $returnType instanceof ArrayType && ! $returnType instanceof ConstantArrayType) {
if (! $returnType->isArray()->yes()) {
return null;
}

Expand All @@ -174,10 +174,8 @@ private function shouldSkip(ClassMethod|Function_|Closure $node, Scope $scope):
);
}

private function changeReturnType(
ClassMethod|Function_|Closure $node,
ArrayType|ConstantArrayType $arrayType
): void {
private function changeReturnType(ClassMethod|Function_|Closure $node, Type $arrayType): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

// skip already filled type, on purpose
Expand Down Expand Up @@ -265,7 +263,7 @@ private function matchArrayAssignedVariable(array $stmts): array
return $variables;
}

private function shouldAddReturnArrayDocType(ArrayType|ConstantArrayType $arrayType): bool
private function shouldAddReturnArrayDocType(Type $arrayType): bool
{
if ($arrayType instanceof ConstantArrayType) {
if ($arrayType->getIterableValueType() instanceof NeverType) {
Expand Down
4 changes: 2 additions & 2 deletions rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
final readonly class ReturnTypeInferer
{
public function __construct(
private TypeNormalizer $typeNormalizer,
private ReturnedNodesReturnTypeInfererTypeInferer $returnedNodesReturnTypeInfererTypeInferer
) {
}
Expand All @@ -30,6 +29,7 @@ public function inferFunctionLike(ClassMethod|Function_|Closure $functionLike):
return new MixedType();
}

return $this->typeNormalizer->normalizeArrayTypeAndArrayNever($originalType);
return $originalType;
// return $this->typeNormalizer->normalizeArrayTypeAndArrayNever($originalType);
}
}
135 changes: 0 additions & 135 deletions rules/TypeDeclaration/TypeNormalizer.php

This file was deleted.

37 changes: 0 additions & 37 deletions rules/TypeDeclaration/ValueObject/NestedArrayType.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\TypeDeclaration\TypeNormalizer;

final readonly class TypeComparator
{
public function __construct(
private TypeHasher $typeHasher,
private TypeNormalizer $typeNormalizer,
private StaticTypeMapper $staticTypeMapper,
private ArrayTypeComparator $arrayTypeComparator,
private ScalarTypeComparator $scalarTypeComparator,
Expand Down Expand Up @@ -56,9 +54,6 @@ public function areTypesEqual(Type $firstType, Type $secondType): bool
return true;
}

$firstType = $this->typeNormalizer->normalizeArrayOfUnionToUnionArray($firstType);
$secondType = $this->typeNormalizer->normalizeArrayOfUnionToUnionArray($secondType);

if ($this->typeHasher->areTypesEqual($firstType, $secondType)) {
return true;
}
Expand Down

0 comments on commit 6fce22d

Please sign in to comment.