Passed
Push — master ( 20e747...679572 )
by Ivan
01:59
created

ManualVoter::isMatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Everlution\Navigation\Voter;
6
7
use Everlution\Navigation\NavigationItem;
8
use Everlution\Navigation\Matcher\VoterContainer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Everlution\Navigation\Voter\VoterContainer.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Everlution\Navigation\Extension\ManualMatch;
10
11
/**
12
 * Class ManualVoter.
13
 * @author Ivan Barlog <[email protected]>
14
 */
15
class ManualVoter extends VoterContainer implements Voter
16
{
17
    /**
18
     * @param NavigationItem $item
19
     * @return bool
20
     */
21
    public function match(NavigationItem $item): bool
22
    {
23
        if (!$item instanceof ManualMatch) {
24
            return false;
25
        }
26
27
        /** @var StringVoter $voter */
28
        foreach ($this->getVoters() as $voter) {
29
            if ($this->isMatch($item, $voter)) {
0 ignored issues
show
Documentation introduced by
$voter is of type object<Everlution\Navigation\Voter\Voter>, but the function expects a object<Everlution\Navigation\Voter\StringVoter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
                return true;
31
            }
32
        }
33
34
        return false;
35
    }
36
37
    /**
38
     * @param ManualMatch $item
39
     * @param StringVoter $voter
40
     * @return bool
41
     */
42
    private function isMatch(ManualMatch $item, StringVoter &$voter): bool
43
    {
44
        foreach ($item->getMatches() as $match) {
45
            if ($voter->matchString($match)) {
46
                return true;
47
            }
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * @param Voter $voter
55
     * @return VoterContainer
56
     * @throws VoterAlreadyRegisteredException
57
     */
58
    public function addVoter(Voter $voter)
59
    {
60
        if (!$voter instanceof StringVoter) {
61
            throw new \InvalidArgumentException(sprintf('Voter must implement %s interface', StringVoter::class));
62
        }
63
64
        return parent::addVoter($voter);
65
    }
66
}
67