Passed
Push — feature/second-release ( 3d3b5e...855c58 )
by Andrea Marco
14:24
created

UseStatements::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Services;
6
7
use ArrayIterator;
8
use IteratorAggregate;
9
use Traversable;
10
11
use function Cerbero\LaravelEnum\yieldLines;
12
13
/**
14
 * The use statements collector.
15
 *
16
 * @implements IteratorAggregate<string, class-string>
17
 */
18
final class UseStatements implements IteratorAggregate
19
{
20
    /**
21
     * The regular expression to extract the use statements already present on the enum.
22
     */
23
    public const RE_STATEMENT = '~^use\s+([^\s;]+)(?:\s+as\s+([^;]+))?~i';
24
25
    /**
26
     * Instantiate the class.
27
     *
28
     * @param Inspector<\UnitEnum> $inspector
29
     */
30
    public function __construct(private readonly Inspector $inspector) {}
31
32
    /**
33
     * Retrieve the use statements.
34
     *
35
     * @return ArrayIterator<string, class-string>
36
     */
37
    public function getIterator(): Traversable
38
    {
39
        $useStatements = [
40
            ...$this->fromMethodAnnotations(),
41
            ...$this->existing(),
42
        ];
43
44
        asort($useStatements);
45
46
        return new ArrayIterator($useStatements);
47
    }
48
49
    /**
50
     * Retrieve the use statements from the method annotations.
51
     *
52
     * @return array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
53
     */
54
    public function fromMethodAnnotations(): array
55
    {
56
        $useStatements = [];
57
58
        foreach ($this->inspector->methodAnnotations() as $annotation) {
59
            foreach ($annotation->namespaces as $namespace) {
60
                if (! $this->inspector->hasSameNamespace($namespace)) {
61
                    $useStatements[class_basename($namespace)] = $namespace;
62
                }
63
            }
64
        }
65
66
        return $useStatements;
67
    }
68
69
    /**
70
     * Retrieve the use statements already present on the enum.
71
     *
72
     * @return array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
73
     */
74
    public function existing(): array
75
    {
76
        $useStatements = [];
77
78
        foreach (yieldLines($this->inspector->filename()) as $line) {
79
            if (strpos('enum', $line) === 0) {
80
                break;
81
            }
82
83
            if (preg_match(self::RE_STATEMENT, $line, $matches)) {
84
                $useStatements[$matches[2] ?? class_basename($matches[1])] = $matches[1];
85
            }
86
        }
87
88
        /** @var array<string, class-string> */
89
        return $useStatements;
90
    }
91
}
92