blob: 758a17686d79dcd4ee8ae546ada29ea532583f6b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?php
declare(strict_types=1);
namespace Shirabe\PluginStubGenerator;
/** The classifier report (scripts/plugin-class-classifier/report.json). */
final class Report
{
/**
* @param array<string, string> $categories fqcn => category
* @param array<string, string> $kinds fqcn => class / interface / trait / enum
*/
private function __construct(private readonly array $categories, private readonly array $kinds)
{
}
public static function load(string $path): self
{
if (!is_file($path)) {
throw new GenerationError([
"missing classifier report $path (run scripts/plugin-class-classifier/classify first)",
]);
}
$data = json_decode((string) file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
if (($data['violations'] ?? null) !== []) {
throw new GenerationError(["$path records classification violations; fix the classifier lists first"]);
}
$categories = [];
$kinds = [];
foreach ($data['classes'] as $class) {
$categories[$class['fqcn']] = $class['category'];
$kinds[$class['fqcn']] = $class['kind'];
}
return new self($categories, $kinds);
}
public function category(string $fqcn): ?string
{
return $this->categories[$fqcn] ?? null;
}
public function kind(string $fqcn): ?string
{
return $this->kinds[$fqcn] ?? null;
}
/**
* The FQCNs of the given categories, in report order.
*
* @param list<string> $categories
* @return list<string>
*/
public function inCategories(array $categories): array
{
$out = [];
foreach ($this->categories as $fqcn => $category) {
if (in_array($category, $categories, true)) {
$out[] = $fqcn;
}
}
return $out;
}
}
|