Skip to content

Commit d2326bf

Browse files
committed
Merge remote-tracking branch 'origin/repeated-3-times' into HEAD
2 parents 4271d9e + 33437be commit d2326bf

8 files changed

Lines changed: 339 additions & 0 deletions
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Fixture;
4+
5+
use Rector\Tests\Issues\RepeatedThreeTimes\Source\SomeArrayCollection;
6+
7+
final class Fixture
8+
{
9+
/**
10+
* @param array|SomeArrayCollection $items
11+
*/
12+
public function run($items)
13+
{
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Fixture;
22+
23+
use Rector\Tests\Issues\RepeatedThreeTimes\Source\SomeCollection;
24+
25+
final class Fixture
26+
{
27+
/**
28+
* @param SomeCollection $items
29+
*/
30+
public function run(SomeCollection $items)
31+
{
32+
}
33+
}
34+
35+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\RepeatedThreeTimes;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
/**
12+
* Each rule depends on the output of the rule registered after it,
13+
* so the file only reaches its final state on the 3rd pass
14+
*
15+
* @see https://github.com/rectorphp/rector-src/pull/8826
16+
*/
17+
final class RepeatedThreeTimesTest extends AbstractRectorTestCase
18+
{
19+
#[DataProvider('provideData')]
20+
public function test(string $filePath): void
21+
{
22+
$this->doTestFile($filePath);
23+
}
24+
25+
public static function provideData(): Iterator
26+
{
27+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
28+
}
29+
30+
public function provideConfigFilePath(): string
31+
{
32+
return __DIR__ . '/config/configured_rule.php';
33+
}
34+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Source;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
10+
use PHPStan\Type\ObjectType;
11+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
12+
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
13+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
14+
use Rector\Rector\AbstractRector;
15+
use Rector\RuleDoc\RuleDefinition;
16+
use Rector\StaticTypeMapper\StaticTypeMapper;
17+
18+
/**
19+
* @param array|SomeArrayCollection $items → @param array|SomeCollection $items
20+
*/
21+
final class NarrowArrayCollectionDocblockRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
25+
private readonly DocBlockUpdater $docBlockUpdater,
26+
private readonly StaticTypeMapper $staticTypeMapper
27+
) {
28+
}
29+
30+
public function getRuleDefinition(): RuleDefinition
31+
{
32+
return new RuleDefinition('', []);
33+
}
34+
35+
/**
36+
* @return array<class-string<Node>>
37+
*/
38+
public function getNodeTypes(): array
39+
{
40+
return [ClassMethod::class];
41+
}
42+
43+
/**
44+
* @param ClassMethod $node
45+
*/
46+
public function refactor(Node $node): ?ClassMethod
47+
{
48+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
49+
if ($phpDocInfo === null) {
50+
return null;
51+
}
52+
53+
$hasChanged = false;
54+
foreach ($phpDocInfo->getParamTagValueNodes() as $paramTagValueNode) {
55+
if (! $paramTagValueNode->type instanceof UnionTypeNode) {
56+
continue;
57+
}
58+
59+
foreach ($paramTagValueNode->type->types as $key => $unionedTypeNode) {
60+
$unionedType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($unionedTypeNode, $node);
61+
if (! $unionedType->equals(new ObjectType(SomeArrayCollection::class))) {
62+
continue;
63+
}
64+
65+
$paramTagValueNode->type->types[$key] = new FullyQualifiedIdentifierTypeNode(SomeCollection::class);
66+
$hasChanged = true;
67+
}
68+
}
69+
70+
if (! $hasChanged) {
71+
return null;
72+
}
73+
74+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
75+
76+
return $node;
77+
}
78+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Source;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
10+
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
11+
use PHPStan\Type\ObjectType;
12+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
13+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
14+
use Rector\Rector\AbstractRector;
15+
use Rector\RuleDoc\RuleDefinition;
16+
use Rector\StaticTypeMapper\StaticTypeMapper;
17+
18+
/**
19+
* @param array|SomeCollection $items → @param SomeCollection $items
20+
*/
21+
final class NarrowParamUnionDocblockRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
25+
private readonly DocBlockUpdater $docBlockUpdater,
26+
private readonly StaticTypeMapper $staticTypeMapper
27+
) {
28+
}
29+
30+
public function getRuleDefinition(): RuleDefinition
31+
{
32+
return new RuleDefinition('', []);
33+
}
34+
35+
/**
36+
* @return array<class-string<Node>>
37+
*/
38+
public function getNodeTypes(): array
39+
{
40+
return [ClassMethod::class];
41+
}
42+
43+
/**
44+
* @param ClassMethod $node
45+
*/
46+
public function refactor(Node $node): ?ClassMethod
47+
{
48+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
49+
if ($phpDocInfo === null) {
50+
return null;
51+
}
52+
53+
$hasChanged = false;
54+
foreach ($phpDocInfo->getParamTagValueNodes() as $paramTagValueNode) {
55+
if (! $paramTagValueNode->type instanceof UnionTypeNode) {
56+
continue;
57+
}
58+
59+
if (count($paramTagValueNode->type->types) !== 2) {
60+
continue;
61+
}
62+
63+
[$firstTypeNode, $secondTypeNode] = $paramTagValueNode->type->types;
64+
if (! $firstTypeNode instanceof IdentifierTypeNode || $firstTypeNode->name !== 'array') {
65+
continue;
66+
}
67+
68+
$secondType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($secondTypeNode, $node);
69+
if (! $secondType->equals(new ObjectType(SomeCollection::class))) {
70+
continue;
71+
}
72+
73+
$paramTagValueNode->type = $secondTypeNode;
74+
$hasChanged = true;
75+
}
76+
77+
if (! $hasChanged) {
78+
return null;
79+
}
80+
81+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
82+
83+
return $node;
84+
}
85+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Source;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Name\FullyQualified;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PHPStan\Type\ObjectType;
11+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
12+
use Rector\Rector\AbstractRector;
13+
use Rector\RuleDoc\RuleDefinition;
14+
15+
/**
16+
* @param SomeCollection $items → native SomeCollection $items param type
17+
*/
18+
final class ParamTypeFromDocblockRector extends AbstractRector
19+
{
20+
public function __construct(
21+
private readonly PhpDocInfoFactory $phpDocInfoFactory
22+
) {
23+
}
24+
25+
public function getRuleDefinition(): RuleDefinition
26+
{
27+
return new RuleDefinition('', []);
28+
}
29+
30+
/**
31+
* @return array<class-string<Node>>
32+
*/
33+
public function getNodeTypes(): array
34+
{
35+
return [ClassMethod::class];
36+
}
37+
38+
/**
39+
* @param ClassMethod $node
40+
*/
41+
public function refactor(Node $node): ?ClassMethod
42+
{
43+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
44+
if ($phpDocInfo === null) {
45+
return null;
46+
}
47+
48+
$hasChanged = false;
49+
foreach ($node->params as $param) {
50+
if ($param->type instanceof Node) {
51+
continue;
52+
}
53+
54+
$paramType = $phpDocInfo->getParamType($this->getName($param));
55+
if (! $paramType->equals(new ObjectType(SomeCollection::class))) {
56+
continue;
57+
}
58+
59+
$param->type = new FullyQualified(SomeCollection::class);
60+
$hasChanged = true;
61+
}
62+
63+
if (! $hasChanged) {
64+
return null;
65+
}
66+
67+
return $node;
68+
}
69+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Source;
6+
7+
final class SomeArrayCollection implements SomeCollection
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\RepeatedThreeTimes\Source;
6+
7+
interface SomeCollection
8+
{
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Tests\Issues\RepeatedThreeTimes\Source\NarrowArrayCollectionDocblockRector;
7+
use Rector\Tests\Issues\RepeatedThreeTimes\Source\NarrowParamUnionDocblockRector;
8+
use Rector\Tests\Issues\RepeatedThreeTimes\Source\ParamTypeFromDocblockRector;
9+
10+
return RectorConfig::configure()
11+
->withImportNames()
12+
// registered in reverse order of dependency:
13+
// 3rd pass: native param type from @param SomeCollection
14+
// 2nd pass: @param array|SomeCollection to @param SomeCollection
15+
// 1st pass: @param array|SomeArrayCollection to @param array|SomeCollection
16+
->withRules([
17+
ParamTypeFromDocblockRector::class,
18+
NarrowParamUnionDocblockRector::class,
19+
NarrowArrayCollectionDocblockRector::class,
20+
]);

0 commit comments

Comments
 (0)