forked from symplify/phpstan-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoModifyAndReturnSelfObjectRule.php
159 lines (135 loc) · 4.46 KB
/
NoModifyAndReturnSelfObjectRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
use Symplify\Astral\Naming\SimpleNameResolver;
use Symplify\Astral\NodeFinder\SimpleNodeFinder;
use Symplify\PHPStanRules\NodeAnalyzer\AssignAnalyzer;
use Symplify\PHPStanRules\NodeFinder\ReturnNodeFinder;
use Symplify\PHPStanRules\Printer\NodeComparator;
use Symplify\PHPStanRules\Reflection\MethodNodeAnalyser;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\NoModifyAndReturnSelfObjectRule\NoModifyAndReturnSelfObjectRuleTest
*/
final class NoModifyAndReturnSelfObjectRule extends AbstractSymplifyRule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Use void instead of modify and return self object';
public function __construct(
private ReturnNodeFinder $returnNodeFinder,
private NodeComparator $nodeComparator,
private SimpleNodeFinder $simpleNodeFinder,
private SimpleNameResolver $simpleNameResolver,
private AssignAnalyzer $assignAnalyzer,
private MethodNodeAnalyser $methodNodeAnalyser
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Return_::class];
}
/**
* @param Return_ $node
* @return string[]
*/
public function process(Node $node, Scope $scope): array
{
if (! $node->expr instanceof Variable) {
return [];
}
$classMethod = $this->simpleNodeFinder->findFirstParentByType($node, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return [];
}
/** @var string $methodName */
$methodName = $this->simpleNameResolver->getName($classMethod);
if ($this->methodNodeAnalyser->hasParentVendorLock($scope, $methodName)) {
return [];
}
if (! $this->isReturnedVariableParam($node->expr, $classMethod)) {
return [];
}
if ($this->shouldSkipForIncompatibleReturnType($classMethod, $scope)) {
return [];
}
$type = $scope->getType($node->expr);
if (! $type instanceof ObjectType) {
return [];
}
return [self::ERROR_MESSAGE];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function modify(ComposerJson $composerJson): ComposerJson
{
$composerJson->addPackage('some-package');
return $composerJson;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function modify(ComposerJson $composerJson): void
{
$composerJson->addPackage('some-package');
}
}
CODE_SAMPLE
),
]);
}
private function isReturnedVariableParam(Variable $variable, ClassMethod $classMethod): bool
{
foreach ($classMethod->params as $param) {
if ($this->nodeComparator->areNodesEqual($param->var, $variable)) {
return true;
}
}
return false;
}
private function shouldSkipForIncompatibleReturnType(ClassMethod $classMethod, Scope $scope): bool
{
$varibleNames = [];
$returns = $this->returnNodeFinder->findReturnsWithValues($classMethod);
foreach ($returns as $return) {
if (! $return->expr instanceof Variable) {
return true;
}
$returnedType = $scope->getType($return->expr);
if ($returnedType instanceof ErrorType) {
return true;
}
if (! $returnedType instanceof ObjectType) {
return true;
}
$varibleNames[] = $this->simpleNameResolver->getName($return->expr);
}
/** @var string[] $uniqueVaribleNames */
$uniqueVaribleNames = array_unique($varibleNames);
if (count($uniqueVaribleNames) !== 1) {
return true;
}
$uniqueVaribleName = $uniqueVaribleNames[0];
return $this->assignAnalyzer->isVarialeNameBeingAssigned($classMethod, $uniqueVaribleName);
}
}