Completed
Push — master ( 1ec99b...a17ead )
by Kevin
02:44
created

Head::doClean()   C

Complexity

Conditions 21
Paths 77

Size

Total Lines 67
Code Lines 38

Duplication

Lines 11
Ratio 16.42 %

Code Coverage

Tests 45
CRAP Score 21.2396

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 11
loc 67
ccs 45
cts 49
cp 0.9184
rs 5.7177
cc 21
eloc 38
nc 77
nop 1
crap 21.2396

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\OpenElement;
7
use Groundskeeper\Tokens\ElementTypes\MetadataContent;
8
use Groundskeeper\Tokens\Token;
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * "head" element
13
 */
14
class Head extends OpenElement
15
{
16 16
    protected function doClean(LoggerInterface $logger = null)
17
    {
18
        // "head" element must be a child of "html" element.
19 16
        if ($this->getParent() !== null &&
20 16
            $this->getParent()->getType() === Token::ELEMENT &&
21 16
            $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
            if ($logger !== null) {
23
                $logger->debug('Element "head" must be a child of "html" element.');
24
            }
25
26
            return false;
27
        }
28
29
        // "head" element must contain only metadata content elements.
30
        // "head" element must contain exactly one "title" element.
31
        // "head" element must contain either 0 or 1 "base" element.
32 16
        $titleCount = 0;
33 16
        $baseCount = 0;
34 16
        foreach ($this->children as $child) {
35 16
            if (!$child instanceof MetadataContent &&
36 16
                $child->getType() !== 'comment' &&
37 16
                $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
38 1
                $this->removeChild($child);
39 1
                if ($logger !== null) {
40 1
                    $logger->debug('Removing ' . $child . '. Only children of metadata content allowed.');
41 1
                }
42 1
            }
43
44 16
            if ($child->getType() != Token::ELEMENT) {
45 1
                continue;
46
            }
47
48 16
            if ($child->getName() == 'title') {
49 16
                $titleCount++;
50 16
                if ($titleCount > 1 &&
51 16
                    $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
52 1
                    if ($logger !== null) {
53 1
                        $logger->debug('Removing ' . $child . '. Only one "title" element allowed.');
54 1
                    }
55
56 1
                    $this->removeChild($child);
57 1
                }
58 16 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...
59 1
                $baseCount++;
60 1
                if ($baseCount > 1 &&
61 1
                    $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
62 1
                    if ($logger !== null) {
63 1
                        $logger->debug('Removing ' . $child . '. Maximum one "base" element allowed.');
64 1
                    }
65
66 1
                    $this->removeChild($child);
67 1
                }
68 1
            }
69 16
        }
70
71
        // Missing title.
72 16
        if ($titleCount == 0) {
73 2
            if ($logger !== null) {
74 2
                $logger->debug('Adding "title" element. One "title" element required.');
75 2
            }
76
77 2
            $title = new Title($this->configuration, 'title');
78 2
            $this->prependChild($title);
79 2
        }
80
81 16
        return true;
82
    }
83
}
84