ComparativeUrl::getMinCounterValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
namespace Malezha\Menu\Support;
3
4
use Illuminate\Contracts\Routing\UrlGenerator;
5
use Malezha\Menu\Contracts\ComparativeUrl as ComparativeUrlContract;
6
7
class ComparativeUrl implements ComparativeUrlContract
8
{
9
    /**
10
     * @var UrlGenerator
11
     */
12
    protected $urlGenerator;
13
14
    /**
15
     * @var array
16
     */
17
    protected $skippedPaths = [];
18
19
    /**
20
     * @var array
21
     */
22
    protected $components = [
23
        'host', 'path', 'query',
24
    ];
25
26
    /**
27
     * @var array
28
     */
29
    protected $parsedCurrentUrl;
30
31
    /**
32
     * @inheritDoc
33
     */
34 25
    public function __construct(UrlGenerator $generator, $skippedPaths = [])
35
    {
36 25
        $this->urlGenerator = $generator;
37 25
        $this->skippedPaths = $skippedPaths;
38 25
        $this->parsedCurrentUrl = parse_url($generator->current());
0 ignored issues
show
Documentation Bug introduced by
It seems like parse_url($generator->current()) can also be of type false. However, the property $parsedCurrentUrl is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39 25
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 13
    public function isEquals($url)
45
    {
46 13
        if (in_array($url, $this->skippedPaths)) {
47 1
            return false;
48
        }
49
50 12
        $parsedUrl = parse_url($this->buildUrl($url));
51
52 12
        $counterMin = $this->getMinCounterValue($parsedUrl);
0 ignored issues
show
Security Bug introduced by
It seems like $parsedUrl defined by parse_url($this->buildUrl($url)) on line 50 can also be of type false; however, Malezha\Menu\Support\Com...l::getMinCounterValue() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
53 12
        $counter = 0;
54
55 12
        foreach ($this->components as $component) {
56 12
            if ($this->checkComponent($component, $parsedUrl)) {
0 ignored issues
show
Security Bug introduced by
It seems like $parsedUrl defined by parse_url($this->buildUrl($url)) on line 50 can also be of type false; however, Malezha\Menu\Support\Com...veUrl::checkComponent() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
57 12
                $counter++;
58
            }
59
        }
60
61 12
        if ($counter >= $counterMin) {
62 9
            return true;
63
        }
64
65 11
        return false;
66
    }
67
68
    /**
69
     * @param string $url
70
     * @return string
71
     */
72 12
    protected function buildUrl($url)
73
    {
74 12
        return $this->urlGenerator->to($url);
75
    }
76
77
    /**
78
     * @param string $component
79
     * @param array $parsedUrl
80
     * @return bool
81
     */
82 12
    protected function checkComponent($component, $parsedUrl)
83
    {
84 12
        if (array_key_exists($component, $this->parsedCurrentUrl) &&
85 12
            array_key_exists($component, $parsedUrl) &&
86 12
            $parsedUrl[$component] === $this->parsedCurrentUrl[$component]
87
        ) {
88 12
            return true;
89
        }
90
91 12
        return false;
92
    }
93
94
    /**
95
     * @param array $parsedUrl
96
     * @return int
97
     */
98 12
    protected function getMinCounterValue($parsedUrl)
99
    {
100 12
        return max(count(array_intersect($this->components, array_keys($parsedUrl))),
101 12
            count(array_intersect($this->components, array_keys($this->parsedCurrentUrl))));
102
    }
103
}