Completed
Push — master ( f59553...fef96a )
by Kevin
02:59
created

Meta   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 57.89 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 4
Metric Value
wmc 8
c 6
b 1
f 4
lcom 1
cbo 3
dl 22
loc 38
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 15 15 1
B doClean() 7 19 7

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\Configuration;
6
use Groundskeeper\Tokens\Element;
7
use Groundskeeper\Tokens\ElementTypes\ClosedElement;
8
use Groundskeeper\Tokens\ElementTypes\MetadataContent;
9
use Groundskeeper\Tokens\Token;
10
use Psr\Log\LoggerInterface;
11
12
class Meta extends ClosedElement implements MetadataContent
13
{
14 3 View Code Duplication
    protected function getAllowedAttributes()
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...
15
    {
16
        $metaAllowedAttributes = array(
17 3
            '/^name$/i' => Element::ATTR_CS_STRING,
18 3
            '/^http-equiv$/i' => Element::ATTR_CI_ENUM . '("content-language","content-type","default-style","refresh","set-cookie","x-ua-compatible","content-security-policy")',
19 3
            '/^content$/i' => Element::ATTR_CS_STRING,
20 3
            '/^charset$/i' => Element::ATTR_CI_STRING,
21
            '/^property$/i' => Element::ATTR_CS_STRING  // Facebook OG attribute name.
22 3
        );
23
24 3
        return array_merge(
25 3
            $metaAllowedAttributes,
26 3
            parent::getAllowedAttributes()
27 3
        );
28
    }
29
30 3
    protected function doClean(LoggerInterface $logger)
31
    {
32
        // If "charset" attribute is present, the must be child of "head" element.
33 3 View Code Duplication
        if ($this->hasAttribute('charset') && $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...
34 1
            if ($this->getParent()->getType() !== Token::ELEMENT || $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\Address, Groundskeeper\Tokens\Elements\Article, Groundskeeper\Tokens\Elements\Aside, Groundskeeper\Tokens\Elements\Base, Groundskeeper\Tokens\Elements\Blockquote, 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\Li, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Main, 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\Script, Groundskeeper\Tokens\Elements\Section, Groundskeeper\Tokens\Elements\Style, Groundskeeper\Tokens\Elements\Template, Groundskeeper\Tokens\Elements\Title, Groundskeeper\Tokens\Elements\Ul.

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...
35 1
                $logger->debug('Element "meta" with a "charset" attribute must be a "head" element child.');
36
37 1
                return false;
38
            }
39 1
        }
40
41
        // "name" attribute requires "content" attribute.
42 3
        if ($this->hasAttribute('name') && !$this->hasAttribute('content')) {
43 1
            $logger->debug('A "meta" element with a "name" attribute requires a "content" attribute.  Adding empty "content" attribute.');
44 1
            $this->addAttribute('content', '');
45 1
        }
46
47 3
        return true;
48
    }
49
}
50