aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/plugin-stub-generator/src/Report.php
blob: 92e0565ed4d53f68d756545c3f54b4bd0b5ad404 (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
<?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 */
    private function __construct(private readonly array $categories)
    {
    }

    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 = [];
        foreach ($data['classes'] as $class) {
            $categories[$class['fqcn']] = $class['category'];
        }
        return new self($categories);
    }

    public function category(string $fqcn): ?string
    {
        return $this->categories[$fqcn] ?? null;
    }
}