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

Body::doClean()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 7.1941

Importance

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