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
Pull Request — master (#688)
by
unknown
18:52
created

Navigation::findActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace SleepingOwl\Admin;
4
5
use Route;
6
use Illuminate\Support\Collection;
7
use SleepingOwl\Admin\Contracts\Navigation\PageInterface;
8
use SleepingOwl\Admin\Contracts\Navigation\NavigationInterface;
9
10
class Navigation extends \KodiComponents\Navigation\Navigation implements NavigationInterface
11
{
12
    protected $currentPage;
13
    protected $currentUrl;
14
15
    /**
16
     * Overload current page.
17
     * @return \KodiComponents\Navigation\Contracts\PageInterface|null
18
     */
19
    public function getCurrentPage()
20
    {
21
        $this->setAliasesId($this->getPages());
0 ignored issues
show
Bug introduced by
It seems like $this->getPages() targeting KodiComponents\Navigation\Navigation::getPages() can also be of type array<integer,object<Kod...ntracts\PageInterface>>; however, SleepingOwl\Admin\Navigation::setAliasesId() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
22
        $this->findActivePage();
23
24
        return $this->currentPage;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->currentPage; (SleepingOwl\Admin\Contra...avigation\PageInterface) is incompatible with the return type declared by the interface KodiComponents\Navigatio...terface::getCurrentPage of type KodiComponents\Navigatio...acts\PageInterface|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
25
    }
26
27
    /**
28
     * Set Alias Id to Page.
29
     * @param Collection $pages
30
     */
31
    public function setAliasesId(Collection $pages)
32
    {
33
        $pages->each(function (PageInterface $page) {
34
            $page->setAliasId();
35
36
            if ($page->getPages()->count()) {
37
                $this->setAliasesId($page->getPages());
38
            }
39
        });
40
    }
41
42
    /**
43
     * @param string $url
44
     * @param array $foundPages
45
     */
46
    protected function findActive($url, array &$foundPages)
47
    {
48
        $this->findPageByAliasId($this->getPages(), $url);
0 ignored issues
show
Bug introduced by
It seems like $this->getPages() targeting KodiComponents\Navigation\Navigation::getPages() can also be of type array<integer,object<Kod...ntracts\PageInterface>>; however, SleepingOwl\Admin\Navigation::findPageByAliasId() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
    }
50
51
    /**
52
     * @param Collection $pages
53
     * @param $url
54
     */
55
    protected function findPageByAliasId(Collection $pages, $url)
56
    {
57
        $pages->each(function (PageInterface $page) use ($url) {
58
            $urlPath = parse_url($url, PHP_URL_PATH);
59
60
            if (Route::current()) {
61
                $parameters = collect(Route::current()->parameters());
62
63
                if ($parameters->has('adminModelId')) {
64
                    $routeUrl = route('admin.model', [
65
                        'adminModel' => snake_case(class_basename($parameters->get('adminModel'))),
66
                    ]);
67
68
                    $urlPath = parse_url($routeUrl, PHP_URL_PATH);
69
                }
70
            }
71
72
            if ($urlPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $urlPath of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73
                if (md5($urlPath) == $page->getAliasId()) {
74
                    $this->currentPage = $page;
75
76
                    return;
77
                }
78
            }
79
80
            $this->findPageByAliasId($page->getPages(), $url);
81
        });
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    protected function findActivePage()
88
    {
89
        if (! is_null($this->currentPage)) {
90
            return true;
91
        }
92
93
        $foundPages = [];
94
95
        $url = $this->getCurrentUrl();
96
97
        $this->findActive($url, $foundPages);
98
99
        if (! is_null($this->currentPage)) {
100
            $this->currentPage->setActive();
101
        }
102
103
        if (config('navigation.aliases')) {
104
            $this->findActiveByAliases(
105
                ltrim(parse_url($url, PHP_URL_PATH), '/')
106
            );
107
        }
108
109
        return false;
110
    }
111
}
112