1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Kévin Gomez https://github.com/K-Phoen <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Analyzer\Pass; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
9
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Describes an analyzer pass. |
13
|
|
|
*/ |
14
|
|
|
class Metadata |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $name; |
18
|
|
|
|
19
|
|
|
/** @var string|null */ |
20
|
|
|
private $description; |
21
|
|
|
|
22
|
|
|
/** @var \Symfony\Component\Config\Definition\Builder\NodeDefinition */ |
23
|
|
|
private $configuration; |
24
|
|
|
|
25
|
|
|
/** @var string|null */ |
26
|
|
|
private $requiredPhpVersion; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $name |
30
|
|
|
* @param string|null $description |
31
|
|
|
* |
32
|
|
|
* @return Metadata |
33
|
|
|
*/ |
34
|
1 |
|
public static function create($name, $description = null) |
35
|
|
|
{ |
36
|
1 |
|
$treeBuilder = new TreeBuilder(); |
37
|
|
|
|
38
|
1 |
|
$config = $treeBuilder->root($name) |
|
|
|
|
39
|
1 |
|
->info($description) |
40
|
1 |
|
->canBeDisabled() |
41
|
1 |
|
; |
42
|
|
|
|
43
|
1 |
|
return new self($name, $config, $description); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @param null $description |
49
|
|
|
* @param NodeDefinition|null $config |
50
|
|
|
*/ |
51
|
1 |
|
private function __construct($name, NodeDefinition $config, $description = null) |
52
|
|
|
{ |
53
|
1 |
|
$this->name = $name; |
54
|
1 |
|
$this->configuration = $config; |
55
|
1 |
|
$this->description = $description; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function getName() |
62
|
|
|
{ |
63
|
1 |
|
return $this->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string|null |
68
|
|
|
*/ |
69
|
|
|
public function getDescription() |
70
|
|
|
{ |
71
|
|
|
return $this->description; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $description |
76
|
|
|
*/ |
77
|
|
|
public function setDescription($description) |
78
|
|
|
{ |
79
|
|
|
$this->description = $description; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return NodeDefinition |
84
|
|
|
*/ |
85
|
1 |
|
public function getConfiguration() |
86
|
|
|
{ |
87
|
1 |
|
return $this->configuration; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string|null |
92
|
|
|
*/ |
93
|
|
|
public function getRequiredPhpVersion() |
94
|
|
|
{ |
95
|
|
|
return $this->requiredPhpVersion; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $requiredPhpVersion |
100
|
|
|
*/ |
101
|
1 |
|
public function setRequiredPhpVersion($requiredPhpVersion) |
102
|
|
|
{ |
103
|
1 |
|
$this->requiredPhpVersion = $requiredPhpVersion; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Tells if the current analyzer can be used with code written in a given PHP version. |
108
|
|
|
* |
109
|
|
|
* @param string $version |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
1 |
|
public function allowsPhpVersion($version) |
114
|
|
|
{ |
115
|
1 |
|
return $this->requiredPhpVersion === null || version_compare($version, $this->requiredPhpVersion, '>='); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: