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

Base::doClean()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 7
Ratio 31.82 %

Code Coverage

Tests 4
CRAP Score 21.5206

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 7
loc 22
ccs 4
cts 12
cp 0.3333
rs 6.9811
cc 7
eloc 10
nc 5
nop 1
crap 21.5206
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
/**
11
 * "base" element
12
 */
13
class Base extends ClosedElement implements MetadataContent
14
{
15 1
    protected function getAllowedAttributes()
16
    {
17
        $baseAllowedAttributes = array(
18 1
            '/^href$/i' => Element::ATTR_URI,
19
            '/^target$/i' => Element::ATTR_CS_STRING
20 1
        );
21
22 1
        return array_merge(
23 1
            $baseAllowedAttributes,
24 1
            parent::getAllowedAttributes()
25 1
        );
26
    }
27
28 1
    protected function doClean(LoggerInterface $logger = null)
29
    {
30
        // "base" element must be child of "head" element.
31 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\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...
32
            if ($logger !== null) {
33
                $logger->debug('Element "base" must be a "head" element child.');
34
            }
35
36
            return false;
37
        }
38
39
        // Must have either "href" or "target" attribute or both.
40 1 View Code Duplication
        if (!$this->hasAttribute('href') && !$this->hasAttribute('target')) {
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...
41
            if ($logger !== null) {
42
                $logger->debug('Element "base" must have either the "href" or "target" attribute or both.');
43
            }
44
45
            return false;
46
        }
47
48 1
        return true;
49
    }
50
}
51