Issues (57)

src/AppState.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\AppStateDiagram;
6
7
use JetBrains\PhpStorm\Immutable;
8
9
use function array_key_exists;
10
use function assert;
11
use function sprintf;
12
13
use const PHP_EOL;
14
15
/** @psalm-immutable */
16
#[Immutable]
17
final class AppState
18
{
19
    /** @var array<string, AbstractDescriptor> */
20
    private $states;
21
22
    /** @var array<string, AbstractDescriptor> */
23
    private $taggedStates;
24
25
    /** @var ?string */
26
    private $color;
27
28
    /** @var LabelNameInterface */
29
    private $labelName;
30
31
    /**
32
     * @param Link[]                    $links
33
     * @param array<AbstractDescriptor> $descriptors
34
     * @param list<string>              $filterIds
0 ignored issues
show
The type Koriym\AppStateDiagram\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...
35
     *
36
     * @psalm-suppress ImpureMethodCall
37
     */
38
    public function __construct(array $links, array $descriptors, LabelNameInterface $labelName, ?TaggedProfile $profile = null, ?string $color = null, array $filterIds = [])
39
    {
40
        $this->labelName = $labelName;
41
        $taggedStates = new Descriptors();
42
        if (isset($profile)) {
43
            foreach ($profile->links as $link) {
44
                $taggedStates->add($descriptors[$link->from]);
45
                $taggedStates->add($descriptors[$link->to]);
46
            }
47
        }
48
49
        $this->taggedStates = $taggedStates->descriptors;
50
51
        $states = new Descriptors();
52
        foreach ($links as $link) {
53
            if (! array_key_exists($link->from, $this->taggedStates)) {
54
                $states->add($descriptors[$link->from]);
55
            }
56
57
            if (! array_key_exists($link->to, $this->taggedStates)) {
58
                if (! isset($descriptors[$link->to])) {
59
                    continue; // @codeCoverageIgnore
60
                }
61
62
                $states->add($descriptors[$link->to]);
63
            }
64
        }
65
66
        $this->states = $states->descriptors;
67
        $this->color = $color;
68
        $this->remove($filterIds);
69
    }
70
71
    /** @param list<string> $filterIds */
72
    private function remove(array $filterIds): void
73
    {
74
        foreach ($filterIds as $filterId) {
75
            unset($this->states[$filterId], $this->taggedStates[$filterId]);
76
        }
77
    }
78
79
    public function __toString(): string
80
    {
81
        $base = '    %s [label = <%s> URL="docs/%s.%s.html" target="_parent"';
82
        $dot = '';
83
        foreach ($this->taggedStates as $descriptor) {
84
            assert($descriptor instanceof SemanticDescriptor);
85
            $dot .= $this->format($descriptor, $base);
86
        }
87
88
        foreach ($this->states as $descriptor) {
89
            assert($descriptor instanceof SemanticDescriptor);
90
            $dot .= sprintf($base . ']' . PHP_EOL, $descriptor->id, $this->labelName->getNodeLabel($descriptor), $descriptor->type, $descriptor->id);
91
        }
92
93
        return $dot;
94
    }
95
96
    private function format(SemanticDescriptor $descriptor, string $base): string
97
    {
98
        if ($this->color === null) {
99
            $template = $base . ']' . PHP_EOL;
100
101
            return sprintf($template, $descriptor->id, $this->labelName->getNodeLabel($descriptor), $descriptor->type, $descriptor->id);
102
        }
103
104
        $template = $base . ' color="%s"]' . PHP_EOL;
105
106
        return sprintf($template, $descriptor->id, $this->labelName->getNodeLabel($descriptor), $descriptor->type, $descriptor->id, $this->color);
107
    }
108
}
109