Passed
Push — claude/understand-crawl-pLOr9 ( efe806 )
by Akihito
02:23
created

Requests::parseQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\Batch;
6
7
use function array_map;
8
use function parse_str;
9
use function parse_url;
10
11
use const PHP_URL_QUERY;
12
13
/**
14
 * Collection of URIs for batch processing with helper methods
15
 *
16
 * @psalm-type UriString = string
17
 */
18
final class Requests
19
{
20
    /** @var list<string> */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\Batch\list 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...
21
    private readonly array $uris;
22
23
    /** @param list<string> $uris */
24
    public function __construct(array $uris)
25
    {
26
        $this->uris = $uris;
0 ignored issues
show
Bug introduced by
The property uris is declared read-only in BEAR\Resource\Batch\Requests.
Loading history...
Documentation Bug introduced by
It seems like $uris of type array is incompatible with the declared type BEAR\Resource\Batch\list of property $uris.

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...
27
    }
28
29
    /**
30
     * Get all URIs
31
     *
32
     * @return list<string>
33
     */
34
    public function uris(): array
35
    {
36
        return $this->uris;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->uris returns the type array which is incompatible with the documented return type BEAR\Resource\Batch\list.
Loading history...
37
    }
38
39
    /**
40
     * Get values of a specific query parameter from all URIs
41
     *
42
     * @return list<mixed>
43
     */
44
    public function getQueryParam(string $name): array
45
    {
46
        $values = [];
47
        foreach ($this->uris as $uri) {
48
            $query = $this->parseQuery($uri);
49
            if (isset($query[$name])) {
50
                $values[] = $query[$name];
51
            }
52
        }
53
54
        return $values;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $values returns the type array which is incompatible with the documented return type BEAR\Resource\Batch\list.
Loading history...
55
    }
56
57
    /**
58
     * Group URIs by a query parameter value
59
     *
60
     * @return array<string, list<string>> parameter value => URIs
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<string>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
61
     */
62
    public function groupBy(string $paramName): array
63
    {
64
        $grouped = [];
65
        foreach ($this->uris as $uri) {
66
            $query = $this->parseQuery($uri);
67
            if (isset($query[$paramName])) {
68
                $key = (string) $query[$paramName];
69
                $grouped[$key][] = $uri;
70
            }
71
        }
72
73
        return $grouped;
74
    }
75
76
    /**
77
     * Map result rows to URIs based on a key column
78
     *
79
     * @param list<array<string, mixed>> $rows      Result rows from database
80
     * @param string                     $keyColumn Column name to use as key for mapping
81
     */
82
    public function mapResults(array $rows, string $keyColumn): Results
83
    {
84
        $grouped = $this->groupBy($keyColumn);
85
        $results = [];
86
87
        foreach ($this->uris as $uri) {
88
            $results[$uri] = [];
89
        }
90
91
        foreach ($rows as $row) {
92
            if (! isset($row[$keyColumn])) {
93
                continue;
94
            }
95
96
            $keyValue = (string) $row[$keyColumn];
97
            if (! isset($grouped[$keyValue])) {
98
                continue;
99
            }
100
101
            foreach ($grouped[$keyValue] as $uri) {
102
                $results[$uri][] = $row;
103
            }
104
        }
105
106
        return new Results($results);
107
    }
108
109
    /**
110
     * Parse query string from URI
111
     *
112
     * @return array<string, mixed>
113
     */
114
    private function parseQuery(string $uri): array
115
    {
116
        $queryString = parse_url($uri, PHP_URL_QUERY);
117
        if ($queryString === null || $queryString === false) {
118
            return [];
119
        }
120
121
        parse_str($queryString, $query);
122
123
        return $query;
124
    }
125
}
126