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

Base   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 8
c 4
b 1
f 2
lcom 1
cbo 2
dl 0
loc 46
ccs 0
cts 33
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validate() 0 30 7
A getAllowedAttributes() 0 12 1
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
7
class Base extends ClosedElement
8
{
9
    protected function getAllowedAttributes()
10
    {
11
        $baseAllowedAttributes = array(
12
            '/^href$/i' => Element::ATTR_URI,
13
            '/^target$/i' => Element::ATTR_CS_STRING
14
        );
15
16
        return array_merge(
17
            $baseAllowedAttributes,
18
            parent::getAllowedAttributes()
19
        );
20
    }
21
22
    public function validate(Configuration $configuration)
23
    {
24
        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\ClosedElement as the method validate() does only exist in the following sub-classes of Groundskeeper\Tokens\Elements\ClosedElement: Groundskeeper\Tokens\Elements\Base, Groundskeeper\Tokens\Elements\Link. 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...
25
26
        // If not valid, then we are done.
27
        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...
28
            return;
29
        }
30
31
        // If no cleaning, then we are done.
32
        if ($configuration->get('clean-strategy') == 'none') {
33
            return;
34
        }
35
36
        // "base" must be child of "head".
37
        if ($this->getParent() === null || $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\Elements\Base, Groundskeeper\Tokens\Elements\Br, Groundskeeper\Tokens\Elements\ClosedElement, Groundskeeper\Tokens\Elements\Element, Groundskeeper\Tokens\Elements\Hr, Groundskeeper\Tokens\Elements\Html, Groundskeeper\Tokens\Elements\Link, Groundskeeper\Tokens\Elements\Meta, Groundskeeper\Tokens\Elements\OpenElement, Groundskeeper\Tokens\Elements\Style.

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...
38
            $this->handleValidationError(
0 ignored issues
show
Bug introduced by
The method handleValidationError() does not seem to exist on object<Groundskeeper\Tokens\Elements\Base>.

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...
39
                $configuration,
40
                'Base element must be a child of a "head" element.'
41
            );
42
        }
43
44
        // Must have either "href" or "target" attribute or both.
45
        if (!$this->hasAttribute('href') && !$this->hasAttribute('target')) {
46
            $this->handleValidationError(
0 ignored issues
show
Bug introduced by
The method handleValidationError() does not seem to exist on object<Groundskeeper\Tokens\Elements\Base>.

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...
47
                $configuration,
48
                'Base element must have either "href" or "target" attribute or both.'
49
            );
50
        }
51
    }
52
}
53