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