Completed
Push — master ( cbdcc2...201068 )
by Akihito
21s queued 12s
created

HyperReference::getHref()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\AppStateDiagram;
6
7
use Generator;
8
use Koriym\AppStateDiagram\Exception\SharpMissingInHrefException;
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> */
0 ignored issues
show
Bug introduced by
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...
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, '#'))) {
0 ignored issues
show
introduced by
The condition is_int(strpos($fullPath, '#')) is always true.
Loading history...
41
            throw new SharpMissingInHrefException($href);
42
        }
43
44
        [, $id] = explode('#', $fullPath);
45
        if (in_array($id, $this->done)) {
0 ignored issues
show
Bug introduced by
$this->done of type Koriym\AppStateDiagram\list is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        if (in_array($id, /** @scrutinizer ignore-type */ $this->done)) {
Loading history...
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
    /**
82
     * @return Generator<string>
83
     */
84
    public function hrefGenerator(): Generator
85
    {
86
        while ($this->hrefs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->hrefs of type array<string,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
87
            yield array_shift($this->hrefs);
88
        }
89
90
        return [];
91
    }
92
93
    public function merge(HyperReference $hyperReference): void
94
    {
95
        $this->hrefs += $hyperReference->hrefs;
96
    }
97
}
98