Skip to content

Commit a7b3eb3

Browse files
authored
[AutoImport] Avoid used import into param that skipped from removal rule (#8078)
* [AutoImport] Avoid used import into param that skipped from removal rule * fix * avoid repeated clone * Revert "avoid repeated clone" This reverts commit de88dba. * handle set php doc info null on AbstractRector * add visitor
1 parent dd21759 commit a7b3eb3

5 files changed

Lines changed: 90 additions & 3 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpParser\NodeVisitor;
6+
7+
use PhpParser\Node;
8+
use PhpParser\NodeVisitorAbstract;
9+
use Rector\NodeTypeResolver\Node\AttributeKey;
10+
11+
final class PhpDocInfoRemovingNodeVisitor extends NodeVisitorAbstract
12+
{
13+
public function enterNode(Node $node): Node
14+
{
15+
$node->setAttribute(AttributeKey::PHP_DOC_INFO, null);
16+
return $node;
17+
}
18+
}

‎src/Rector/AbstractRector.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
3636
use Rector\PhpParser\Comparing\NodeComparator;
3737
use Rector\PhpParser\Node\NodeFactory;
38+
use Rector\PhpParser\NodeVisitor\PhpDocInfoRemovingNodeVisitor;
3839
use Rector\Skipper\Skipper\Skipper;
3940
use Rector\Skipper\ValueObject\SkipMatch;
4041
use Rector\ValueObject\Application\File;
@@ -280,7 +281,7 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void
280281
*/
281282
private function cloneNode(Node $node): Node
282283
{
283-
$nodeTraverser = new NodeTraverser(new CloningVisitor());
284+
$nodeTraverser = new NodeTraverser(new CloningVisitor(), new PhpDocInfoRemovingNodeVisitor());
284285
return $nodeTraverser->traverse([$node])[0];
285286
}
286287

‎stubs/Config/App.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
if (class_exists('Config\App')) {
6+
return;
7+
}
8+
9+
class App
10+
{
11+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\HTTP;
6+
7+
use Config\App;
8+
9+
final class Response
10+
{
11+
public $property;
12+
13+
/**
14+
* @param App $config
15+
*
16+
* @deprecated The param $config is no longer used.
17+
*/
18+
public function __construct($config)
19+
{
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
declare(strict_types=1);
28+
29+
namespace CodeIgniter\HTTP;
30+
31+
use DateTime;
32+
use Config\App;
33+
34+
final class Response
35+
{
36+
/**
37+
* @var DateTime
38+
*/
39+
public $property;
40+
41+
/**
42+
* @param App $config
43+
*
44+
* @deprecated The param $config is no longer used.
45+
*/
46+
public function __construct($config)
47+
{
48+
}
49+
}
50+
51+
?>

‎tests/Issues/ImportFullyQualifiedIdentifierDocblock/config/configured_rule.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
67
use Rector\Tests\Issues\ImportFullyQualifiedIdentifierDocblock\Source\AddFullyQualifiedIdentifierDocblockRector;
78

89
return RectorConfig::configure()
9-
->withRules([AddFullyQualifiedIdentifierDocblockRector::class])
10-
->withImportNames();
10+
->withRules([AddFullyQualifiedIdentifierDocblockRector::class, RemoveUnusedConstructorParamRector::class])
11+
->withImportNames()
12+
->withSkip([
13+
RemoveUnusedConstructorParamRector::class => [
14+
realpath(__DIR__ . '/../Fixture') . '/keep_import_used_in_param.php',
15+
],
16+
]);

0 commit comments

Comments
 (0)