Skip to content

Commit 6a73bb5

Browse files
committed
const
1 parent c074f1d commit 6a73bb5

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
"doctrine/inflector": "^2.1",
2121
"illuminate/container": "^11.46",
2222
"nette/utils": "^4.0",
23+
<<<<<<< HEAD
2324
"nikic/php-parser": "^5.6.2",
25+
=======
26+
"nikic/php-parser": "5.6.1",
27+
>>>>>>> 2a1a5105ab (lock php parser to 5.6.1 to avoid breaking space on attributes)
2428
"ondram/ci-detector": "^4.2",
2529
"phpstan/phpdoc-parser": "^2.3",
2630
"phpstan/phpstan": "^2.1.26",

rules/DeadCode/Rector/ClassMethod/RemoveUnusedPublicMethodParameterRector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
1414
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
1515
use Rector\Rector\AbstractRector;
16+
use Rector\Symfony\Enum\SymfonyClass;
17+
use Rector\ValueObject\MethodName;
1618
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1719
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1820

@@ -115,7 +117,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod, Class_ $class):
115117
}
116118

117119
// parameter is required for contract coupling
118-
if ($this->isName($classMethod->name, '__invoke') && $this->phpAttributeAnalyzer->hasPhpAttribute(
120+
if ($this->isName($classMethod->name, MethodName::INVOKE) && $this->phpAttributeAnalyzer->hasPhpAttribute(
119121
$class,
120122
'Symfony\Component\Messenger\Attribute\AsMessageHandler'
121123
)) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Utils\Rector;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\PropertyFetch;
9+
use PhpParser\Node\Stmt\Class_;
10+
use PhpParser\Node\Stmt\If_;
11+
use PhpParser\Node\Stmt\Property;
12+
use PHPStan\Type\ObjectType;
13+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
14+
use Rector\NodeManipulator\ClassDependencyManipulator;
15+
use Rector\PhpParser\Node\BetterNodeFinder;
16+
use Rector\PhpParser\Node\Value\ValueResolver;
17+
use Rector\PostRector\ValueObject\PropertyMetadata;
18+
use Rector\Rector\AbstractRector;
19+
use Rector\StaticTypeMapper\StaticTypeMapper;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
22+
final class RemoveRefactorDuplicatedNodeInstanceCheckRector extends AbstractRector
23+
{
24+
public function getRuleDefinition(): RuleDefinition
25+
{
26+
return new RuleDefinition('Remove refactor() method of Rector rule double check of $node instance, if already defined in @param type', []);
27+
}
28+
29+
public function getNodeTypes(): array
30+
{
31+
return [Node\Stmt\ClassMethod::class];
32+
}
33+
34+
/**
35+
* @param Node\Stmt\ClassMethod $node
36+
*/
37+
public function refactor(Node $node): ?Node
38+
{
39+
if (! $this->isName($node->name, 'refactor')) {
40+
return null;
41+
}
42+
43+
if (! $node->isPublic()) {
44+
return null;
45+
}
46+
47+
$firstStmt = $node->stmts[0] ?? null;
48+
if (! $firstStmt instanceof If_) {
49+
return null;
50+
}
51+
52+
// remove already added properties
53+
54+
if ($typesToAdd === []) {
55+
return null;
56+
}
57+
58+
$hasChanged = false;
59+
60+
foreach ($typesToAdd as $propertyNameToAdd => $propertyTypeToAdd) {
61+
// skip if property already exists
62+
if ($node->getProperty($propertyNameToAdd) instanceof Property) {
63+
continue;
64+
}
65+
66+
$this->classDependencyManipulator->addConstructorDependency(
67+
$node,
68+
new PropertyMetadata($propertyNameToAdd, new ObjectType($propertyTypeToAdd))
69+
);
70+
71+
$hasChanged = true;
72+
}
73+
74+
if (! $hasChanged) {
75+
return null;
76+
}
77+
78+
return $node;
79+
}
80+
}

0 commit comments

Comments
 (0)