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\NodeTrait; |
13
|
|
|
use Respect\Validation\Validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class PhpFileModel. |
17
|
|
|
* |
18
|
|
|
* @author Paul Thébaud <[email protected]>. |
19
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
20
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
21
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
22
|
|
|
* @since Class available since Release 2.0.0. |
23
|
|
|
*/ |
24
|
|
|
class PhpFileModel implements PhpFileModelInterface |
25
|
|
|
{ |
26
|
|
|
use NamespaceTrait; |
27
|
|
|
use NodeTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This array is constructed with the full name as key, and the class name as a value. |
31
|
|
|
* @var string[] $uses Imports needed for tests skeletons. |
32
|
|
|
*/ |
33
|
|
|
private $concreteUses = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This array is constructed with the name or the alias as key, and the real namespace, full name as a value. |
37
|
|
|
* @var string[] $uses Imports contained in the file. |
38
|
|
|
*/ |
39
|
|
|
private $uses = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var FunctionModelInterface[] $functions Functions contained in the file. |
43
|
|
|
*/ |
44
|
|
|
private $functions = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ClassModelInterface[] $classes Classes contained in the file. |
48
|
|
|
*/ |
49
|
|
|
private $classes = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var TraitModelInterface[] $traits Traits contained in the file. |
53
|
|
|
*/ |
54
|
|
|
private $traits = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var InterfaceModelInterface[] $interfaces Interfaces contained in the file. |
58
|
|
|
*/ |
59
|
|
|
private $interfaces = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function addConcreteUse(string $fullName, string $name): void |
65
|
|
|
{ |
66
|
|
|
if (Validator::contains($name)->validate($this->concreteUses)) { |
67
|
|
|
throw new ParseException(sprintf( |
68
|
|
|
'It seems you import two times the class "%s" in your code', |
69
|
|
|
$fullName |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
$this->concreteUses[$fullName] = $name; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function getConcreteUses(): array |
79
|
|
|
{ |
80
|
|
|
return $this->concreteUses; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function addUse(string $name, string $fullName): void |
87
|
|
|
{ |
88
|
|
|
$this->uses[$name] = $fullName; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function hasUse(string $name): bool |
95
|
|
|
{ |
96
|
|
|
return isset($this->uses[$name]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function getFullNameUse(string $name): ?string |
103
|
|
|
{ |
104
|
|
|
if ($this->hasUse($name)) { |
105
|
|
|
return $this->uses[$name]; |
106
|
|
|
} |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getUses(): array |
114
|
|
|
{ |
115
|
|
|
return $this->uses; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function addFunction(FunctionModelInterface $function): void |
122
|
|
|
{ |
123
|
|
|
$this->functions[] = $function; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getFunctions(): array |
130
|
|
|
{ |
131
|
|
|
return $this->functions; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function addClass(ClassModelInterface $class): void |
138
|
|
|
{ |
139
|
|
|
$this->classes[] = $class; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function getClasses(): array |
146
|
|
|
{ |
147
|
|
|
return $this->classes; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function addTrait(TraitModelInterface $trait): void |
154
|
|
|
{ |
155
|
|
|
$this->traits[] = $trait; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function getTraits(): array |
162
|
|
|
{ |
163
|
|
|
return $this->traits; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function addInterface(InterfaceModelInterface $interface): void |
170
|
|
|
{ |
171
|
|
|
$this->interfaces[] = $interface; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
|
|
public function getInterfaces(): array |
178
|
|
|
{ |
179
|
|
|
return $this->interfaces; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|