|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Solidifier\Visitors\GetterSetter; |
|
4
|
|
|
|
|
5
|
|
|
use Solidifier\Parser\Visitors\ContextualVisitor; |
|
6
|
|
|
use Solidifier\Visitors\ObjectType; |
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PhpParser\Node\Stmt\Return_; |
|
9
|
|
|
use PhpParser\Node\Expr\Variable; |
|
10
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
11
|
|
|
use Solidifier\Defects\NotFluidSetter; |
|
12
|
|
|
|
|
13
|
|
|
class FluidSetters extends ContextualVisitor |
|
14
|
|
|
{ |
|
15
|
|
|
private |
|
16
|
|
|
$currentMethodState; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
protected function before(array $nodes) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->currentMethodState = null; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function enter(Node $node) |
|
24
|
|
|
{ |
|
25
|
|
|
if($node instanceof ClassMethod) |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->enterClassMethod($node); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if($node instanceof Return_) |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->enterReturn($node); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function enterClassMethod(ClassMethod $node) |
|
37
|
|
|
{ |
|
38
|
|
|
if($this->isASetter($node->name)) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->currentMethodState = new FluidSetterState($node->name); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function isASetter($methodName) |
|
45
|
|
|
{ |
|
46
|
|
|
return strtolower(substr($methodName, 0, 3)) === 'set'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function enterReturn(Return_ $node) |
|
50
|
|
|
{ |
|
51
|
|
|
if($this->currentMethodState instanceof FluidSetterState) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->currentMethodState->returnCount++; |
|
54
|
|
|
|
|
55
|
|
|
if($node->expr instanceof Variable) |
|
56
|
|
|
{ |
|
57
|
|
|
if($node->expr->name === 'this') |
|
58
|
|
|
{ |
|
59
|
|
|
$this->currentMethodState->returnThis = true; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function leave(Node $node) |
|
66
|
|
|
{ |
|
67
|
|
|
if($node instanceof ClassMethod) |
|
68
|
|
|
{ |
|
69
|
|
|
if($this->currentMethodState instanceof FluidSetterState) |
|
70
|
|
|
{ |
|
71
|
|
|
if($this->currentObjectType->type !== ObjectType::TYPE_INTERFACE |
|
72
|
|
|
&& $this->currentMethodState->isValid() === false) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->dispatch( |
|
75
|
|
|
new NotFluidSetter($this->currentObjectType, $node) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->currentMethodState = null; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.