GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a7502b...53a580 )
by Sebastian
11s
created

Activatable::setActive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Menu\Traits;
4
5
use Spatie\Menu\Helpers\Str;
6
use Spatie\Url\Url;
7
8
/**
9
 * Expects an `$active` property on the class.
10
 *
11
 * @property string $url
12
 */
13
trait Activatable
14
{
15
    /**
16
     * @return bool
17
     */
18
    public function isActive(): bool
19
    {
20
        return $this->active;
0 ignored issues
show
Bug introduced by
The property active 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...
21
    }
22
23
    /**
24
     * @param bool|callable $active
25
     *
26
     * @return $this
27
     */
28
    public function setActive($active = true)
29
    {
30
        if (is_callable($active)) {
31
            $this->active = $active($this);
32
33
            return $this;
34
        }
35
36
        $this->active = (bool) $active;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @return $this
43
     */
44
    public function setInactive()
45
    {
46
        $this->active = false;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return string|null
53
     */
54
    public function url()
55
    {
56
        return $this->url;
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function hasUrl(): bool
63
    {
64
        return ! is_null($this->url);
65
    }
66
67
    /**
68
     * @param string|null $url
69
     *
70
     * @return $this
71
     */
72
    public function setUrl($url)
73
    {
74
        $this->url = $url;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $url
81
     * @param string $root
82
     *
83
     * @return $this
84
     */
85
    public function determineActiveForUrl(string $url, string $root = '/')
86
    {
87
        if (! $this->hasUrl()) {
88
            return;
89
        }
90
91
        $itemUrl = Url::fromString($this->url);
92
        $matchUrl = Url::fromString($url);
93
94
        // If the hosts don't match, this url isn't active.
95
        if ($itemUrl->getHost() !== $matchUrl->getHost()) {
96
            return $this->setInactive();
97
        }
98
99
        // If this url doesn't start with the root, it's inactive.
100
        if (! Str::startsWith($itemUrl->getPath(), $root)) {
101
            return $this->setInactive();
102
        }
103
104
        // For the next comparisons we just need the paths, and we'll remove
105
        // the root first.
106
        $itemPath = Str::removeFromStart($root, $itemUrl->getPath());
107
        $matchPath = Str::removeFromStart($root, $matchUrl->getPath());
108
109
        // If this url starts with the url we're matching with, it's active.
110
        if (Str::startsWith($matchPath, $itemPath)) {
111
            return $this->setActive();
112
        }
113
114
        return $this->setInactive();
115
    }
116
}
117