Completed
Push — master ( a17ead...35004c )
by Kevin
02:36
created

Body::doClean()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 7
Ratio 53.85 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 7
loc 13
ccs 4
cts 8
cp 0.5
rs 9.2
cc 4
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Tokens\Element;
6
use Groundskeeper\Tokens\ElementTypes\OpenElement;
7
use Groundskeeper\Tokens\ElementTypes\SectioningRoot;
8
use Groundskeeper\Tokens\Token;
9
use Psr\Log\LoggerInterface;
10
11
class Body extends OpenElement implements SectioningRoot
12
{
13
    protected function getAllowedAttributes()
14
    {
15
        $bodyAllowedAttributes = array(
16
            '/^onafterprint$/i' => Element::ATTR_JS,
17
            '/^onbeforeprint$/i' => Element::ATTR_JS,
18
            '/^onbeforeunload$/i' => Element::ATTR_JS,
19
            '/^onhashchange$/i' => Element::ATTR_JS,
20
            '/^onlanguagechange$/i' => Element::ATTR_JS,
21
            '/^onmessage$/i' => Element::ATTR_JS,
22
            '/^onoffline$/i' => Element::ATTR_JS,
23
            '/^ononline$/i' => Element::ATTR_JS,
24
            '/^onpagehide$/i' => Element::ATTR_JS,
25
            '/^onpageshow$/i' => Element::ATTR_JS,
26
            '/^onpopstate$/i' => Element::ATTR_JS,
27
            '/^onrejectionhandled$/i' => Element::ATTR_JS,
28
            '/^onstorage$/i' => Element::ATTR_JS,
29
            '/^onunhandledrejection$/i' => Element::ATTR_JS,
30
            '/^onunload$/i' => Element::ATTR_JS
31
        );
32
33
        return array_merge(
34
            $bodyAllowedAttributes,
35
            parent::getAllowedAttributes()
36
        );
37
    }
38 16
39
    protected function doClean(LoggerInterface $logger)
40
    {
41 16
        // "body" element must be a child of "html" element.
42 16 View Code Duplication
        if ($this->getParent() !== null &&
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...
43 16
            $this->getParent()->getType() === Token::ELEMENT &&
44
            $this->getParent()->getName() != 'html') {
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...
45
            $logger->debug('Element "body" must be a child of "html" element.');
46
47
            return false;
48
        }
49
50
        return true;
51 16
    }
52
}
53