1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Koriym\AppStateDiagram; |
||
6 | |||
7 | use Generator; |
||
8 | use Koriym\AppStateDiagram\Exception\MissingHashSignInHrefException; |
||
9 | use stdClass; |
||
10 | |||
11 | use function array_shift; |
||
12 | use function explode; |
||
13 | use function in_array; |
||
14 | use function is_int; |
||
15 | use function strpos; |
||
16 | |||
17 | class HyperReference |
||
18 | { |
||
19 | /** @var array<string, string> */ |
||
20 | private $hrefs = []; |
||
21 | |||
22 | /** @var FullPath */ |
||
23 | private $fullPath; |
||
24 | |||
25 | /** @var list<string> */ |
||
26 | private $done = []; |
||
27 | |||
28 | /** @var LabelNameInterface */ |
||
29 | private $labelName; |
||
30 | |||
31 | public function __construct(LabelNameInterface $labelName) |
||
32 | { |
||
33 | $this->fullPath = new FullPath(); |
||
34 | $this->labelName = $labelName; |
||
35 | } |
||
36 | |||
37 | public function add(string $alpsFile, string $href): void |
||
38 | { |
||
39 | $fullPath = ($this->fullPath)($alpsFile, $href); |
||
40 | if (! is_int(strpos($fullPath, '#'))) { |
||
41 | throw new MissingHashSignInHrefException($href); |
||
42 | } |
||
43 | |||
44 | [, $id] = explode('#', $fullPath); |
||
45 | if (in_array($id, $this->done)) { |
||
46 | return; |
||
47 | } |
||
48 | |||
49 | $this->hrefs[$id] = $fullPath; |
||
50 | $this->done[] = $id; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param array<string, stdClass> $instances |
||
55 | * |
||
56 | * @return array<string, stdClass> |
||
57 | */ |
||
58 | public function getInstances(array $instances): array |
||
59 | { |
||
60 | $hrefs = $this->hrefGenerator(); |
||
61 | foreach ($hrefs as $href) { |
||
62 | [$file, $id] = explode('#', $href); |
||
63 | if (! $file) { |
||
64 | continue; // @codeCoverageIgnore |
||
65 | } |
||
66 | |||
67 | if (isset($instances[$id])) { |
||
68 | continue; |
||
69 | } |
||
70 | |||
71 | $alps = new Profile($file, $this->labelName, false); |
||
72 | [$importInstances, $hyperReference] = $alps->export($id, $file); |
||
73 | /** @var array<string, stdClass> $importInstances */ |
||
74 | $this->merge($hyperReference); |
||
75 | $instances += $importInstances; |
||
76 | } |
||
77 | |||
78 | return $instances; |
||
79 | } |
||
80 | |||
81 | /** @return Generator<string> */ |
||
82 | public function hrefGenerator(): Generator |
||
83 | { |
||
84 | while ($this->hrefs) { |
||
0 ignored issues
–
show
|
|||
85 | yield array_shift($this->hrefs); |
||
86 | } |
||
87 | |||
88 | return []; |
||
89 | } |
||
90 | |||
91 | public function merge(HyperReference $hyperReference): void |
||
92 | { |
||
93 | $this->hrefs += $hyperReference->hrefs; |
||
94 | } |
||
95 | } |
||
96 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.