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