Passed
Push — develop ( fbb092...e84888 )
by Paul
03:57
created

PhpFileModel::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpUnitGen\Model;
4
5
use PhpUnitGen\Exception\ParseException;
6
use PhpUnitGen\Model\ModelInterface\ClassModelInterface;
7
use PhpUnitGen\Model\ModelInterface\FunctionModelInterface;
8
use PhpUnitGen\Model\ModelInterface\InterfaceModelInterface;
9
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
10
use PhpUnitGen\Model\ModelInterface\TraitModelInterface;
11
use PhpUnitGen\Model\ModelInterface\UseModelInterface;
12
use PhpUnitGen\Model\PropertyTrait\NamespaceTrait;
13
use PhpUnitGen\Model\PropertyTrait\NameTrait;
14
use PhpUnitGen\Model\PropertyTrait\NodeTrait;
15
use Respect\Validation\Validator;
16
17
/**
18
 * Class PhpFileModel.
19
 *
20
 * @author     Paul Thébaud <[email protected]>.
21
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
22
 * @license    https://opensource.org/licenses/MIT The MIT license.
23
 * @link       https://github.com/paul-thebaud/phpunit-generator
24
 * @since      Class available since Release 2.0.0.
25
 */
26
class PhpFileModel implements PhpFileModelInterface
27
{
28
    use NameTrait;
29
    use NamespaceTrait;
30
    use NodeTrait;
31
32
    /**
33
     * This array is constructed with the full name as key, and the class name as a value.
34
     * @var string[] $uses Imports needed for tests skeletons.
35
     */
36
    private $concreteUses = [];
37
38
    /**
39
     * @var string[] $uses Imports contained in the file.
40
     */
41
    private $uses = [];
42
43
    /**
44
     * @var FunctionModelInterface[] $functions Functions contained in the file.
45
     */
46
    private $functions = [];
47
48
    /**
49
     * @var ClassModelInterface[] $classes Classes contained in the file.
50
     */
51
    private $classes = [];
52
53
    /**
54
     * @var TraitModelInterface[] $traits Traits contained in the file.
55
     */
56
    private $traits = [];
57
58
    /**
59
     * @var InterfaceModelInterface[] $interfaces Interfaces contained in the file.
60
     */
61
    private $interfaces = [];
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getFullNameFor(string $name): string
67
    {
68
        $namespace = $this->getNamespaceString();
69
        return $namespace === null? $name : $namespace . '\\' . $name;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function addConcreteUse(string $fullName, string $name): void
76
    {
77
        // Full name exists and concrete use correspond to this one
78
        if (Validator::key($fullName)->validate($this->concreteUses)
79
            && $name === $this->concreteUses[$fullName]
80
        ) {
81
            // Do not add
82
            return;
83
        }
84
85
        // Delete duplicate class name
86
        $iteration = 0;
87
        while (Validator::contains($name)->validate($this->concreteUses)) {
88
            if ($iteration === 0 && Validator::contains($fullName)->validate($this->uses)) {
89
                // If a known alias exists
90
                $name = array_search($fullName, $this->uses);
91
            } else {
92
                // Give a default alias
93
                $name .= 'Alias';
94
            }
95
            $iteration++;
96
        }
97
        $this->concreteUses[$fullName] = $name;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getConcreteUses(): array
104
    {
105
        return $this->concreteUses;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function addUse(string $name, string $fullName): void
112
    {
113
        $this->uses[$name] = $fullName;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function hasUse(string $name): bool
120
    {
121
        return Validator::key($name)->validate($this->uses);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getUse(string $name): string
128
    {
129
        if (! $this->hasUse($name)) {
130
            throw new ParseException(sprintf(
131
                'Trying to get a full class name for "%s", but it does not exists',
132
                $name
133
            ));
134
        }
135
        return $this->uses[$name];
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function addFunction(FunctionModelInterface $function): void
142
    {
143
        $this->functions[] = $function;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getFunctions(): array
150
    {
151
        return $this->functions;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function addClass(ClassModelInterface $class): void
158
    {
159
        $this->classes[] = $class;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getClasses(): array
166
    {
167
        return $this->classes;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function addTrait(TraitModelInterface $trait): void
174
    {
175
        $this->traits[] = $trait;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function getTraits(): array
182
    {
183
        return $this->traits;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function addInterface(InterfaceModelInterface $interface): void
190
    {
191
        $this->interfaces[] = $interface;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getInterfaces(): array
198
    {
199
        return $this->interfaces;
200
    }
201
}
202