Skip to content

Commit 4b182bc

Browse files
authored
generalize string type (#7366)
1 parent 48c564a commit 4b182bc

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;
4+
5+
use PHPStan\Type\ObjectType;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;
9+
10+
final class KeepStringTypeSimple extends TestCase
11+
{
12+
#[DataProvider('provideData')]
13+
public function testSomething()
14+
{
15+
}
16+
17+
public static function provideData(): \Iterator
18+
{
19+
$anotherTypeObjectType = new ObjectType(AnotherType::class);
20+
yield [__DIR__ . '/Fixture/new_class.php.inc', 1, $anotherTypeObjectType];
21+
yield [__DIR__ . '/Fixture/argument_typehint.php.inc', 1, $anotherTypeObjectType];
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;
30+
31+
use PHPStan\Type\ObjectType;
32+
use PHPUnit\Framework\Attributes\DataProvider;
33+
use PHPUnit\Framework\TestCase;
34+
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;
35+
36+
final class KeepStringTypeSimple extends TestCase
37+
{
38+
#[DataProvider('provideData')]
39+
public function testSomething()
40+
{
41+
}
42+
43+
/**
44+
* @return \Iterator<array<int, (int | \PHPStan\Type\ObjectType | string)>>
45+
*/
46+
public static function provideData(): \Iterator
47+
{
48+
$anotherTypeObjectType = new ObjectType(AnotherType::class);
49+
yield [__DIR__ . '/Fixture/new_class.php.inc', 1, $anotherTypeObjectType];
50+
yield [__DIR__ . '/Fixture/argument_typehint.php.inc', 1, $anotherTypeObjectType];
51+
}
52+
}
53+
54+
?>

rules/Privatization/TypeManipulator/TypeNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Rector\Privatization\TypeManipulator;
66

7+
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
8+
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
79
use PHPStan\Type\ArrayType;
810
use PHPStan\Type\BooleanType;
911
use PHPStan\Type\Constant\ConstantArrayType;
@@ -55,6 +57,10 @@ public function generalizeConstantBoolTypes(Type $type): Type
5557
public function generalizeConstantTypes(Type $type): Type
5658
{
5759
return TypeTraverser::map($type, function (Type $type, callable $traverseCallback): Type {
60+
if ($type instanceof AccessoryNonFalsyStringType || $type instanceof AccessoryLiteralStringType) {
61+
return new StringType();
62+
}
63+
5864
if ($type instanceof ConstantBooleanType) {
5965
return new BooleanType();
6066
}

0 commit comments

Comments
 (0)