1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace DependencyAnalyzer\Inspector\RuleViolationDetector; |
5
|
|
|
|
6
|
|
|
use DependencyAnalyzer\Patterns\QualifiedNamePattern; |
7
|
|
|
|
8
|
|
|
class Component |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $name; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var QualifiedNamePattern |
17
|
|
|
*/ |
18
|
|
|
protected $pattern; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array|QualifiedNamePattern[] |
22
|
|
|
*/ |
23
|
|
|
protected $dependerPatterns; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array|QualifiedNamePattern[] |
27
|
|
|
*/ |
28
|
|
|
protected $dependeePatterns; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Component constructor. |
32
|
|
|
* @param string $name |
33
|
|
|
* @param QualifiedNamePattern $pattern |
34
|
|
|
* @param QualifiedNamePattern $dependerPatterns |
35
|
|
|
* @param QualifiedNamePattern $dependeePatterns |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $name, QualifiedNamePattern $pattern, QualifiedNamePattern $dependerPatterns = null, QualifiedNamePattern $dependeePatterns = null) |
38
|
|
|
{ |
39
|
|
|
$this->name = $name; |
40
|
|
|
$this->pattern = $pattern; |
41
|
|
|
$this->dependerPatterns = $dependerPatterns; |
|
|
|
|
42
|
|
|
$this->dependeePatterns = $dependeePatterns; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getName(): string |
46
|
|
|
{ |
47
|
|
|
return $this->name; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isBelongedTo(string $className): bool |
51
|
|
|
{ |
52
|
|
|
return $this->pattern->isMatch($className); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function verifyDepender(string $className): bool |
56
|
|
|
{ |
57
|
|
|
if ($this->isBelongedTo($className)) { |
58
|
|
|
return true; |
59
|
|
|
} elseif (is_null($this->dependerPatterns)) { |
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->dependerPatterns->isMatch($className); |
64
|
|
|
// return $this->checkPatterns($className, $this->dependerPatterns); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function verifyDependee(string $className): bool |
68
|
|
|
{ |
69
|
|
|
if ($this->isBelongedTo($className)) { |
70
|
|
|
return true; |
71
|
|
|
} elseif (is_null($this->dependeePatterns)) { |
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->dependeePatterns->isMatch($className); |
76
|
|
|
// return $this->checkPatterns($className, $this->dependeePatterns); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $className |
81
|
|
|
* @param QualifiedNamePattern[] $patterns |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function checkPatterns(string $className, array $patterns): bool |
85
|
|
|
{ |
86
|
|
|
foreach ($patterns as $pattern) { |
87
|
|
|
if ($pattern->isMatch($className)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.