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