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
|
146 |
|
public function __construct(Configuration $configuration, $line, $position) |
32
|
|
|
{ |
33
|
146 |
|
$this->configuration = $configuration; |
34
|
146 |
|
$this->depth = 0; |
35
|
146 |
|
$this->line = $line; |
36
|
146 |
|
$this->parent = null; |
37
|
146 |
|
$this->position = $position; |
38
|
146 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Required by the Token interface. |
42
|
|
|
*/ |
43
|
101 |
|
public function getDepth() |
44
|
|
|
{ |
45
|
101 |
|
return $this->depth; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Getter for 'line'. |
50
|
|
|
*/ |
51
|
71 |
|
public function getLine() |
52
|
|
|
{ |
53
|
71 |
|
return $this->line; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Required by the Token interface. |
58
|
|
|
*/ |
59
|
60 |
|
public function getParent() |
60
|
|
|
{ |
61
|
60 |
|
return $this->parent; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Chainable setter for 'parent'. |
66
|
|
|
*/ |
67
|
100 |
|
public function setParent(Token $parent = null) |
68
|
|
|
{ |
69
|
100 |
|
$this->depth = 0; |
70
|
100 |
|
if ($parent instanceof Token) { |
71
|
100 |
|
$this->depth = $parent->getDepth() + 1; |
72
|
100 |
|
} |
73
|
|
|
|
74
|
100 |
|
$this->parent = $parent; |
75
|
|
|
|
76
|
100 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
14 |
|
public function hasAncestor(Element $element) |
80
|
|
|
{ |
81
|
14 |
|
if ($this->parent === null) { |
82
|
12 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
13 |
|
if ($this->parent->getType() == Token::ELEMENT && |
86
|
13 |
|
$this->parent->getName() == $element->getName()) { |
|
|
|
|
87
|
7 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
11 |
|
return $this->parent->hasAncestor($element); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Getter for 'position'. |
95
|
|
|
*/ |
96
|
71 |
|
public function getPosition() |
97
|
|
|
{ |
98
|
71 |
|
return $this->position; |
99
|
|
|
} |
100
|
|
|
|
101
|
109 |
|
public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger) |
102
|
|
|
{ |
103
|
109 |
|
if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) { |
104
|
96 |
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
108 |
|
foreach ($children as $key => $child) { |
108
|
104 |
|
if ($child instanceof Cleanable) { |
109
|
98 |
|
$isClean = $child->clean($logger); |
110
|
98 |
|
if (!$isClean && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) { |
111
|
23 |
|
unset($children[$key]); |
112
|
23 |
|
} |
113
|
98 |
|
} |
114
|
108 |
|
} |
115
|
|
|
|
116
|
108 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
abstract public function getType(); |
120
|
|
|
|
121
|
12 |
|
public function __toString() |
122
|
|
|
{ |
123
|
12 |
|
return ucfirst($this->getType()) . ' (line: ' . $this->line . |
124
|
12 |
|
'; position: ' . $this->position . ')'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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: