Completed
Push — master ( fef96a...9f8b44 )
by Kevin
03:02
created

Base::doClean()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 9
nc 4
nop 1
crap 6
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Tokens\Element;
7
use Groundskeeper\Tokens\ElementTypes\ClosedElement;
8
use Groundskeeper\Tokens\ElementTypes\MetadataContent;
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * "base" element
13
 */
14
class Base extends ClosedElement implements MetadataContent
15
{
16 4
    protected function getAllowedAttributes()
17
    {
18
        $baseAllowedAttributes = array(
19 4
            '/^href$/i' => Element::ATTR_URI,
20
            '/^target$/i' => Element::ATTR_CS_STRING
21 4
        );
22
23 4
        return array_merge(
24 4
            $baseAllowedAttributes,
25 4
            parent::getAllowedAttributes()
26 4
        );
27
    }
28
29 4
    protected function doClean(LoggerInterface $logger)
30
    {
31 4
        if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
32
            // "base" element must be child of "head" element.
33 4
            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\A, Groundskeeper\Tokens\Elements\Abbr, Groundskeeper\Tokens\Elements\Address, Groundskeeper\Tokens\Elements\Article, Groundskeeper\Tokens\Elements\Aside, 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\Cite, Groundskeeper\Tokens\Elements\Code, 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\Figcaption, Groundskeeper\Tokens\Elements\Figure, 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\I, Groundskeeper\Tokens\Elements\Ins, Groundskeeper\Tokens\Elements\Kbd, Groundskeeper\Tokens\Elements\Li, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Main, Groundskeeper\Tokens\Elements\Mark, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\Nav, Groundskeeper\Tokens\Elements\Noscript, Groundskeeper\Tokens\Elements\Ol, Groundskeeper\Tokens\Elements\P, 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\Span, Groundskeeper\Tokens\Elements\Strong, Groundskeeper\Tokens\Elements\Style, Groundskeeper\Tokens\Elements\Sub, Groundskeeper\Tokens\Elements\Sup, Groundskeeper\Tokens\Elements\Template, Groundskeeper\Tokens\Elements\Time, Groundskeeper\Tokens\Elements\Title, Groundskeeper\Tokens\Elements\U, Groundskeeper\Tokens\Elements\Ul, 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...
34 1
                $logger->debug('Element "base" must be a "head" element child.');
35
36 1
                return false;
37
            }
38
39
            // Must have either "href" or "target" attribute or both.
40 3
            if (!$this->hasAttribute('href') && !$this->hasAttribute('target')) {
41 1
                $logger->debug('Element "base" must have either the "href" or "target" attribute or both.');
42
43 1
                return false;
44
            }
45 2
        }
46
47 4
        return true;
48
    }
49
}
50