1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\Attribute; |
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\ClosedElement; |
7
|
|
|
use Groundskeeper\Tokens\ElementTypes\MetadataContent; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
class Meta extends ClosedElement implements MetadataContent |
11
|
|
|
{ |
12
|
7 |
|
protected function getAllowedAttributes() |
13
|
|
|
{ |
14
|
|
|
$metaAllowedAttributes = array( |
15
|
7 |
|
'/^name$/i' => Attribute::CS_STRING, |
16
|
|
|
'/^http-equiv$/i' => Attribute::CI_ENUM . '("content-language","content-type","default-style","refresh","set-cookie","x-ua-compatible","content-security-policy")', |
17
|
|
|
'/^content$/i' => Attribute::CS_STRING, |
18
|
|
|
'/^charset$/i' => Attribute::CI_STRING, |
19
|
|
|
'/^property$/i' => Attribute::CS_STRING // Facebook OG attribute name. |
20
|
|
|
); |
21
|
|
|
|
22
|
7 |
|
return array_merge( |
23
|
7 |
|
$metaAllowedAttributes, |
24
|
7 |
|
parent::getAllowedAttributes() |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
View Code Duplication |
protected function fixSelf(LoggerInterface $logger) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
// "name" attribute requires "content" attribute. |
31
|
3 |
|
if ($this->hasAttribute('name') && !$this->hasAttribute('content')) { |
32
|
1 |
|
$logger->debug('Modifying ' . $this . '. A "meta" element with a "name" attribute requires a "content" attribute. Adding empty "content" attribute.'); |
33
|
1 |
|
$this->addAttribute('content', ''); |
34
|
|
|
} |
35
|
3 |
|
} |
36
|
|
|
|
37
|
3 |
View Code Duplication |
protected function removeInvalidSelf(LoggerInterface $logger) : bool |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
// If "charset" attribute is present, the must be child of "head" element. |
40
|
3 |
|
if ($this->hasAttribute('charset') && $this->getParent() !== null) { |
41
|
1 |
|
if (!$this->getParent() instanceof Head) { |
42
|
1 |
|
$logger->debug('Removing ' . $this . '. Element "meta" with a "charset" attribute must be a "head" element child.'); |
43
|
|
|
|
44
|
1 |
|
return true; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return false; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
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.