|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Solidifier\Visitors\Encapsulation; |
|
4
|
|
|
|
|
5
|
|
|
use Solidifier\Parser\Visitors\ContextualVisitor; |
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
use PhpParser\Node\Stmt\Property; |
|
8
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
9
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
10
|
|
|
|
|
11
|
|
|
class EncapsulationViolation extends ContextualVisitor |
|
12
|
|
|
{ |
|
13
|
|
|
private |
|
14
|
|
|
$privateAttributes, |
|
|
|
|
|
|
15
|
|
|
$publicMethods; |
|
16
|
|
|
|
|
17
|
|
|
protected function enter(Node $node) |
|
18
|
|
|
{ |
|
19
|
|
|
if($node instanceof Class_) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->privateAttributes = array(); |
|
22
|
|
|
$this->publicMethods = array(); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
elseif($node instanceof Property) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->enterProperty($node); |
|
27
|
|
|
} |
|
28
|
|
|
elseif($node instanceof ClassMethod) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->enterClassMethod($node); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function enterProperty(Property $node) |
|
35
|
|
|
{ |
|
36
|
|
|
if($node->isPrivate()) |
|
37
|
|
|
{ |
|
38
|
|
|
foreach($node->props as $property) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->privateAttributes[$property->name] = $node; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function enterClassMethod(ClassMethod $node) |
|
46
|
|
|
{ |
|
47
|
|
|
if($node->isPublic()) |
|
48
|
|
|
{ |
|
49
|
|
|
$methodName = $node->name; |
|
50
|
|
|
|
|
51
|
|
|
if($this->isGetterOrSetter($methodName)) |
|
52
|
|
|
{ |
|
53
|
|
|
$correspondingAttribute = strtolower(substr($methodName, 3)); |
|
54
|
|
|
|
|
55
|
|
|
if(! isset($this->publicMethods[$correspondingAttribute])) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->publicMethods[$correspondingAttribute] = array(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->publicMethods[$correspondingAttribute][] = $methodName; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function isGetterOrSetter($methodName) |
|
66
|
|
|
{ |
|
67
|
|
|
$prefix = strtolower(substr($methodName, 0, 3)); |
|
68
|
|
|
|
|
69
|
|
|
return $prefix === 'get' || $prefix === 'set'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function leave(Node $node) |
|
73
|
|
|
{ |
|
74
|
|
|
if($node instanceof Class_) |
|
75
|
|
|
{ |
|
76
|
|
|
foreach($this->privateAttributes as $attribute => $attributeNode) |
|
77
|
|
|
{ |
|
78
|
|
|
$key = strtolower($attribute); |
|
79
|
|
|
if(isset($this->publicMethods[$key])) |
|
80
|
|
|
{ |
|
81
|
|
|
if(count($this->publicMethods[$key]) >= 2) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->dispatch( |
|
84
|
|
|
new \Solidifier\Defects\EncapsulationViolation($attribute, $attributeNode) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function after(array $nodes) |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.