Skip to content

Commit 6fce22d

Browse files
authored
Remove TypeNormalizer as type comparison is handled well by PHPStan (#6570)
* try running without type narrower * Remove type normalizer, as no longer needed * Fix array type using isArray()
1 parent ff49b20 commit 6fce22d

File tree

8 files changed

+8
-233
lines changed

8 files changed

+8
-233
lines changed

rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/false_bool_docblock.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class FalseBoolDocblock
2828
/**
2929
* @return array|bool some description
3030
*/
31-
public function run(): array|false
31+
public function run(): false|array
3232
{
3333
if (rand(0, 1)) {
3434
return false;

rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use stdClass;
3232

3333
final class UnionArray
3434
{
35-
public function run($a, $b): \DateTime|array|null
35+
public function run($a, $b): null|\DateTime|array
3636
{
3737
if ($a) {
3838
return null;

rules-tests/TypeDeclaration/TypeNormalizerTest.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private function processAddArrayReturnType(
147147
ClassMethod|Function_|Closure $node,
148148
Type $returnType
149149
): ClassMethod|Function_|Closure|null {
150-
if (! $returnType instanceof ArrayType && ! $returnType instanceof ConstantArrayType) {
150+
if (! $returnType->isArray()->yes()) {
151151
return null;
152152
}
153153

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

177-
private function changeReturnType(
178-
ClassMethod|Function_|Closure $node,
179-
ArrayType|ConstantArrayType $arrayType
180-
): void {
177+
private function changeReturnType(ClassMethod|Function_|Closure $node, Type $arrayType): void
178+
{
181179
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
182180

183181
// skip already filled type, on purpose
@@ -265,7 +263,7 @@ private function matchArrayAssignedVariable(array $stmts): array
265263
return $variables;
266264
}
267265

268-
private function shouldAddReturnArrayDocType(ArrayType|ConstantArrayType $arrayType): bool
266+
private function shouldAddReturnArrayDocType(Type $arrayType): bool
269267
{
270268
if ($arrayType instanceof ConstantArrayType) {
271269
if ($arrayType->getIterableValueType() instanceof NeverType) {

rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
final readonly class ReturnTypeInferer
1919
{
2020
public function __construct(
21-
private TypeNormalizer $typeNormalizer,
2221
private ReturnedNodesReturnTypeInfererTypeInferer $returnedNodesReturnTypeInfererTypeInferer
2322
) {
2423
}
@@ -30,6 +29,7 @@ public function inferFunctionLike(ClassMethod|Function_|Closure $functionLike):
3029
return new MixedType();
3130
}
3231

33-
return $this->typeNormalizer->normalizeArrayTypeAndArrayNever($originalType);
32+
return $originalType;
33+
// return $this->typeNormalizer->normalizeArrayTypeAndArrayNever($originalType);
3434
}
3535
}

rules/TypeDeclaration/TypeNormalizer.php

Lines changed: 0 additions & 135 deletions
This file was deleted.

rules/TypeDeclaration/ValueObject/NestedArrayType.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/NodeTypeResolver/TypeComparator/TypeComparator.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
use Rector\Reflection\ReflectionResolver;
2222
use Rector\StaticTypeMapper\StaticTypeMapper;
2323
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
24-
use Rector\TypeDeclaration\TypeNormalizer;
2524

2625
final readonly class TypeComparator
2726
{
2827
public function __construct(
2928
private TypeHasher $typeHasher,
30-
private TypeNormalizer $typeNormalizer,
3129
private StaticTypeMapper $staticTypeMapper,
3230
private ArrayTypeComparator $arrayTypeComparator,
3331
private ScalarTypeComparator $scalarTypeComparator,
@@ -56,9 +54,6 @@ public function areTypesEqual(Type $firstType, Type $secondType): bool
5654
return true;
5755
}
5856

59-
$firstType = $this->typeNormalizer->normalizeArrayOfUnionToUnionArray($firstType);
60-
$secondType = $this->typeNormalizer->normalizeArrayOfUnionToUnionArray($secondType);
61-
6257
if ($this->typeHasher->areTypesEqual($firstType, $secondType)) {
6358
return true;
6459
}

0 commit comments

Comments
 (0)