Completed
Push — master ( 9fbe45...817727 )
by Kevin
03:24
created

Body   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 27.08 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 86.21%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 6
c 4
b 0
f 3
lcom 0
cbo 3
dl 13
loc 48
ccs 25
cts 29
cp 0.8621
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getAllowedAttributes() 0 25 1
A removeInvalidSelf() 13 13 4
A doClean() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
/**
12
 * "body" element
13
 *
14
 * https://html.spec.whatwg.org/multipage/semantics.html#the-body-element
15
 */
16
class Body extends OpenElement implements SectioningRoot
17
{
18 1
    protected function getAllowedAttributes()
19
    {
20
        $bodyAllowedAttributes = array(
21 1
            '/^onafterprint$/i' => Element::ATTR_JS,
22 1
            '/^onbeforeprint$/i' => Element::ATTR_JS,
23 1
            '/^onbeforeunload$/i' => Element::ATTR_JS,
24 1
            '/^onhashchange$/i' => Element::ATTR_JS,
25 1
            '/^onlanguagechange$/i' => Element::ATTR_JS,
26 1
            '/^onmessage$/i' => Element::ATTR_JS,
27 1
            '/^onoffline$/i' => Element::ATTR_JS,
28 1
            '/^ononline$/i' => Element::ATTR_JS,
29 1
            '/^onpagehide$/i' => Element::ATTR_JS,
30 1
            '/^onpageshow$/i' => Element::ATTR_JS,
31 1
            '/^onpopstate$/i' => Element::ATTR_JS,
32 1
            '/^onrejectionhandled$/i' => Element::ATTR_JS,
33 1
            '/^onstorage$/i' => Element::ATTR_JS,
34 1
            '/^onunhandledrejection$/i' => Element::ATTR_JS,
35
            '/^onunload$/i' => Element::ATTR_JS
36 1
        );
37
38 1
        return array_merge(
39 1
            $bodyAllowedAttributes,
40 1
            parent::getAllowedAttributes()
41 1
        );
42
    }
43
44 33 View Code Duplication
    protected function removeInvalidSelf(LoggerInterface $logger)
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...
45
    {
46
        // "body" element must be a child of "html" element.
47 33
        if ($this->getParent() !== null &&
48 33
            $this->getParent()->getType() === Token::ELEMENT &&
49 33
            $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\A, Groundskeeper\Tokens\Elements\Abbr, Groundskeeper\Tokens\Elements\Address, Groundskeeper\Tokens\Elements\Area, Groundskeeper\Tokens\Elements\Article, Groundskeeper\Tokens\Elements\Aside, Groundskeeper\Tokens\Elements\Audio, 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\Caption, Groundskeeper\Tokens\Elements\Cite, Groundskeeper\Tokens\Elements\Code, Groundskeeper\Tokens\Elements\Col, Groundskeeper\Tokens\Elements\Colgroup, 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\Embed, Groundskeeper\Tokens\Elements\Figcaption, Groundskeeper\Tokens\Elements\Figure, Groundskeeper\Tokens\Elements\Footer, Groundskeeper\Tokens\Elements\Form, 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\Iframe, Groundskeeper\Tokens\Elements\Img, Groundskeeper\Tokens\Elements\Ins, Groundskeeper\Tokens\Elements\Kbd, Groundskeeper\Tokens\Elements\Label, Groundskeeper\Tokens\Elements\Li, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Main, Groundskeeper\Tokens\Elements\Map, Groundskeeper\Tokens\Elements\Mark, Groundskeeper\Tokens\Elements\Menu, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\Nav, Groundskeeper\Tokens\Elements\Noscript, Groundskeeper\Tokens\Elements\Object, Groundskeeper\Tokens\Elements\Ol, Groundskeeper\Tokens\Elements\P, Groundskeeper\Tokens\Elements\Param, Groundskeeper\Tokens\Elements\Picture, 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\Source, Groundskeeper\Tokens\Elements\Span, Groundskeeper\Tokens\Elements\Strong, Groundskeeper\Tokens\Elements\Style, Groundskeeper\Tokens\Elements\Sub, Groundskeeper\Tokens\Elements\Sup, Groundskeeper\Tokens\Elements\Table, Groundskeeper\Tokens\Elements\Tbody, Groundskeeper\Tokens\Elements\Td, Groundskeeper\Tokens\Elements\Template, Groundskeeper\Tokens\Elements\Tfoot, Groundskeeper\Tokens\Elements\Th, Groundskeeper\Tokens\Elements\Thead, Groundskeeper\Tokens\Elements\Time, Groundskeeper\Tokens\Elements\Title, Groundskeeper\Tokens\Elements\Tr, Groundskeeper\Tokens\Elements\Track, Groundskeeper\Tokens\Elements\U, Groundskeeper\Tokens\Elements\Ul, Groundskeeper\Tokens\Elements\VarElement, Groundskeeper\Tokens\Elements\Video, 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...
50
            $logger->debug($this . ' must be a child of "html" element.');
51
52
            return true;
53
        }
54
55 33
        return false;
56
    }
57
58
    protected function doClean(LoggerInterface $logger)
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
61
        return true;
62
    }
63
}
64