Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.78% covered (warning)
83.78%
31 / 37
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
DiscoveredSymbols
83.78% covered (warning)
83.78%
31 / 37
77.78% covered (warning)
77.78%
7 / 9
12.61
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSymbols
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getConstants
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNamespaces
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getClasses
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDiscoveredNamespaces
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
 getDiscoveredClasses
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 getDiscoveredConstants
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BrianHenryIE\Strauss;
4
5use BrianHenryIE\Strauss\Types\ClassSymbol;
6use BrianHenryIE\Strauss\Types\ConstantSymbol;
7use BrianHenryIE\Strauss\Types\NamespaceSymbol;
8
9class DiscoveredSymbols
10{
11    /**
12     * All discovered symbols, grouped by type, indexed by original name.
13     *
14     * @var array<string,array<string,DiscoveredSymbol>>
15     */
16    protected array $types = [];
17
18    public function __construct()
19    {
20        $this->types = [
21            ClassSymbol::class => [],
22            ConstantSymbol::class => [],
23            NamespaceSymbol::class => [],
24        ];
25    }
26
27    /**
28     * @param DiscoveredSymbol $symbol
29     */
30    public function add(DiscoveredSymbol $symbol): void
31    {
32        $this->types[get_class($symbol)][$symbol->getOriginalSymbol()] = $symbol;
33    }
34
35    /**
36     * @return DiscoveredSymbol[]
37     */
38    public function getSymbols(): array
39    {
40        return array_merge(
41            array_values($this->getNamespaces()),
42            array_values($this->getClasses()),
43            array_values($this->getConstants())
44        );
45    }
46
47    /**
48     * @return array<string, ConstantSymbol>
49     */
50    public function getConstants()
51    {
52        return $this->types[ConstantSymbol::class];
53    }
54
55    /**
56     * @return array<string, NamespaceSymbol>
57     */
58    public function getNamespaces(): array
59    {
60        return $this->types[NamespaceSymbol::class];
61    }
62
63    /**
64     * @return array<string, ClassSymbol>
65     */
66    public function getClasses(): array
67    {
68        return $this->types[ClassSymbol::class];
69    }
70
71
72    /**
73     * TODO: Order by longest string first. (or instead, record classnames with their namespaces)
74     *
75     * @return array<string, NamespaceSymbol>
76     */
77    public function getDiscoveredNamespaces(?string $namespacePrefix = ''): array
78    {
79        $discoveredNamespaceReplacements = [];
80
81        // When running subsequent times, try to discover the original namespaces.
82        // This is naive: it will not work where namespace replacement patterns have been used.
83        foreach ($this->getNamespaces() as $key => $value) {
84            $discoveredNamespaceReplacements[ $value->getOriginalSymbol() ] = $value;
85        }
86
87        uksort($discoveredNamespaceReplacements, function ($a, $b) {
88            return strlen($a) <=> strlen($b);
89        });
90
91        return $discoveredNamespaceReplacements;
92    }
93
94    /**
95     * @return string[]
96     */
97    public function getDiscoveredClasses(?string $classmapPrefix = ''): array
98    {
99        $discoveredClasses = $this->getClasses();
100
101        $discoveredClasses = array_filter(
102            array_keys($discoveredClasses),
103            function (string $replacement) use ($classmapPrefix) {
104                return empty($classmapPrefix) || ! str_starts_with($replacement, $classmapPrefix);
105            }
106        );
107
108        return $discoveredClasses;
109    }
110
111    /**
112     * @return string[]
113     */
114    public function getDiscoveredConstants(?string $constantsPrefix = ''): array
115    {
116        $discoveredConstants = $this->getConstants();
117        $discoveredConstants = array_filter(
118            array_keys($discoveredConstants),
119            function (string $replacement) use ($constantsPrefix) {
120                return empty($constantsPrefix) || ! str_starts_with($replacement, $constantsPrefix);
121            }
122        );
123
124        return $discoveredConstants;
125    }
126}