|
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> */ |
|
|
|
|
|
|
21
|
|
|
private readonly array $uris; |
|
22
|
|
|
|
|
23
|
|
|
/** @param list<string> $uris */ |
|
24
|
|
|
public function __construct(array $uris) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->uris = $uris; |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get all URIs |
|
31
|
|
|
* |
|
32
|
|
|
* @return list<string> |
|
33
|
|
|
*/ |
|
34
|
|
|
public function uris(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->uris; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Group URIs by a query parameter value |
|
59
|
|
|
* |
|
60
|
|
|
* @return array<string, list<string>> parameter value => URIs |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths