Issues (480)

src/Url/Plugin.php (17 issues)

1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Url;
7
8
use Mvc5\Http\HttpUri;
9
use Mvc5\Http\Request;
10
use Mvc5\Http\Uri;
11
12
use function array_shift;
13
use function strrpos;
14
use function substr;
15
16
use const Mvc5\{ ABSOLUTE, FRAGMENT, HOST, NAME, PARAMS, PARENT, PORT, QUERY, SEPARATOR, SCHEME, URI };
0 ignored issues
show
The type Mvc5\SCHEME was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\PARENT was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\QUERY was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\NAME was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\PORT was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\FRAGMENT was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\SEPARATOR was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\URI was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\ABSOLUTE was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\HOST was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\PARAMS was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
final class Plugin
19
{
20
    /**
21
     * @var bool|false
22
     */
23
    protected bool $absolute = false;
24
25
    /**
26
     * @var callable
27
     */
28
    protected $assembler;
29
30
    /**
31
     * @var callable
32
     */
33
    protected $generator;
34
35
    /**
36
     * @var string|null
37
     */
38
    protected ?string $name = null;
39
40
    /**
41
     * @var array
42
     */
43
    protected array $params = [];
44
45
    /**
46
     * @var Uri
47
     */
48
    protected Uri $uri;
49
50
    /**
51
     * @param Request $request
52
     * @param callable $generator
53
     * @param callable|null $assembler
54
     * @param bool|false $absolute
55
     */
56 8
    function __construct(Request $request, callable $generator, callable $assembler = null, bool $absolute = false)
57
    {
58 8
        $this->absolute = $absolute;
59 8
        $this->assembler = $assembler ?? new Assemble;
60 8
        $this->generator = $generator;
61 8
        $this->name = $request[NAME];
62 8
        $this->uri = $request[URI] ?? new HttpUri;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request[Mvc5\URI] ?? new Mvc5\Http\HttpUri() can also be of type Mvc5\Http\HttpUri. However, the property $uri is declared as type Mvc5\URI. 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...
63
64 8
        $this->params[$this->name] = (array) $request[PARAMS];
65
66 8
        $this->parent($request, $request[PARENT]);
0 ignored issues
show
The constant parent was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67 8
    }
68
69
    /**
70
     * @param array|Uri $uri
71
     * @param array $options
72
     * @return array|Uri
73
     */
74 8
    protected function absolute($uri, array $options = [])
75
    {
76 8
        if (!$this->absolute && empty($uri[ABSOLUTE])) {
77 6
            return $uri;
78
        }
79
80 2
        !isset($uri[SCHEME]) &&
81 2
            $options[SCHEME] = $this->uri[SCHEME];
82
83 2
        !isset($uri[PORT]) &&
84 2
            $options[PORT] = $this->uri[PORT];
85
86 2
        !isset($uri[HOST]) &&
87 2
            $options[HOST] = $this->uri[HOST];
88
89 2
        return !$options ? $uri : ($uri instanceof Uri ? $uri->with($options) : $options + $uri);
90
    }
91
92
    /**
93
     * @param string|Uri $route
94
     * @param array|string|null $query
95
     * @param string|null $fragment
96
     * @param array $options
97
     * @return string|null
98
     */
99 8
    protected function assemble($route, $query = null, string $fragment = null, array $options = []) : ?string
100
    {
101 8
        return $route ? ($this->assembler)($route, $query, $fragment, $options) : null;
102
    }
103
104
    /**
105
     * @param array|string|Uri|null $route
106
     * @param array|string|null $query
107
     * @param string|null $fragment
108
     * @param array $options
109
     * @return string|null
110
     */
111 8
    protected function create($route, $query = null, string $fragment = null, array $options = []) : ?string
112
    {
113 8
        return $route instanceof Uri ? $this->uri($route) :
114 8
            $this->uri($this->route((array) $route, $this->options($query, $fragment, $options)));
115
    }
116
117
    /**
118
     * @param string $name
119
     * @param array $params
120
     * @param array $options
121
     * @return Uri|null
122
     */
123 7
    protected function generate(string $name, array $params, array $options) : ?Uri
124
    {
125 7
        return $name[0] === SEPARATOR ? null : ($this->generator)($name, $this->params($name, $params), $options);
126
    }
127
128
    /**
129
     * @param int $pos
130
     * @param string $name
131
     * @return array
132
     */
133 2
    protected function match(int $pos, string $name) : array
134
    {
135 2
        return !$pos ? [] : $this->params[$name = substr($name, 0, $pos)] ??
136 2
            $this->match((int) strrpos($name, SEPARATOR), $name);
137
    }
138
139
    /**
140
     * @param string $name
141
     * @return string
142
     */
143 7
    protected function name($name) : string
144
    {
145 7
        return (string) ($name ?? $this->name);
146
    }
147
148
    /**
149
     * @param array|string $query
150
     * @param string|null $fragment
151
     * @param array $options
152
     * @return array
153
     */
154 7
    protected function options($query, string $fragment = null, array $options = []) : array
155
    {
156 7
        return [FRAGMENT => $fragment, QUERY => $query] + $options;
157
    }
158
159
    /**
160
     * @param Request|null $request
161
     * @param Request|null $parent
162
     * @return Request|null
163
     */
164 8
    protected function parent(Request $request = null, Request $parent = null) : ?Request
165
    {
166 8
        $parent && ($name = $parent[NAME]) &&
167 1
            $this->params[$name] = $parent[PARAMS] ?? [];
168
169 8
        return $request && $request !== $parent ? $this->parent($parent, $parent[PARENT] ?? null) : null;
0 ignored issues
show
The constant parent was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
170
    }
171
172
    /**
173
     * @param string $name
174
     * @param array $params
175
     * @return array
176
     */
177 5
    protected function params(string $name, array $params) : array
178
    {
179 5
        return $params + ($this->params[$name] ?? $this->match((int) strrpos($name, SEPARATOR), $name));
180
    }
181
182
    /**
183
     * @param array $route
184
     * @param array $options
185
     * @return Uri|null
186
     */
187 7
    protected function route(array $route, array $options) : ?Uri
188
    {
189 7
        return $this->generate($this->name(array_shift($route)), $route, $options);
190
    }
191
192
    /**
193
     * @param Uri|null $uri
194
     * @return string|null
195
     */
196 8
    protected function uri(?Uri $uri) : ?string
197
    {
198 8
        return $uri ? $this->assemble($this->absolute($uri)) : null;
0 ignored issues
show
It seems like $this->absolute($uri) can also be of type array; however, parameter $route of Mvc5\Url\Plugin::assemble() does only seem to accept Mvc5\Http\Uri|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
        return $uri ? $this->assemble(/** @scrutinizer ignore-type */ $this->absolute($uri)) : null;
Loading history...
199
    }
200
201
    /**
202
     * @param array|string|Uri|null $route
203
     * @param array|string|null $query
204
     * @param string|null $fragment
205
     * @param array $options
206
     * @return string|null
207
     */
208 8
    function __invoke($route = null, $query = null, string $fragment = null, array $options = []) : ?string
209
    {
210 8
        return $this->create($route, $query, $fragment, $options) ??
211 8
            $this->assemble($route, $query, $fragment, $this->absolute($options));
0 ignored issues
show
It seems like $this->absolute($options) can also be of type Mvc5\Http\Uri; however, parameter $options of Mvc5\Url\Plugin::assemble() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

211
            $this->assemble($route, $query, $fragment, /** @scrutinizer ignore-type */ $this->absolute($options));
Loading history...
It seems like $route can also be of type array; however, parameter $route of Mvc5\Url\Plugin::assemble() does only seem to accept Mvc5\Http\Uri|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

211
            $this->assemble(/** @scrutinizer ignore-type */ $route, $query, $fragment, $this->absolute($options));
Loading history...
212
    }
213
}
214