1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\MetadataContent; |
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
8
|
|
|
use Groundskeeper\Tokens\Token; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* "head" element |
13
|
|
|
* |
14
|
|
|
* https://html.spec.whatwg.org/multipage/semantics.html#the-head-element |
15
|
|
|
*/ |
16
|
|
|
class Head extends OpenElement |
17
|
|
|
{ |
18
|
31 |
|
protected function fixSelf(LoggerInterface $logger) |
19
|
|
|
{ |
20
|
|
|
// Look for "title" element |
21
|
31 |
|
foreach ($this->children as $child) { |
22
|
31 |
|
if ($child instanceof Title) { |
23
|
31 |
|
return; |
24
|
|
|
} |
25
|
5 |
|
} |
26
|
|
|
|
27
|
|
|
// Missing title. |
28
|
3 |
|
$logger->debug('Adding "title" element. One "title" element required.'); |
29
|
3 |
|
$title = new Title($this->configuration, 'title'); |
30
|
3 |
|
$this->prependChild($title); |
31
|
3 |
|
} |
32
|
|
|
|
33
|
30 |
|
protected function removeInvalidChildren(LoggerInterface $logger) |
34
|
|
|
{ |
35
|
|
|
// "head" element must contain only metadata content elements. |
36
|
|
|
// "head" element must contain exactly one "title" element. |
37
|
|
|
// "head" element must contain either 0 or 1 "base" element. |
38
|
30 |
|
$titleCount = 0; |
39
|
30 |
|
$baseCount = 0; |
40
|
30 |
|
foreach ($this->children as $child) { |
41
|
30 |
|
if ($child->getType() == Token::COMMENT) { |
42
|
1 |
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
30 |
View Code Duplication |
if (!$child instanceof MetadataContent) { |
|
|
|
|
46
|
1 |
|
$logger->debug('Removing ' . $child . '. Only children of metadata content allowed.'); |
47
|
1 |
|
$this->removeChild($child); |
48
|
|
|
|
49
|
1 |
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
30 |
|
if ($child->getType() != Token::ELEMENT) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
30 |
|
if ($child->getName() == 'title') { |
57
|
30 |
|
++$titleCount; |
58
|
30 |
|
if ($titleCount > 1) { |
59
|
1 |
|
$logger->debug('Removing ' . $child . '. Only one "title" element allowed.'); |
60
|
1 |
|
$this->removeChild($child); |
|
|
|
|
61
|
|
|
|
62
|
1 |
|
continue; |
63
|
|
|
} |
64
|
30 |
View Code Duplication |
} elseif ($child->getName() == 'base') { |
|
|
|
|
65
|
3 |
|
++$baseCount; |
66
|
3 |
|
if ($baseCount > 1) { |
67
|
1 |
|
$logger->debug('Removing ' . $child . '. Maximum one "base" element allowed.'); |
68
|
|
|
|
69
|
1 |
|
$this->removeChild($child); |
|
|
|
|
70
|
|
|
|
71
|
1 |
|
continue; |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
30 |
|
} |
75
|
30 |
|
} |
76
|
|
|
|
77
|
30 |
View Code Duplication |
protected function removeInvalidSelf(LoggerInterface $logger) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
// "head" element must be a child of "html" element. |
80
|
30 |
|
if ($this->getParent() !== null && |
81
|
30 |
|
$this->getParent()->getType() === Token::ELEMENT && |
82
|
30 |
|
$this->getParent()->getName() != 'html') { |
|
|
|
|
83
|
|
|
$logger->debug($this . ' must be a child of "html" element.'); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
30 |
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.