1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A base class for all tokens. |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractToken implements Token |
12
|
|
|
{ |
13
|
|
|
/** @var Configuration */ |
14
|
|
|
protected $configuration; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
private $depth; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
private $line; |
21
|
|
|
|
22
|
|
|
/** @var null|Token */ |
23
|
|
|
private $parent; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
private $position; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
*/ |
31
|
202 |
|
public function __construct(Configuration $configuration, int $line, int $position) |
32
|
|
|
{ |
33
|
202 |
|
$this->configuration = $configuration; |
34
|
202 |
|
$this->depth = 0; |
35
|
202 |
|
$this->line = $line; |
36
|
202 |
|
$this->parent = null; |
37
|
202 |
|
$this->position = $position; |
38
|
202 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Required by the Token interface. |
42
|
|
|
*/ |
43
|
141 |
|
public function getDepth() : int |
44
|
|
|
{ |
45
|
141 |
|
return $this->depth; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Getter for 'line'. |
50
|
|
|
*/ |
51
|
92 |
|
public function getLine() : int |
52
|
|
|
{ |
53
|
92 |
|
return $this->line; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Required by the Token interface. |
58
|
|
|
*/ |
59
|
80 |
|
public function getParent() |
60
|
|
|
{ |
61
|
80 |
|
return $this->parent; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Chainable setter for 'parent'. |
66
|
|
|
*/ |
67
|
140 |
|
public function setParent(Token $parent = null) |
68
|
|
|
{ |
69
|
140 |
|
$this->depth = 0; |
70
|
140 |
|
if ($parent instanceof Token) { |
71
|
140 |
|
$this->depth = $parent->getDepth() + 1; |
72
|
|
|
} |
73
|
|
|
|
74
|
140 |
|
$this->parent = $parent; |
75
|
140 |
|
} |
76
|
|
|
|
77
|
18 |
|
public function hasAncestor(Element $element) : bool |
78
|
|
|
{ |
79
|
18 |
|
if ($this->parent === null) { |
80
|
16 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
16 |
|
if ($this->parent->getType() === Token::ELEMENT && |
84
|
16 |
|
$this->parent->getName() === $element->getName()) { |
|
|
|
|
85
|
9 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
13 |
|
return $this->parent->hasAncestor($element); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Getter for 'position'. |
93
|
|
|
*/ |
94
|
92 |
|
public function getPosition() : int |
95
|
|
|
{ |
96
|
92 |
|
return $this->position; |
97
|
|
|
} |
98
|
|
|
|
99
|
150 |
|
public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger) |
100
|
|
|
{ |
101
|
150 |
|
if ($configuration->get('clean-strategy') === Configuration::CLEAN_STRATEGY_NONE) { |
102
|
137 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
149 |
|
foreach ($children as $key => $child) { |
106
|
145 |
|
if ($child instanceof Cleanable) { |
107
|
139 |
|
$isClean = $child->clean($logger); |
108
|
139 |
|
if (!$isClean && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) { |
109
|
145 |
|
unset($children[$key]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
149 |
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
abstract public function getType() : string; |
118
|
|
|
|
119
|
13 |
|
public function __toString() |
120
|
|
|
{ |
121
|
13 |
|
return ucfirst($this->getType()) . ' (line: ' . $this->line . |
122
|
13 |
|
'; position: ' . $this->position . ')'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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 implementation 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 interface: