Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fc4e94

Browse files
authored
[utils] Add RemoveRefactorDuplicatedNodeInstanceCheckRector (#7529)
* simplify ClassMethodParamVendorLockResolver check * const * [utils] Add RemoveRefactorDuplicatedNodeInstanceCheckRector * add test
1 parent deb6fd6 commit 4fc4e94

File tree

13 files changed

+330
-50
lines changed

13 files changed

+330
-50
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"rules-tests",
8282
"tests"
8383
],
84+
"Rector\\Utils\\Tests\\": "utils-tests",
8485
"E2e\\Parallel\\Reflection\\Resolver\\": [
8586
"e2e/parallel-reflection-resolver/src/",
8687
"e2e/no-parallel-reflection-resolver/src"

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,5 @@ parameters:
352352
-
353353
identifier: offsetAccess.invalidOffset
354354
path: src/CustomRules/SimpleNodeDumper.php
355+
356+
- '#Method Rector\\Utils\\Rector\\RemoveRefactorDuplicatedNodeInstanceCheckRector\:\:getInstanceofNodeClass\(\) should return class\-string<PhpParser\\Node>\|null but returns class\-string#'

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<testsuite name="main">
1414
<directory>tests</directory>
1515
<directory>rules-tests</directory>
16+
<directory>utils-tests</directory>
1617
</testsuite>
1718
</testsuites>
1819

rector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rector\Config\RectorConfig;
77
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
88
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
9+
use Rector\Utils\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector;
910

1011
return RectorConfig::configure()
1112
->withPreparedSets(
@@ -18,7 +19,6 @@
1819
naming: true,
1920
instanceOf: true,
2021
earlyReturn: true,
21-
strictBooleans: true,
2222
rectorPreset: true,
2323
phpunitCodeQuality: true
2424
)
@@ -37,6 +37,7 @@
3737
])
3838
->withRootFiles()
3939
->withImportNames(removeUnusedImports: true)
40+
->withRules([RemoveRefactorDuplicatedNodeInstanceCheckRector::class])
4041
->withSkip([
4142
StringClassNameToClassConstantRector::class,
4243
// tests

rules/CodeQuality/Rector/BooleanNot/ReplaceConstantBooleanNotRector.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ public function getNodeTypes(): array
6969
*/
7070
public function refactor(Node $node): ?Node
7171
{
72-
if (! $node instanceof BooleanNot) {
73-
return null;
74-
}
75-
7672
if ($this->valueResolver->isFalse($node->expr)) {
7773
return new ConstFetch(new Name('true'));
7874
}

rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ public function getNodeTypes(): array
6161
return [FuncCall::class];
6262
}
6363

64+
/**
65+
* @param FuncCall $node
66+
*/
6467
public function refactor(Node $node): ?FuncCall
6568
{
66-
if (! $node instanceof FuncCall) {
67-
return null;
68-
}
69-
7069
if (! $node->name instanceof Name) {
7170
return null;
7271
}

rules/DeadCode/Rector/ClassMethod/RemoveUnusedPublicMethodParameterRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
1414
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
1515
use Rector\Rector\AbstractRector;
16+
use Rector\ValueObject\MethodName;
1617
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1718
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1819

@@ -115,7 +116,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod, Class_ $class):
115116
}
116117

117118
// parameter is required for contract coupling
118-
if ($this->isName($classMethod->name, '__invoke') && $this->phpAttributeAnalyzer->hasPhpAttribute(
119+
if ($this->isName($classMethod->name, MethodName::INVOKE) && $this->phpAttributeAnalyzer->hasPhpAttribute(
119120
$class,
120121
'Symfony\Component\Messenger\Attribute\AsMessageHandler'
121122
)) {

src/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node\Stmt\ClassMethod;
88
use PHPStan\Reflection\ClassReflection;
9-
use Rector\FileSystem\FilePathHelper;
109
use Rector\NodeNameResolver\NodeNameResolver;
1110
use Rector\Reflection\ReflectionResolver;
1211

@@ -15,7 +14,6 @@
1514
public function __construct(
1615
private NodeNameResolver $nodeNameResolver,
1716
private ReflectionResolver $reflectionResolver,
18-
private FilePathHelper $filePathHelper
1917
) {
2018
}
2119

@@ -38,11 +36,7 @@ public function isVendorLocked(ClassMethod $classMethod): bool
3836
$methodName = $this->nodeNameResolver->getName($classMethod);
3937

4038
// has interface vendor lock? → better skip it, as PHPStan has access only to just analyzed classes
41-
if ($this->hasParentInterfaceMethod($classReflection, $methodName)) {
42-
return true;
43-
}
44-
45-
return $this->hasClassMethodLockMatchingFileName($classReflection, $methodName, '/vendor/');
39+
return $this->hasParentInterfaceMethod($classReflection, $methodName);
4640
}
4741

4842
/**
@@ -60,37 +54,4 @@ private function hasParentInterfaceMethod(ClassReflection $classReflection, stri
6054

6155
return false;
6256
}
63-
64-
private function hasClassMethodLockMatchingFileName(
65-
ClassReflection $classReflection,
66-
string $methodName,
67-
string $filePathPartName
68-
): bool {
69-
$ancestorClassReflections = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];
70-
foreach ($ancestorClassReflections as $ancestorClassReflection) {
71-
// parent type
72-
if (! $ancestorClassReflection->hasNativeMethod($methodName)) {
73-
continue;
74-
}
75-
76-
// is file in vendor?
77-
$fileName = $ancestorClassReflection->getFileName();
78-
// probably internal class
79-
if ($fileName === null) {
80-
continue;
81-
}
82-
83-
// not conditions? its a match
84-
if ($filePathPartName === '') {
85-
return true;
86-
}
87-
88-
$normalizedFileName = $this->filePathHelper->normalizePathAndSchema($fileName);
89-
if (str_contains($normalizedFileName, $filePathPartName)) {
90-
return true;
91-
}
92-
}
93-
94-
return false;
95-
}
9657
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;
4+
5+
use PhpParser\Node;
6+
use Rector\Rector\AbstractRector;
7+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
8+
9+
final class CoverBareGetNodeTypes extends AbstractRector
10+
{
11+
public function getRuleDefinition(): RuleDefinition
12+
{
13+
}
14+
15+
public function getNodeTypes(): array
16+
{
17+
return [Node\Stmt\ClassMethod::class];
18+
}
19+
20+
public function refactor(Node $node)
21+
{
22+
if (! $node instanceof Node\Stmt\ClassMethod) {
23+
return null;
24+
}
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;
33+
34+
use PhpParser\Node;
35+
use Rector\Rector\AbstractRector;
36+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
37+
38+
final class CoverBareGetNodeTypes extends AbstractRector
39+
{
40+
public function getRuleDefinition(): RuleDefinition
41+
{
42+
}
43+
44+
public function getNodeTypes(): array
45+
{
46+
return [Node\Stmt\ClassMethod::class];
47+
}
48+
49+
public function refactor(Node $node)
50+
{
51+
}
52+
}
53+
54+
?>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;
4+
5+
use PhpParser\Node;
6+
use Rector\Rector\AbstractRector;
7+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
8+
9+
final class SomeClass extends AbstractRector
10+
{
11+
public function getRuleDefinition(): RuleDefinition
12+
{
13+
}
14+
15+
public function getNodeTypes(): array
16+
{
17+
return [Node\Stmt\ClassMethod::class];
18+
}
19+
20+
/**
21+
* @param Node\Stmt\ClassMethod $node
22+
*/
23+
public function refactor(Node $node)
24+
{
25+
if (! $node instanceof Node\Stmt\ClassMethod) {
26+
return null;
27+
}
28+
}
29+
}
30+
31+
?>
32+
-----
33+
<?php
34+
35+
namespace Rector\Utils\Tests\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector\Fixture;
36+
37+
use PhpParser\Node;
38+
use Rector\Rector\AbstractRector;
39+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
40+
41+
final class SomeClass extends AbstractRector
42+
{
43+
public function getRuleDefinition(): RuleDefinition
44+
{
45+
}
46+
47+
public function getNodeTypes(): array
48+
{
49+
return [Node\Stmt\ClassMethod::class];
50+
}
51+
52+
/**
53+
* @param Node\Stmt\ClassMethod $node
54+
*/
55+
public function refactor(Node $node)
56+
{
57+
}
58+
}
59+
60+
?>

0 commit comments

Comments
 (0)