|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
|
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
|
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\MetadataContent; |
|
8
|
|
|
use Groundskeeper\Tokens\Token; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* "head" element |
|
13
|
|
|
*/ |
|
14
|
|
|
class Head extends OpenElement |
|
15
|
|
|
{ |
|
16
|
16 |
|
protected function doClean(LoggerInterface $logger = null) |
|
17
|
|
|
{ |
|
18
|
|
|
// "head" element must be a child of "html" element. |
|
19
|
16 |
|
if ($this->getParent() !== null && |
|
20
|
16 |
|
$this->getParent()->getType() === Token::ELEMENT && |
|
21
|
16 |
|
$this->getParent()->getName() != 'html') { |
|
|
|
|
|
|
22
|
|
|
if ($logger !== null) { |
|
23
|
|
|
$logger->debug('Element "head" must be a child of "html" element.'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// "head" element must contain only metadata content elements. |
|
30
|
|
|
// "head" element must contain exactly one "title" element. |
|
31
|
|
|
// "head" element must contain either 0 or 1 "base" element. |
|
32
|
16 |
|
$titleCount = 0; |
|
33
|
16 |
|
$baseCount = 0; |
|
34
|
16 |
|
foreach ($this->children as $child) { |
|
35
|
16 |
|
if (!$child instanceof MetadataContent && |
|
36
|
16 |
|
$child->getType() !== 'comment' && |
|
37
|
16 |
|
$this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) { |
|
38
|
1 |
|
$this->removeChild($child); |
|
39
|
1 |
|
if ($logger !== null) { |
|
40
|
1 |
|
$logger->debug('Removing ' . $child . '. Only children of metadata content allowed.'); |
|
41
|
1 |
|
} |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
16 |
|
if ($child->getType() != Token::ELEMENT) { |
|
45
|
1 |
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
16 |
|
if ($child->getName() == 'title') { |
|
49
|
16 |
|
$titleCount++; |
|
50
|
16 |
|
if ($titleCount > 1 && |
|
51
|
16 |
|
$this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) { |
|
52
|
1 |
|
if ($logger !== null) { |
|
53
|
1 |
|
$logger->debug('Removing ' . $child . '. Only one "title" element allowed.'); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$this->removeChild($child); |
|
57
|
1 |
|
} |
|
58
|
16 |
View Code Duplication |
} elseif ($child->getName() == 'base') { |
|
|
|
|
|
|
59
|
1 |
|
$baseCount++; |
|
60
|
1 |
|
if ($baseCount > 1 && |
|
61
|
1 |
|
$this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) { |
|
62
|
1 |
|
if ($logger !== null) { |
|
63
|
1 |
|
$logger->debug('Removing ' . $child . '. Maximum one "base" element allowed.'); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$this->removeChild($child); |
|
67
|
1 |
|
} |
|
68
|
1 |
|
} |
|
69
|
16 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Missing title. |
|
72
|
16 |
|
if ($titleCount == 0) { |
|
73
|
2 |
|
if ($logger !== null) { |
|
74
|
2 |
|
$logger->debug('Adding "title" element. One "title" element required.'); |
|
75
|
2 |
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
$title = new Title($this->configuration, 'title'); |
|
78
|
2 |
|
$this->prependChild($title); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
16 |
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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: