Completed
Push — master ( 6e9edd...3f79e5 )
by Kevin
02:25
created

Base::clean()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 0
cts 14
cp 0
rs 8.8571
cc 6
eloc 9
nc 4
nop 1
crap 42
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
7
class Base extends ClosedElement
8
{
9
    protected function getAllowedAttributes()
10
    {
11
        $baseAllowedAttributes = array(
12
            '/^href$/i' => Element::ATTR_URI,
13
            '/^target$/i' => Element::ATTR_CS_STRING
14
        );
15
16
        return array_merge(
17
            $baseAllowedAttributes,
18
            parent::getAllowedAttributes()
19
        );
20
    }
21
22
    /**
23
     * Required by the Cleanable interface.
24
     */
25
    public function clean(LoggerInterface $logger = null)
26
    {
27
        if ($this->configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
28
            return true;
29
        }
30
31
        parent::clean($logger);
32
33
        // "base" must be child of "head".
34
        if ($this->getParent() === null || $this->getParent()->getName() !== 'head') {
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\Elements\Base, Groundskeeper\Tokens\Elements\Body, Groundskeeper\Tokens\Elements\Br, Groundskeeper\Tokens\Elements\ClosedElement, Groundskeeper\Tokens\Elements\Element, Groundskeeper\Tokens\Elements\Head, Groundskeeper\Tokens\Elements\Hr, Groundskeeper\Tokens\Elements\Html, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\OpenElement, Groundskeeper\Tokens\Elements\Style.

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...
35
            return false;
36
        }
37
38
        // Must have either "href" or "target" attribute or both.
39
        if (!$this->hasAttribute('href') && !$this->hasAttribute('target')) {
40
            return false;
41
        }
42
43
        return true;
44
    }
45
}
46