|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Solidifier\Visitors\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Solidifier\Parser\Visitors\ContextualVisitor; |
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
use PhpParser\Node\Name; |
|
8
|
|
|
use PhpParser\Node\Expr\New_; |
|
9
|
|
|
use Solidifier\Visitors\ObjectType; |
|
10
|
|
|
|
|
11
|
|
|
class StrongCoupling extends ContextualVisitor |
|
12
|
|
|
{ |
|
13
|
|
|
private |
|
14
|
|
|
$allowedClass, |
|
|
|
|
|
|
15
|
|
|
$excludePattern; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
parent::__construct(); |
|
20
|
|
|
|
|
21
|
|
|
$this->allowedClass = array(); |
|
|
|
|
|
|
22
|
|
|
$this->excludePattern = array(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function addAllowedClass($fullyQualifiedClassName) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->allowedClass[] = $fullyQualifiedClassName; |
|
28
|
|
|
|
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function addExcludePattern($regex) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->excludePattern[] = $regex; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function enter(Node $node) |
|
40
|
|
|
{ |
|
41
|
|
|
if($this->currentObjectType instanceof ObjectType) |
|
42
|
|
|
{ |
|
43
|
|
|
if($node instanceof New_) |
|
44
|
|
|
{ |
|
45
|
|
|
if($node->class instanceof Name) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->enterNewName($node); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function enterNewName(New_ $node) |
|
54
|
|
|
{ |
|
55
|
|
|
if($this->isCurrentClassAllowedToInstanciateObjects() === false) |
|
56
|
|
|
{ |
|
57
|
|
|
if($this->isAnAllowedObjectType($node->class) === false) |
|
58
|
|
|
{ |
|
59
|
|
|
$defect = new \Solidifier\Defects\StrongCoupling($this->currentObjectType, $node); |
|
|
|
|
|
|
60
|
|
|
$defect->setContext($this->nodeStack->top()); |
|
61
|
|
|
$this->dispatch($defect); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function isCurrentClassAllowedToInstanciateObjects() |
|
67
|
|
|
{ |
|
68
|
|
|
return in_array($this->currentObjectType->fullname, $this->allowedClass); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function isAnAllowedObjectType($objectType) |
|
72
|
|
|
{ |
|
73
|
|
|
$allowed = false; |
|
74
|
|
|
|
|
75
|
|
|
$objectType = $this->extractClassNameFromFullName($objectType); |
|
76
|
|
|
|
|
77
|
|
|
foreach($this->excludePattern as $pattern) |
|
78
|
|
|
{ |
|
79
|
|
|
if(preg_match($pattern, $objectType)) |
|
80
|
|
|
{ |
|
81
|
|
|
$allowed = true; |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $allowed; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function extractClassNameFromFullName($classFullname) |
|
90
|
|
|
{ |
|
91
|
|
|
$classname = $classFullname; |
|
92
|
|
|
|
|
93
|
|
|
$lastNamespaceDelimiterPosition = strrpos($classFullname, '\\'); |
|
94
|
|
|
if($lastNamespaceDelimiterPosition !== false) |
|
95
|
|
|
{ |
|
96
|
|
|
$classname = substr($classFullname, $lastNamespaceDelimiterPosition + 1); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $classname; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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.