Completed
Push — master ( 35004c...f59553 )
by Kevin
02:42
created

Head::doClean()   C

Complexity

Conditions 16
Paths 27

Size

Total Lines 54
Code Lines 33

Duplication

Lines 17
Ratio 31.48 %

Code Coverage

Tests 39
CRAP Score 16

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 17
loc 54
ccs 39
cts 39
cp 1
rs 6.6856
cc 16
eloc 33
nc 27
nop 1
crap 16

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 &&
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...
20 18
            $this->getParent()->getType() === Token::ELEMENT &&
21 18
            $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\Article, Groundskeeper\Tokens\Elements\Aside, Groundskeeper\Tokens\Elements\Base, Groundskeeper\Tokens\Elements\Body, Groundskeeper\Tokens\Elements\Br, Groundskeeper\Tokens\Elements\Footer, 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\Link, Groundskeeper\Tokens\Elements\Main, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\Nav, Groundskeeper\Tokens\Elements\Noscript, Groundskeeper\Tokens\Elements\Script, Groundskeeper\Tokens\Elements\Section, Groundskeeper\Tokens\Elements\Style, Groundskeeper\Tokens\Elements\Template, Groundskeeper\Tokens\Elements\Title.

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...
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 &&
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...
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 &&
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...
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