Issues (87)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Models/MenuManager.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Menus\Models;
6
7
use Closure;
8
use Countable;
9
use ReflectionFunction;
10
use Illuminate\Routing\Router;
11
use Illuminate\Support\Facades\Route;
12
use Illuminate\Contracts\View\Factory as ViewFactory;
13
14
class MenuManager implements Countable
15
{
16
    /**
17
     * The menus collection.
18
     *
19
     * @var \Illuminate\Support\Collection
20
     */
21
    protected $menus;
22
23
    /**
24
     * The view factory.
25
     *
26
     * @var \Illuminate\Contracts\View\Factory
27
     */
28
    protected $viewFactory;
29
30
    /**
31
     * The router instance.
32
     *
33
     * @var \Illuminate\Routing\Router
34
     */
35
    protected $router;
36
37
    /**
38
     * The menu factory.
39
     *
40
     * @var \Rinvex\Menus\Models\MenuGenerator
41
     */
42
    protected $generator;
43
44
    /**
45
     * The menu callbacks.
46
     *
47
     * @var array
48
     */
49
    protected $callbacks = [];
50
51
    /**
52
     * The constructor.
53
     *
54
     * @param \Illuminate\Contracts\View\Factory $viewFactory
55
     * @param \Illuminate\Routing\Router         $router
56
     */
57
    public function __construct(ViewFactory $viewFactory, Router $router)
58
    {
59
        $this->viewFactory = $viewFactory;
60
        $this->callbacks = collect();
0 ignored issues
show
Documentation Bug introduced by
It seems like collect() of type object<Illuminate\Support\Collection> is incompatible with the declared type array of property $callbacks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
        $this->menus = collect();
62
        $this->router = $router;
63
    }
64
65
    /**
66
     * Check if the menu exists.
67
     *
68
     * @param string $name
69
     *
70
     * @return bool
71
     */
72
    public function has($name): bool
73
    {
74
        return $this->menus->has($name);
75
    }
76
77
    /**
78
     * Get instance of the given menu if exists.
79
     *
80
     * @param string $name
81
     *
82
     * @return \Rinvex\Menus\Models\MenuGenerator|null
83
     */
84
    public function instance($name): ?MenuGenerator
85
    {
86
        return $this->menus->get($name);
87
    }
88
89
    /**
90
     * Register a menu.
91
     *
92
     * @param string   $name
93
     * @param \Closure $callback
94
     *
95
     * @return void
96
     */
97
    public function register($name, Closure $callback): void
98
    {
99
        if ($this->has($name)) {
100
            $this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback));
0 ignored issues
show
The method get cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
The method put cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
101
        } else {
102
            $builder = new MenuGenerator();
103
104
            $builder->setViewFactory($this->viewFactory);
0 ignored issues
show
$this->viewFactory of type object<Illuminate\Contracts\View\Factory> is not a sub-type of object<Illuminate\View\Factory>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\View\Factory to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
105
106
            $this->menus->put($name, $builder);
107
108
            $this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback));
0 ignored issues
show
The method get cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
The method put cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
        }
110
    }
111
112
    /**
113
     * Render the menu tag by given name.
114
     *
115
     * @param string $name
116
     * @param string $presenter
0 ignored issues
show
Should the type for parameter $presenter not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
117
     * @param array  $bindings
118
     * @param bool   $specialSidebar
119
     *
120
     * @return string|null
121
     */
122
    public function render(string $name, string $presenter = null, array $bindings = [], bool $specialSidebar = false): ?string
123
    {
124
        if ($this->has($name)) {
125
            $instance = $this->instance($name);
126
127
            $this->callbacks->get($name)->each(function ($callback) use ($instance) {
0 ignored issues
show
The method get cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
128
                $reflectionParams = collect((new ReflectionFunction($callback))->getParameters());
129
                $reflectionParams->shift();
130
131
                collect($reflectionParams)->each(function ($param) use (&$params) {
132
                    $name = $param->getType()->getName();
133
                    $params[] = Route::current()->hasParameter($param->getName()) ? Route::current()->parameter($param->getName())
134
                        : ((app()->bound($name) ?: class_exists($name) ?: interface_exists($name)) ? app($name) : null);
135
                });
136
137
                $params ? $callback($instance, ...$params) : $callback($instance);
138
            });
139
140
            return $instance->setBindings($bindings)->render($presenter, $specialSidebar);
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Get all menus.
148
     *
149
     * @return array
150
     */
151
    public function all(): array
152
    {
153
        return $this->menus->toArray();
154
    }
155
156
    /**
157
     * Get count from all menus.
158
     *
159
     * @return int
160
     */
161
    public function count(): int
162
    {
163
        return $this->menus->count();
164
    }
165
166
    /**
167
     * Empty the current menus.
168
     */
169
    public function destroy()
170
    {
171
        $this->menus = collect();
172
173
        return $this;
174
    }
175
}
176