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