|
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 null|Token */ |
|
20
|
|
|
private $parent; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constructor |
|
24
|
|
|
*/ |
|
25
|
112 |
|
public function __construct(Configuration $configuration) |
|
26
|
|
|
{ |
|
27
|
112 |
|
$this->configuration = $configuration; |
|
28
|
112 |
|
$this->depth = 0; |
|
29
|
112 |
|
$this->parent = null; |
|
30
|
112 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Required by the Token interface. |
|
34
|
|
|
*/ |
|
35
|
84 |
|
public function getDepth() |
|
36
|
|
|
{ |
|
37
|
84 |
|
return $this->depth; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Required by the Token interface. |
|
42
|
|
|
*/ |
|
43
|
54 |
|
public function getParent() |
|
44
|
|
|
{ |
|
45
|
54 |
|
return $this->parent; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Chainable setter for 'parent'. |
|
50
|
|
|
*/ |
|
51
|
83 |
|
public function setParent(Token $parent = null) |
|
52
|
|
|
{ |
|
53
|
83 |
|
$this->depth = 0; |
|
54
|
83 |
|
if ($parent instanceof Token) { |
|
55
|
83 |
|
$this->depth = $parent->getDepth() + 1; |
|
56
|
83 |
|
} |
|
57
|
|
|
|
|
58
|
83 |
|
$this->parent = $parent; |
|
59
|
|
|
|
|
60
|
83 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
5 |
|
public function hasAncestor(Element $element) |
|
64
|
|
|
{ |
|
65
|
5 |
|
if ($this->parent === null) { |
|
66
|
3 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
if ($this->parent->getType() == Token::ELEMENT && |
|
70
|
5 |
|
$this->parent->getName() == $element->getName()) { |
|
|
|
|
|
|
71
|
4 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $this->parent->hasAncestor($element); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
89 |
|
public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger) |
|
78
|
|
|
{ |
|
79
|
89 |
|
if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) { |
|
80
|
76 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
88 |
|
foreach ($children as $key => $child) { |
|
84
|
84 |
|
if ($child instanceof Cleanable) { |
|
85
|
78 |
|
$isClean = $child->clean($logger); |
|
86
|
78 |
|
if (!$isClean && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) { |
|
87
|
19 |
|
unset($children[$key]); |
|
88
|
19 |
|
} |
|
89
|
78 |
|
} |
|
90
|
88 |
|
} |
|
91
|
|
|
|
|
92
|
88 |
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
abstract public function getType(); |
|
96
|
|
|
|
|
97
|
12 |
|
public function __toString() |
|
98
|
|
|
{ |
|
99
|
12 |
|
return ucfirst($this->getType()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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: