Issues (141)

src/Uri.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Exception\UriException;
8
9
use function array_key_exists;
10
use function count;
11
use function filter_var;
12
use function parse_str;
13
use function parse_url;
14
use function sprintf;
15
use function uri_template;
16
17
use const FILTER_FLAG_PATH_REQUIRED;
18
use const FILTER_VALIDATE_URL;
19
20
/** @psalm-import-type Query from Types */
21
final class Uri extends AbstractUri
22
{
23
    /**
24
     * @param Query $query Query parameters that may contain user input
0 ignored issues
show
The type BEAR\Resource\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...
25
     *
26
     * @throws UriException
27
     *
28
     * @psalm-taint-source input $query
29
     */
30
    public function __construct(
31
        string $uri,
32
        array $query = [],
33
    ) {
34
        $this->validate($uri);
35
        if (count($query) !== 0) {
36
            $uri = uri_template($uri, $query);
37
        }
38
39
        $parts = (array) parse_url($uri);
40
        // parse_url() returns array<string>|false but we cast to array
41
        $host = isset($parts['port']) ? sprintf('%s:%s', $parts['host'] ?? '', $parts['port'] ?? '') : $parts['host'] ?? ''; // @phpstan-ignore-line
42
        [$this->scheme, $this->host, $this->path] = [$parts['scheme'] ?? '', $host, $parts['path'] ?? ''];
43
        $parseQuery = $this->query;
44
        if (array_key_exists('query', $parts)) {
45
            parse_str($parts['query'], $parseQuery);
0 ignored issues
show
$parseQuery of type BEAR\Resource\Query is incompatible with the type array expected by parameter $result of parse_str(). ( Ignorable by Annotation )

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

45
            parse_str($parts['query'], /** @scrutinizer ignore-type */ $parseQuery);
Loading history...
46
            // parse_str() outputs array<int|string, array|string> but Query is array<string, mixed>
47
            /** @psalm-suppress MixedPropertyTypeCoercion */
48
            $this->query = $parseQuery; // @phpstan-ignore-line
49
        }
50
51
        if (count($query) === 0) {
52
            return;
53
        }
54
55
        // Array union may contain int keys from parse_str output
56
        /** @psalm-suppress MixedPropertyTypeCoercion */
57
        $this->query = $query + $parseQuery; // @phpstan-ignore-line
58
    }
59
60
    /** @throws UriException */
61
    private function validate(string $uri): void
62
    {
63
        if (filter_var($uri, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
64
            return;
65
        }
66
67
        throw new UriException($uri, 500);
68
    }
69
}
70