Skip to content

Commit 241920f

Browse files
committed
inluce non-core special nodes
1 parent f0d8be2 commit 241920f

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector\Fixture;
4+
5+
class RemoveOnCallerViaSelf
6+
{
7+
private static function just_a_test(
8+
string $foo,
9+
array $bar,
10+
$used_parameter
11+
): void {
12+
\external::whatever($foo, $used_parameter);
13+
}
14+
15+
public static function trigger(): void
16+
{
17+
self::just_a_test('hello', [], 'test');
18+
}
19+
}
20+
21+
?>

rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public function refactor(Node $node): ?Node
8080
continue;
8181
}
8282

83+
if ($key === 0) {
84+
continue;
85+
}
86+
8387
$previousStmt = $node->stmts[$key - 1] ?? null;
8488
if (! $previousStmt instanceof If_) {
8589
continue;

src/Bridge/PhpParser/NodeClassFinder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Rector\Bridge\PhpParser;
66

77
use Nette\Loaders\RobotLoader;
8+
use PHPStan\Node\AnonymousClassNode;
9+
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
810
use ReflectionClass;
911

1012
final class NodeClassFinder
@@ -26,13 +28,21 @@ public static function find(): array
2628
/** @var array<class-string> $nodeClasses */
2729
$nodeClasses = array_keys($robotLoader->getIndexedClasses());
2830

29-
return array_filter($nodeClasses, function (string $nodeClass): bool {
31+
$instantiableNodeClasses = array_filter($nodeClasses, function (string $nodeClass): bool {
3032
$nodeClassReflection = new ReflectionClass($nodeClass);
3133
if ($nodeClassReflection->isAbstract()) {
3234
return false;
3335
}
3436

3537
return ! $nodeClassReflection->isInterface();
3638
});
39+
40+
// this is needed, as PHPStan replaces the Class_ node of anonymous class completely
41+
// @see https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Parser/AnonymousClassVisitor.php
42+
$specialPHPStanNodes = [AnonymousClassNode::class];
43+
44+
$specialRectorNodes = [FileWithoutNamespace::class];
45+
46+
return array_merge($instantiableNodeClasses, $specialPHPStanNodes, $specialRectorNodes);
3747
}
3848
}

src/Configuration/ConfigurationRuleFilter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function filter(array $rectors): array
3131

3232
$onlyRule = $this->configuration->getOnlyRule();
3333
if ($onlyRule !== null) {
34-
return [$onlyRule];
34+
foreach ($rectors as $rector) {
35+
if ($rector instanceof $onlyRule) {
36+
return [$rector];
37+
}
38+
}
3539
}
3640

3741
return $rectors;

src/PhpParser/NodeTraverser/RectorNodeTraverser.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function refreshPhpRectors(array $rectors): void
6565
public function getVisitorsForNode(Node $node): array
6666
{
6767
Assert::true($this->areNodeVisitorsPrepared);
68-
6968
return $this->visitorsPerNodeClass[$node::class] ?? [];
7069
}
7170

@@ -85,13 +84,11 @@ private function prepareNodeVisitors(): void
8584
// filter by configuration
8685
$this->visitors = $this->configurationRuleFilter->filter($this->visitors);
8786

88-
// static $counter = 0;
89-
9087
// 1. get all node non-interface, non-abstract classes
9188
// 2. iterate through them
92-
foreach (\Rector\Bridge\PhpParser\NodeClassFinder::find() as $nodeClass) {
93-
/** @var RectorInterface $visitor */
94-
foreach ($this->visitors as $visitor) {
89+
/** @var RectorInterface $visitor */
90+
foreach ($this->visitors as $visitor) {
91+
foreach (\Rector\Bridge\PhpParser\NodeClassFinder::find() as $nodeClass) {
9592
foreach ($visitor->getNodeTypes() as $matchingNodeType) {
9693
if (is_a($nodeClass, $matchingNodeType, true)) {
9794
$this->visitorsPerNodeClass[$nodeClass][] = $visitor;

src/ValueObject/Configuration.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
88
use Rector\Configuration\Option;
99
use Rector\Configuration\Parameter\SimpleParameterProvider;
10-
use Rector\Contract\Rector\RectorInterface;
1110
use Rector\ValueObject\Configuration\LevelOverflow;
1211
use Webmozart\Assert\Assert;
1312

@@ -62,9 +61,6 @@ public function getFileExtensions(): array
6261
return $this->fileExtensions;
6362
}
6463

65-
/**
66-
* @return class-string<RectorInterface>|null
67-
*/
6864
public function getOnlyRule(): ?string
6965
{
7066
return $this->onlyRule;

0 commit comments

Comments
 (0)