Completed
Push — master ( 9fbe45...817727 )
by Kevin
03:24
created

Head::removeInvalidChildren()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 43
Code Lines 24

Duplication

Lines 16
Ratio 37.21 %

Code Coverage

Tests 26
CRAP Score 9.0041

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 43
ccs 26
cts 27
cp 0.963
rs 4.909
cc 9
eloc 24
nc 9
nop 1
crap 9.0041
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$child is of type object<Groundskeeper\Tok...tTypes\MetadataContent>, but the function expects a object<Groundskeeper\Tokens\Token>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
62 1
                    continue;
63
                }
64 30 View Code Duplication
            } elseif ($child->getName() == 'base') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$child is of type object<Groundskeeper\Tok...tTypes\MetadataContent>, but the function expects a object<Groundskeeper\Tokens\Token>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
71 1
                    continue;
72
                }
73 3
            }
74 30
        }
75 30
    }
76
77 30 View Code Duplication
    protected function removeInvalidSelf(LoggerInterface $logger)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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') {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Groundskeeper\Tokens\Token as the method getName() does only exist in the following implementations of said interface: Groundskeeper\Tokens\Element, Groundskeeper\Tokens\ElementTypes\ClosedElement, Groundskeeper\Tokens\ElementTypes\OpenElement, Groundskeeper\Tokens\Elements\A, Groundskeeper\Tokens\Elements\Abbr, Groundskeeper\Tokens\Elements\Address, Groundskeeper\Tokens\Elements\Area, Groundskeeper\Tokens\Elements\Article, Groundskeeper\Tokens\Elements\Aside, Groundskeeper\Tokens\Elements\Audio, Groundskeeper\Tokens\Elements\B, Groundskeeper\Tokens\Elements\Base, Groundskeeper\Tokens\Elements\Bdi, Groundskeeper\Tokens\Elements\Bdo, Groundskeeper\Tokens\Elements\Blockquote, Groundskeeper\Tokens\Elements\Body, Groundskeeper\Tokens\Elements\Br, Groundskeeper\Tokens\Elements\Caption, Groundskeeper\Tokens\Elements\Cite, Groundskeeper\Tokens\Elements\Code, Groundskeeper\Tokens\Elements\Col, Groundskeeper\Tokens\Elements\Colgroup, Groundskeeper\Tokens\Elements\Data, Groundskeeper\Tokens\Elements\Dd, Groundskeeper\Tokens\Elements\Del, Groundskeeper\Tokens\Elements\Dfn, Groundskeeper\Tokens\Elements\Div, Groundskeeper\Tokens\Elements\Dl, Groundskeeper\Tokens\Elements\Dt, Groundskeeper\Tokens\Elements\Em, Groundskeeper\Tokens\Elements\Embed, Groundskeeper\Tokens\Elements\Figcaption, Groundskeeper\Tokens\Elements\Figure, Groundskeeper\Tokens\Elements\Footer, Groundskeeper\Tokens\Elements\Form, Groundskeeper\Tokens\Elements\H1, Groundskeeper\Tokens\Elements\H2, Groundskeeper\Tokens\Elements\H3, Groundskeeper\Tokens\Elements\H4, Groundskeeper\Tokens\Elements\H5, Groundskeeper\Tokens\Elements\H6, Groundskeeper\Tokens\Elements\Head, Groundskeeper\Tokens\Elements\Header, Groundskeeper\Tokens\Elements\Hgroup, Groundskeeper\Tokens\Elements\Hr, Groundskeeper\Tokens\Elements\Html, Groundskeeper\Tokens\Elements\I, Groundskeeper\Tokens\Elements\Iframe, Groundskeeper\Tokens\Elements\Img, Groundskeeper\Tokens\Elements\Ins, Groundskeeper\Tokens\Elements\Kbd, Groundskeeper\Tokens\Elements\Label, Groundskeeper\Tokens\Elements\Li, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Main, Groundskeeper\Tokens\Elements\Map, Groundskeeper\Tokens\Elements\Mark, Groundskeeper\Tokens\Elements\Menu, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\Nav, Groundskeeper\Tokens\Elements\Noscript, Groundskeeper\Tokens\Elements\Object, Groundskeeper\Tokens\Elements\Ol, Groundskeeper\Tokens\Elements\P, Groundskeeper\Tokens\Elements\Param, Groundskeeper\Tokens\Elements\Picture, Groundskeeper\Tokens\Elements\Pre, Groundskeeper\Tokens\Elements\Q, Groundskeeper\Tokens\Elements\Rp, Groundskeeper\Tokens\Elements\Rt, Groundskeeper\Tokens\Elements\Ruby, Groundskeeper\Tokens\Elements\S, Groundskeeper\Tokens\Elements\Samp, Groundskeeper\Tokens\Elements\Script, Groundskeeper\Tokens\Elements\Section, Groundskeeper\Tokens\Elements\Small, Groundskeeper\Tokens\Elements\Source, Groundskeeper\Tokens\Elements\Span, Groundskeeper\Tokens\Elements\Strong, Groundskeeper\Tokens\Elements\Style, Groundskeeper\Tokens\Elements\Sub, Groundskeeper\Tokens\Elements\Sup, Groundskeeper\Tokens\Elements\Table, Groundskeeper\Tokens\Elements\Tbody, Groundskeeper\Tokens\Elements\Td, Groundskeeper\Tokens\Elements\Template, Groundskeeper\Tokens\Elements\Tfoot, Groundskeeper\Tokens\Elements\Th, Groundskeeper\Tokens\Elements\Thead, Groundskeeper\Tokens\Elements\Time, Groundskeeper\Tokens\Elements\Title, Groundskeeper\Tokens\Elements\Tr, Groundskeeper\Tokens\Elements\Track, Groundskeeper\Tokens\Elements\U, Groundskeeper\Tokens\Elements\Ul, Groundskeeper\Tokens\Elements\VarElement, Groundskeeper\Tokens\Elements\Video, Groundskeeper\Tokens\Elements\Wbr.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
83
            $logger->debug($this . ' must be a child of "html" element.');
84
85
            return true;
86
        }
87
88 30
        return false;
89
    }
90
}
91