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

Link::getAllowedAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 17
ccs 0
cts 16
cp 0
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
class Link extends ClosedElement
6
{
7
    protected function getAllowedAttributes()
8
    {
9
        $linkAllowedAttributes = array(
10
            '/^href$/i' => Element::ATTR_URI,
11
            '/^crossorigin$/i' => Element::ATTR_CS_STRING,
12
            '/^rel$/i' => Element::ATTR_CS_STRING,
13
            '/^media$/i' => Element::ATTR_CS_STRING,
14
            '/^hreflang$/i' => Element::ATTR_CS_STRING,
15
            '/^type$/i' => Element::ATTR_CS_STRING,
16
            '/^sizes$/i' => Element::ATTR_CS_STRING
17
        );
18
19
        return array_merge(
20
            $linkAllowedAttributes,
21
            parent::getAllowedAttributes()
22
        );
23
    }
24
25 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...
26
    {
27
        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...
28
29
        // If not valid, then we are done.
30
        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...
31
            return;
32
        }
33
34
        // If no cleaning, then we are done.
35
        if ($configuration->get('clean-strategy') == 'none') {
36
            return;
37
        }
38
39
        // Must have "href" attribute.
40
        if (!$this->hasAttribute('href')) {
41
            $this->handleValidationError(
0 ignored issues
show
Bug introduced by
The method handleValidationError() does not seem to exist on object<Groundskeeper\Tokens\Elements\Link>.

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...
42
                $configuration,
43
                'Link element must have "href" attribute.'
44
            );
45
        }
46
47
        // Must have either "rel" or "itemprop" attribute, but not both.
48
        /// @todo
49
    }
50
}
51