Completed
Push — develop ( 1b34c6...481302 )
by Paul
02:47
created

PhpFileModel::hasUse()   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 1
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\PropertyTrait\NamespaceTrait;
12
use PhpUnitGen\Model\PropertyTrait\NameTrait;
13
use PhpUnitGen\Model\PropertyTrait\NodeTrait;
14
use Respect\Validation\Validator;
15
16
/**
17
 * Class PhpFileModel.
18
 *
19
 * @author     Paul Thébaud <[email protected]>.
20
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
21
 * @license    https://opensource.org/licenses/MIT The MIT license.
22
 * @link       https://github.com/paul-thebaud/phpunit-generator
23
 * @since      Class available since Release 2.0.0.
24
 */
25
class PhpFileModel implements PhpFileModelInterface
26
{
27
    use NameTrait;
28
    use NamespaceTrait;
29
    use NodeTrait;
30
31
    /**
32
     * This array is constructed with the full name as key, and the class name as a value.
33
     * @var string[] $uses Imports needed for tests skeletons.
34
     */
35
    private $concreteUses = [];
36
37
    /**
38
     * This array is constructed with the name or the alias as key, and the real namespace, full name as a value.
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
        if (Validator::key($fullName)->validate($this->concreteUses)) {
78
            throw new ParseException(sprintf(
79
                'It seems you import two times the class "%s" in your code',
80
                $fullName
81
            ));
82
        }
83
84
        // Delete duplicate class name
85
        $iteration = 0;
86
        while (Validator::contains($name)->validate($this->concreteUses)) {
87
            if ($iteration === 0 && Validator::contains($fullName)->validate($this->uses)) {
88
                // If a known alias exists
89
                $name = array_search($fullName, $this->uses);
90
            } else {
91
                // Give a default alias
92
                $name .= 'Alias';
93
            }
94
            $iteration++;
95
        }
96
        $this->concreteUses[$fullName] = $name;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function hasConcreteUse(string $name): bool
103
    {
104
        return Validator::contains($name)->validate($this->concreteUses);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getConcreteUses(): array
111
    {
112
        return $this->concreteUses;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function addUse(string $name, string $fullName): void
119
    {
120
        if (Validator::contains($fullName)->validate($this->uses)) {
121
            throw new ParseException(sprintf(
122
                'It seems you import two times the class "%s" in your code',
123
                $fullName
124
            ));
125
        }
126
        $this->uses[$name] = $fullName;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function hasUse(string $name): bool
133
    {
134
        return Validator::key($name)->validate($this->uses);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getFullNameUse(string $name): ?string
141
    {
142
        if ($this->hasUse($name)) {
143
            return $this->uses[$name];
144
        }
145
        return null;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getUses(): array
152
    {
153
        return $this->uses;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function addFunction(FunctionModelInterface $function): void
160
    {
161
        $this->functions[] = $function;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getFunctions(): array
168
    {
169
        return $this->functions;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function addClass(ClassModelInterface $class): void
176
    {
177
        $this->classes[] = $class;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function getClasses(): array
184
    {
185
        return $this->classes;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function addTrait(TraitModelInterface $trait): void
192
    {
193
        $this->traits[] = $trait;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function getTraits(): array
200
    {
201
        return $this->traits;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function addInterface(InterfaceModelInterface $interface): void
208
    {
209
        $this->interfaces[] = $interface;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getInterfaces(): array
216
    {
217
        return $this->interfaces;
218
    }
219
}
220