Test Setup Failed
Branch master (da5350)
by Timur
06:44
created

DependencyAwareGenerator::removeUse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
abstract class DependencyAwareGenerator extends AbstractGenerator
8
{
9
    protected array $usePaths = [];
10
    protected array $useGroups = [];
11
12
    /**
13
     * List of all generator children, which maintain their own use dependencies.
14
     * The list should be defined in the constructor.
15
     *
16
     * @var mixed[]
17
     */
18
    protected array $dependencyAwareChildren = [];
19
20
    public function resolveQualifier(string $path, $alias = ''): string
21
    {
22
        if (empty($path) || false === Config::$shortenQualifiers || '\\' === $path[0]) {
23
            return $path;
24
        }
25
26
        if ($path[0] === Config::$suppressSymbol) {
27
            return substr($path, 1);
28
        }
29
30
        if ($qualifier = Utils::resolveQualifier($path)) {
31
            $this->usePaths[$path] = $alias;
32
            $path = $qualifier;
33
        }
34
35
        return $path;
36
    }
37
38
    public function addUse(string $fqcn, string ...$aliases): self
39
    {
40
        $this->usePaths[$fqcn] = implode(', ', $aliases);
41
42
        return $this;
43
    }
44
45
    public function addUseGroup(string $fqcn, string ...$classNames)
46
    {
47
        foreach ($classNames as $name) {
48
            if ($qualifier = Utils::resolveQualifier($name)) {
49
                $name = $qualifier;
50
            }
51
52
            if (empty($this->useGroups[$fqcn]) || !in_array($name, $this->useGroups[$fqcn])) {
53
                $this->useGroups[$fqcn][] = $name;
54
            }
55
        }
56
57
        return $this;
58
    }
59
60
    public function removeUse(string $fqcn): self
61
    {
62
        unset($this->usePaths[$fqcn]);
63
        unset($this->useGroups[$fqcn]);
64
65
        return $this;
66
    }
67
68
    public function useGroupsToArray()
69
    {
70
        $result = [];
71
72
        foreach ($this->useGroups as $path => $classNames) {
73
            $result[rtrim($path, '\\').'\{'.implode(', ', $classNames).'}'] = '';
74
        }
75
76
        return $result;
77
    }
78
79
    /**
80
     * Returns all use-qualifiers used in this object and all it's children.
81
     *
82
     * @return string[]
83
     */
84
    public function getUsePaths(): array
85
    {
86
        // Merge self use paths and use groups
87
        $mergedPaths = $this->usePaths + $this->useGroupsToArray();
88
89
        foreach ($this->dependencyAwareChildren as $child) {
90
            if (is_array($child)) {
91
                foreach ($child as $subchild) {
92
                    if ($subchild instanceof self) {
93
                        $mergedPaths = $mergedPaths + $subchild->getUsePaths();
94
                    }
95
                }
96
            } else {
97
                $mergedPaths = $mergedPaths + $child->getUsePaths();
98
            }
99
        }
100
101
        return $mergedPaths;
102
    }
103
}
104