Lines 100.00% 10 / 10
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AttributeKindInference 100.00% 10 / 10 100.00% 1 / 1 5 100.00% 1 / 1
 inferKindsByAttributeName 100.00% 10 / 10 100.00% 1 / 1 5
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Attribute;
6
7use Stewart\Contracts\State\Collection\EntityStateCollection;
8
9final readonly class AttributeKindInference
10{
11    /** @return array<string, AttributeKind> */
12    public function inferKindsByAttributeName(EntityStateCollection $states): array
13    {
14        /** @var array<string, AttributeKind|null> $kinds */
15        $kinds = [];
16
17        foreach ($states as $state) {
18            // Unavailable entities report incomplete attributes.
19            if ($state->isUnavailable()) {
20                continue;
21            }
22
23            foreach ($state->attributes as $name => $value) {
24                $name = (string) $name;
25                $observed = AttributeKind::ofValue($value);
26                $known = $kinds[$name] ?? null;
27                $kinds[$name] = $observed === null ? $known : $known?->widen($observed) ?? $observed;
28            }
29        }
30
31        ksort($kinds, \SORT_STRING);
32
33        return array_map(static fn(?AttributeKind $kind): AttributeKind => $kind ?? AttributeKind::Mixed, $kinds);
34    }
35}