Completed
Push — master ( 3f79e5...1ec99b )
by Kevin
02:42
created

Base::doClean()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.9256

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 4
cts 6
cp 0.6667
rs 8.8571
cc 5
eloc 6
nc 3
nop 1
crap 5.9256
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Tokens\Element;
6
use Groundskeeper\Tokens\ElementTypes\ClosedElement;
7
use Groundskeeper\Tokens\ElementTypes\MetadataContent;
8
use Psr\Log\LoggerInterface;
9
10
class Base extends ClosedElement implements MetadataContent
11
{
12 1
    protected function getAllowedAttributes()
13
    {
14
        $baseAllowedAttributes = array(
15 1
            '/^href$/i' => Element::ATTR_URI,
16
            '/^target$/i' => Element::ATTR_CS_STRING
17 1
        );
18
19 1
        return array_merge(
20 1
            $baseAllowedAttributes,
21 1
            parent::getAllowedAttributes()
22 1
        );
23
    }
24
25 1
    protected function doClean(LoggerInterface $logger = null)
26
    {
27
        // BASE must be child of HEAD.
28 1
        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\Element, Groundskeeper\Tokens\ElementTypes\ClosedElement, Groundskeeper\Tokens\ElementTypes\OpenElement, Groundskeeper\Tokens\Elements\Base, Groundskeeper\Tokens\Elements\Body, Groundskeeper\Tokens\Elements\Br, Groundskeeper\Tokens\Elements\Head, Groundskeeper\Tokens\Elements\Hr, Groundskeeper\Tokens\Elements\Html, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\Noscript, Groundskeeper\Tokens\Elements\Script, 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...
29
            return false;
30
        }
31
32
        // Must have either "href" or "target" attribute or both.
33 1
        if (!$this->hasAttribute('href') && !$this->hasAttribute('target')) {
34
            return false;
35
        }
36
37 1
        return true;
38
    }
39
}
40