Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.55% |
68 / 69 |
|
93.33% |
14 / 15 |
CRAP | |
0.00% |
0 / 1 |
ClassGenerator | |
98.55% |
68 / 69 |
|
93.33% |
14 / 15 |
37 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
setParent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addInterface | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addInterfaces | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getInterfaces | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasInterfaces | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasInterface | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
removeInterface | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
render | |
96.55% |
28 / 29 |
|
0.00% |
0 / 1 |
13 | |||
formatConstants | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
formatProperties | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
formatMethods | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Code\Generator; |
15 | |
16 | /** |
17 | * Class generator class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Code |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 5.0.0 |
25 | */ |
26 | class ClassGenerator extends AbstractClassGenerator |
27 | { |
28 | |
29 | use Traits\UseTrait, Traits\PropertiesTrait, Traits\AbstractFinalTrait; |
30 | |
31 | /** |
32 | * Parent class that is extended |
33 | * @var ?string |
34 | */ |
35 | protected ?string $parent = null; |
36 | |
37 | /** |
38 | * Interfaces that are implemented |
39 | * @var array |
40 | */ |
41 | protected array $interfaces = []; |
42 | |
43 | /** |
44 | * Constructor |
45 | * |
46 | * Instantiate the class generator object |
47 | * |
48 | * @param string $name |
49 | * @param ?string $parent |
50 | * @param mixed $interface |
51 | * @param bool $abstract |
52 | */ |
53 | public function __construct(string $name, ?string $parent = null, mixed $interface = null, bool $abstract = false) |
54 | { |
55 | $this->setName($name); |
56 | |
57 | if ($parent !== null) { |
58 | $this->setParent($parent); |
59 | } |
60 | |
61 | if ($interface !== null) { |
62 | if (is_array($interface)) { |
63 | $this->addInterfaces($interface); |
64 | } else if (str_contains($interface, ',')) { |
65 | $this->addInterfaces(array_map('trim', explode(',', $interface))); |
66 | } else { |
67 | $this->addInterface($interface); |
68 | } |
69 | } |
70 | |
71 | $this->setAsAbstract($abstract); |
72 | } |
73 | |
74 | /** |
75 | * Set the class parent |
76 | * |
77 | * @param ?string $parent |
78 | * @return ClassGenerator |
79 | */ |
80 | public function setParent(?String $parent = null): ClassGenerator |
81 | { |
82 | $this->parent = $parent; |
83 | return $this; |
84 | } |
85 | |
86 | /** |
87 | * Get the class parent |
88 | * |
89 | * @return string|null |
90 | */ |
91 | public function getParent(): string|null |
92 | { |
93 | return $this->parent; |
94 | } |
95 | |
96 | /** |
97 | * Has parent |
98 | * |
99 | * @return bool |
100 | */ |
101 | public function hasParent(): bool |
102 | { |
103 | return ($this->parent !== null); |
104 | } |
105 | |
106 | /** |
107 | * Add a class interface |
108 | * |
109 | * @param string $interface |
110 | * @return ClassGenerator |
111 | */ |
112 | public function addInterface(string $interface): ClassGenerator |
113 | { |
114 | if (!in_array($interface, $this->interfaces)) { |
115 | $this->interfaces[] = $interface; |
116 | } |
117 | |
118 | return $this; |
119 | } |
120 | |
121 | /** |
122 | * Add a class interface |
123 | * |
124 | * @param array $interfaces |
125 | * @return ClassGenerator |
126 | */ |
127 | public function addInterfaces(array $interfaces): ClassGenerator |
128 | { |
129 | foreach ($interfaces as $interface) { |
130 | $this->addInterface($interface); |
131 | } |
132 | |
133 | return $this; |
134 | } |
135 | |
136 | /** |
137 | * Get the class interfaces |
138 | * |
139 | * @return array |
140 | */ |
141 | public function getInterfaces(): array |
142 | { |
143 | return $this->interfaces; |
144 | } |
145 | |
146 | /** |
147 | * Has class interfaces |
148 | * |
149 | * @return bool |
150 | */ |
151 | public function hasInterfaces(): bool |
152 | { |
153 | return (!empty($this->interfaces)); |
154 | } |
155 | |
156 | /** |
157 | * Has class interface |
158 | * |
159 | * @param string $interface |
160 | * @return bool |
161 | */ |
162 | public function hasInterface(string $interface): bool |
163 | { |
164 | return (in_array($interface, $this->interfaces)); |
165 | } |
166 | |
167 | /** |
168 | * Remove class interface |
169 | * |
170 | * @param string $interface |
171 | * @return ClassGenerator |
172 | */ |
173 | public function removeInterface(string $interface): ClassGenerator |
174 | { |
175 | if (in_array($interface, $this->interfaces)) { |
176 | $key = array_search($interface, $this->interfaces); |
177 | unset($this->interfaces[$key]); |
178 | } |
179 | |
180 | return $this; |
181 | } |
182 | |
183 | /** |
184 | * Render class |
185 | * |
186 | * @return string |
187 | */ |
188 | public function render(): string |
189 | { |
190 | $classKeyword = null; |
191 | |
192 | if ($this->isAbstract()) { |
193 | $classKeyword = 'abstract '; |
194 | } else if ($this->isFinal()) { |
195 | $classKeyword = 'final '; |
196 | } |
197 | |
198 | $this->output = ($this->namespace !== null) ? $this->namespace->render() . PHP_EOL : null; |
199 | $this->output .= ($this->docblock !== null) ? $this->docblock->render() : null; |
200 | $this->output .= $classKeyword . 'class ' . $this->name; |
201 | |
202 | if ($this->parent !== null) { |
203 | $this->output .= ' extends ' . $this->parent; |
204 | } |
205 | if (!empty($this->interfaces)) { |
206 | $this->output .= ' implements ' . implode(', ', $this->interfaces); |
207 | } |
208 | |
209 | $this->output .= PHP_EOL . '{' . PHP_EOL; |
210 | |
211 | if ($this->hasUses()) { |
212 | $this->output .= PHP_EOL; |
213 | foreach ($this->uses as $ns => $as) { |
214 | $this->output .= $this->printIndent() . 'use '; |
215 | $this->output .= $ns; |
216 | if ($as !== null) { |
217 | $this->output .= ' as ' . $as; |
218 | } |
219 | $this->output .= ';' . PHP_EOL; |
220 | } |
221 | } |
222 | |
223 | if ($this->hasConstants()) { |
224 | $this->output .= $this->formatConstants(); |
225 | } |
226 | if ($this->hasProperties()) { |
227 | $this->output .= $this->formatProperties(); |
228 | } |
229 | if ($this->hasMethods()) { |
230 | $this->output .= $this->formatMethods(); |
231 | } |
232 | |
233 | $this->output .= PHP_EOL . '}' . PHP_EOL; |
234 | |
235 | return $this->output; |
236 | } |
237 | |
238 | /** |
239 | * Format the constants |
240 | * |
241 | * @return string |
242 | */ |
243 | protected function formatConstants(): string |
244 | { |
245 | $constants = null; |
246 | |
247 | foreach ($this->constants as $constant) { |
248 | $constants .= $constant->render() . PHP_EOL; |
249 | } |
250 | |
251 | return $constants; |
252 | } |
253 | |
254 | /** |
255 | * Format the properties |
256 | * |
257 | * @return string |
258 | */ |
259 | protected function formatProperties(): string |
260 | { |
261 | $props = null; |
262 | |
263 | foreach ($this->properties as $prop) { |
264 | $props .= $prop->render() . PHP_EOL; |
265 | } |
266 | |
267 | return $props; |
268 | } |
269 | |
270 | /** |
271 | * Format the methods |
272 | * |
273 | * @return string |
274 | */ |
275 | protected function formatMethods(): string |
276 | { |
277 | $methods = null; |
278 | |
279 | foreach ($this->methods as $method) { |
280 | $methods .= $method->render() . PHP_EOL; |
281 | } |
282 | |
283 | return $methods; |
284 | } |
285 | |
286 | /** |
287 | * Print class |
288 | * |
289 | * @return string |
290 | */ |
291 | public function __toString(): string |
292 | { |
293 | return $this->render(); |
294 | } |
295 | |
296 | } |