Completed
Push — master ( 9d450b...0b908e )
by Kevin
02:19
created

Base::validate()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
ccs 0
cts 0
cp 0
rs 6.7272
cc 7
eloc 14
nc 6
nop 1
crap 56
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
7
class Base extends ClosedElement
8
{
9
    protected function getAllowedAttrbutes()
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::getAllowedAttrbutes()
19
        );
20
    }
21
22
    public function validate(Configuration $configuration)
23
    {
24
        parent::validate($configuration);
25
26
        // If not valid, then we are done.
27
        if (!$this->isValid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isValid of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
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(
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(
47
                $configuration,
48
                'Base element must have either "href" or "target" attribute or both.'
49
            );
50
        }
51
    }
52
}
53