1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Model; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Exception\ParseException; |
6
|
|
|
use PhpUnitGen\Model\ModelInterface\ClassModelInterface; |
7
|
|
|
use PhpUnitGen\Model\ModelInterface\InterfaceModelInterface; |
8
|
|
|
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface; |
9
|
|
|
use PhpUnitGen\Model\ModelInterface\TraitModelInterface; |
10
|
|
|
use PhpUnitGen\Model\PropertyTrait\ClassLikeTrait; |
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
|
|
|
use ClassLikeTrait; |
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 ClassModelInterface[] $classes Classes contained in the file. |
45
|
|
|
*/ |
46
|
|
|
private $classes = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var TraitModelInterface[] $traits Traits contained in the file. |
50
|
|
|
*/ |
51
|
|
|
private $traits = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var InterfaceModelInterface[] $interfaces Interfaces contained in the file. |
55
|
|
|
*/ |
56
|
|
|
private $interfaces = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getTestableFunctionsCount(): int |
62
|
|
|
{ |
63
|
|
|
$sum = $this->countFunctions(); |
64
|
|
|
foreach ($this->getTraits() as $trait) { |
65
|
|
|
$sum += $trait->countFunctions(); |
66
|
|
|
} |
67
|
|
|
foreach ($this->getClasses() as $class) { |
68
|
|
|
$sum += $class->countFunctions(); |
69
|
|
|
} |
70
|
|
|
return $sum; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getInterfacesFunctionsCount(): int |
77
|
|
|
{ |
78
|
|
|
$sum = 0; |
79
|
|
|
foreach ($this->getInterfaces() as $interface) { |
80
|
|
|
$sum += $interface->countFunctions(); |
81
|
|
|
} |
82
|
|
|
return $sum; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getFullNameFor(string $name): string |
89
|
|
|
{ |
90
|
|
|
$namespace = $this->getNamespaceString(); |
91
|
|
|
return $namespace === null? $name : $namespace . '\\' . $name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function addConcreteUse(string $fullName, string $name): void |
98
|
|
|
{ |
99
|
|
|
// Full name exists and concrete use correspond to this one |
100
|
|
|
if (Validator::key($fullName)->validate($this->concreteUses) |
101
|
|
|
&& $name === $this->concreteUses[$fullName] |
102
|
|
|
) { |
103
|
|
|
// Do not add |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Delete duplicate class name |
108
|
|
|
$iteration = 0; |
109
|
|
|
while (Validator::contains($name)->validate($this->concreteUses)) { |
110
|
|
|
if ($iteration === 0 && Validator::contains($fullName)->validate($this->uses)) { |
111
|
|
|
// If a known alias exists |
112
|
|
|
$name = array_search($fullName, $this->uses); |
113
|
|
|
} else { |
114
|
|
|
// Give a default alias |
115
|
|
|
$name .= 'Alias'; |
116
|
|
|
} |
117
|
|
|
$iteration++; |
118
|
|
|
} |
119
|
|
|
$this->concreteUses[$fullName] = $name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getConcreteUses(): array |
126
|
|
|
{ |
127
|
|
|
return $this->concreteUses; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function addUse(string $name, string $fullName): void |
134
|
|
|
{ |
135
|
|
|
$this->uses[$name] = $fullName; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function hasUse(string $name): bool |
142
|
|
|
{ |
143
|
|
|
return Validator::key($name)->validate($this->uses); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function getUse(string $name): string |
150
|
|
|
{ |
151
|
|
|
if (! $this->hasUse($name)) { |
152
|
|
|
throw new ParseException(sprintf( |
153
|
|
|
'Trying to get a full class name for "%s", but it does not exists', |
154
|
|
|
$name |
155
|
|
|
)); |
156
|
|
|
} |
157
|
|
|
return $this->uses[$name]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function addClass(ClassModelInterface $class): void |
164
|
|
|
{ |
165
|
|
|
$this->classes[] = $class; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function getClasses(): array |
172
|
|
|
{ |
173
|
|
|
return $this->classes; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function addTrait(TraitModelInterface $trait): void |
180
|
|
|
{ |
181
|
|
|
$this->traits[] = $trait; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
public function getTraits(): array |
188
|
|
|
{ |
189
|
|
|
return $this->traits; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function addInterface(InterfaceModelInterface $interface): void |
196
|
|
|
{ |
197
|
|
|
$this->interfaces[] = $interface; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function getInterfaces(): array |
204
|
|
|
{ |
205
|
|
|
return $this->interfaces; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|