WebRouter::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Router;
6
7
use BEAR\Sunday\Annotation\DefaultSchemeHost;
8
use BEAR\Sunday\Extension\Router\RouterInterface;
9
use BEAR\Sunday\Extension\Router\RouterMatch;
10
use Override;
11
12
use function assert;
13
use function parse_url;
14
15
use const PHP_URL_PATH;
16
17
/**
18
 * @psalm-import-type Globals from RouterInterface
19
 * @psalm-import-type Server from RouterInterface
20
 * @psalm-suppress ClassMustBeFinal
21
 */
22
class WebRouter implements RouterInterface, WebRouterInterface
23
{
24
    public function __construct(
25
        #[DefaultSchemeHost]
26
        private string $schemeHost,
27
        private HttpMethodParamsInterface $httpMethodParams,
28
    ) {
29
    }
30
31
    /**
32
     * @param array{HTTP_X_HTTP_METHOD_OVERRIDE?: string, REQUEST_METHOD: string, REQUEST_URI: string, ...} $server
33
     * @param array{_GET: array<string|array>, _POST: array<string|array>}                              $globals
34
     */
35
36
    /**
37
     * {@inheritDoc}
38
     *
39
     * @param Globals $globals Superglobals containing user input ($_GET, $_POST)
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Provide\Router\Globals 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...
40
     * @param Server  $server  Server variables containing request data
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Provide\Router\Server 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...
41
     *
42
     * @psalm-taint-source input $globals
43
     * @psalm-taint-source input $server
44
     */
45
    #[Override]
46
    public function match(array $globals, array $server)
47
    {
48
        $requestUri = $server['REQUEST_URI'];
49
        $get = $globals['_GET'];
50
        $post = $globals['_POST'];
51
        [$method, $query] = $this->httpMethodParams->get($server, $get, $post);
52
        $parsedPath = parse_url($requestUri, PHP_URL_PATH);
53
        assert($parsedPath !== null && $parsedPath !== false);
54
        $path = $this->schemeHost . $parsedPath;
55
56
        return new RouterMatch($method, $path, $query);
0 ignored issues
show
Bug introduced by
$query of type BEAR\Package\Provide\Router\QueryParams is incompatible with the type array expected by parameter $query of BEAR\Sunday\Extension\Ro...terMatch::__construct(). ( Ignorable by Annotation )

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

56
        return new RouterMatch($method, $path, /** @scrutinizer ignore-type */ $query);
Loading history...
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    #[Override]
63
    public function generate($name, $data)
64
    {
65
        return false;
66
    }
67
}
68