Completed
Push — master ( 886262...62c01b )
by Kevin
02:18
created

Html   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 54.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 1
cbo 2
dl 18
loc 33
ccs 0
cts 15
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 18 18 4
A getAllowedAttributes() 0 11 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\Configuration;
6
7
class Html extends OpenElement
8
{
9
    protected function getAllowedAttributes()
10
    {
11
        $htmlAllowedAttributes = array(
12
            '/^manifest$/i' => Element::ATTR_CS_STRING
13
        );
14
15
        return array_merge(
16
            $htmlAllowedAttributes,
17
            parent::getAllowedAttributes()
18
        );
19
    }
20
21 View Code Duplication
    public function validate(Configuration $configuration)
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...
22
    {
23
        parent::validate($configuration);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Groundskeeper\Tokens\Elements\OpenElement as the method validate() does only exist in the following sub-classes of Groundskeeper\Tokens\Elements\OpenElement: Groundskeeper\Tokens\Elements\Html. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
24
25
        // If not valid, then we are done.
26
        if (!$this->isValid) {
0 ignored issues
show
Bug introduced by
The property isValid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
            return;
28
        }
29
30
        // If no cleaning, then we are done.
31
        if ($configuration->get('clean-strategy') == 'none') {
32
            return;
33
        }
34
35
        if ($this->getParent() !== null) {
36
            return $this->handleValidationError('Html element must not be nested.');
0 ignored issues
show
Bug introduced by
The method handleValidationError() does not seem to exist on object<Groundskeeper\Tokens\Elements\Html>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
        }
38
    }
39
}
40