Completed
Push — master ( 817727...d8e3ee )
by Kevin
03:58
created

AbstractToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 7
Bugs 2 Features 4
Metric Value
c 7
b 2
f 4
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
use Psr\Log\LoggerInterface;
7
8
/**
9
 * A base class for all tokens.
10
 */
11
abstract class AbstractToken implements Token
12
{
13
    /** @var Configuration */
14
    protected $configuration;
15
16
    /** @var int */
17
    private $depth;
18
19
    /** @var null|Token */
20
    private $parent;
21
22
    /**
23
     * Constructor
24
     */
25 112
    public function __construct(Configuration $configuration)
26
    {
27 112
        $this->configuration = $configuration;
28 112
        $this->depth = 0;
29 112
        $this->parent = null;
30 112
    }
31
32
    /**
33
     * Required by the Token interface.
34
     */
35 84
    public function getDepth()
36
    {
37 84
        return $this->depth;
38
    }
39
40
    /**
41
     * Required by the Token interface.
42
     */
43 54
    public function getParent()
44
    {
45 54
        return $this->parent;
46
    }
47
48
    /**
49
     * Chainable setter for 'parent'.
50
     */
51 83
    public function setParent(Token $parent = null)
52
    {
53 83
        $this->depth = 0;
54 83
        if ($parent instanceof Token) {
55 83
            $this->depth = $parent->getDepth() + 1;
56 83
        }
57
58 83
        $this->parent = $parent;
59
60 83
        return $this;
61
    }
62
63 5
    public function hasAncestor(Element $element)
64
    {
65 5
        if ($this->parent === null) {
66 3
            return false;
67
        }
68
69 5
        if ($this->parent->getType() == Token::ELEMENT &&
70 5
            $this->parent->getName() == $element->getName()) {
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...
71 4
            return true;
72
        }
73
74 3
        return $this->parent->hasAncestor($element);
75
    }
76
77 89
    public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger)
78
    {
79 89
        if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
80 76
            return true;
81
        }
82
83 88
        foreach ($children as $key => $child) {
84 84
            if ($child instanceof Cleanable) {
85 78
                $isClean = $child->clean($logger);
86 78
                if (!$isClean && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
87 19
                    unset($children[$key]);
88 19
                }
89 78
            }
90 88
        }
91
92 88
        return true;
93
    }
94
95
    abstract public function getType();
96
97 12
    public function __toString()
98
    {
99 12
        return ucfirst($this->getType());
100
    }
101
}
102