Total Complexity | 61 |
Total Lines | 496 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ContentManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | trait ContentManager |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Sets <?php open tag for source code |
||
24 | */ |
||
25 | protected function setTag(): void |
||
26 | { |
||
27 | $this->sourceCode = PhpInterface::PHP_OPEN_TAG . PHP_EOL; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param string $postfix |
||
32 | */ |
||
33 | protected function setNamespace(string $postfix): void |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $path |
||
43 | * @param bool $isTrait |
||
44 | * @param bool $isLast |
||
45 | */ |
||
46 | protected function setUse(string $path, bool $isTrait = false, bool $isLast = false): void |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $name |
||
55 | * @param null $extends |
||
|
|||
56 | */ |
||
57 | protected function startClass(string $name, $extends = NULL): void |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Ends class declaration |
||
71 | */ |
||
72 | protected function endClass(): void |
||
73 | { |
||
74 | $this->sourceCode .= PhpInterface::CLOSE_BRACE . PHP_EOL; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param MethodOptions $methodOptions |
||
79 | */ |
||
80 | protected function startMethod(MethodOptions $methodOptions): void |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Sets return stmt for any generated method |
||
98 | * |
||
99 | * @param string $value |
||
100 | * @param bool $isString |
||
101 | */ |
||
102 | protected function setMethodReturn(string $value, $isString = false): void |
||
103 | { |
||
104 | $this->setTabs(2); |
||
105 | $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE . (($isString === false) ? $value : |
||
106 | PhpInterface::DOUBLE_QUOTES . $value . PhpInterface::DOUBLE_QUOTES) . PhpInterface::SEMICOLON . PHP_EOL; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Ends method declaration |
||
111 | * |
||
112 | * @param int $eolCnt |
||
113 | */ |
||
114 | protected function endMethod(int $eolCnt = 2): void |
||
115 | { |
||
116 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::CLOSE_BRACE; |
||
117 | for ($i = $eolCnt; $i > 0; --$i) { |
||
118 | $this->sourceCode .= PHP_EOL; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Starts an array declaration in string notation |
||
124 | */ |
||
125 | protected function startArray(): void |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Ends an array declaration after values had been set |
||
134 | */ |
||
135 | protected function endArray(): void |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Creates simple key=value map array property |
||
143 | * |
||
144 | * @param $key |
||
145 | * @param $value |
||
146 | */ |
||
147 | private function setArrayProperty($key, array $value): void |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $prop |
||
157 | * @param string $modifier |
||
158 | * @param string $value |
||
159 | * @param bool $isString |
||
160 | */ |
||
161 | protected function createProperty( |
||
162 | string $prop, |
||
163 | string $modifier, |
||
164 | $value = PhpInterface::PHP_TYPES_NULL, |
||
165 | bool $isString = false |
||
166 | ): void { |
||
167 | if ($value === PhpInterface::PHP_TYPES_NULL) { // drop null assignments as they are already nullable by default |
||
168 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . |
||
169 | $prop . PhpInterface::SEMICOLON . PHP_EOL; |
||
170 | } else { |
||
171 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . |
||
172 | $prop . PhpInterface::SPACE . PhpInterface::EQUALS . PhpInterface::SPACE |
||
173 | . (($isString === false) ? $value : PhpInterface::QUOTES . $value . PhpInterface::QUOTES) . PhpInterface::SEMICOLON . PHP_EOL; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param string $prop |
||
179 | * @param string $modifier |
||
180 | * @param array $value |
||
181 | */ |
||
182 | protected function createPropertyArray(string $prop, string $modifier, array $value): void |
||
183 | { |
||
184 | $val = $this->setArrayToString($value); |
||
185 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . |
||
186 | $prop . PhpInterface::SPACE . PhpInterface::EQUALS . PhpInterface::SPACE . $val . PhpInterface::SEMICOLON . PHP_EOL; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param array $value |
||
191 | * @return string |
||
192 | */ |
||
193 | private function setArrayToString(array $value): string |
||
194 | { |
||
195 | $val = PhpInterface::OPEN_BRACKET; |
||
196 | $val .= PhpInterface::QUOTES . implode( |
||
197 | PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $value |
||
198 | ) . PhpInterface::QUOTES; |
||
199 | $val .= PhpInterface::CLOSE_BRACKET; |
||
200 | |||
201 | return $val; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param string $comment |
||
206 | * @param int $tabs |
||
207 | */ |
||
208 | protected function setComment(string $comment, int $tabs = 1): void |
||
209 | { |
||
210 | $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::COMMENT |
||
211 | . PhpInterface::SPACE . $comment . PHP_EOL; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param int $tabs |
||
216 | */ |
||
217 | protected function openComment(int $tabs = 1): void |
||
218 | { |
||
219 | $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::SLASH |
||
220 | . PhpInterface::ASTERISK . PhpInterface::ASTERISK . PHP_EOL; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param int $tabs |
||
225 | */ |
||
226 | protected function closeComment(int $tabs = 1): void |
||
227 | { |
||
228 | $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::ASTERISK |
||
229 | . PhpInterface::SLASH . PHP_EOL; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $comment |
||
234 | * @param int $tabs |
||
235 | * @param int $afterTabs |
||
236 | */ |
||
237 | protected function setStarredComment(string $comment, int $tabs = 1, int $afterTabs = 0): void |
||
238 | { |
||
239 | $this->setTabs($tabs); |
||
240 | $this->sourceCode .= PhpInterface::ASTERISK |
||
241 | . PhpInterface::SPACE; |
||
242 | |||
243 | $this->setTabs($afterTabs); |
||
244 | |||
245 | $this->sourceCode .= $comment . PHP_EOL; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Sets an amount of tabs to source code |
||
250 | * |
||
251 | * @param int $amount |
||
252 | */ |
||
253 | protected function setTabs(int $amount = 1): void |
||
254 | { |
||
255 | for ($i = $amount; $i > 0; --$i) { |
||
256 | $this->sourceCode .= PhpInterface::TAB_PSR4; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Sets an amount of backslashes to source code |
||
262 | * |
||
263 | * @param int $amount |
||
264 | */ |
||
265 | protected function setBackslashes(int $amount = 1): void |
||
266 | { |
||
267 | for ($i = $amount; $i > 0; --$i) { |
||
268 | $this->sourceCode .= PhpInterface::BACKSLASH; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @param array $attrVal |
||
274 | */ |
||
275 | public function setDescription(array $attrVal): void |
||
276 | { |
||
277 | foreach ($attrVal as $k => $v) { |
||
278 | if ($k === ApiInterface::RAML_KEY_DESCRIPTION) { |
||
279 | $this->setTabs(3); |
||
280 | $this->setComment($v); |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param array $params |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | private function getMethodParams(array $params): string |
||
291 | { |
||
292 | $paramsStr = ''; |
||
293 | $cnt = count($params); |
||
294 | foreach ($params as $type => $name) { |
||
295 | --$cnt; |
||
296 | if (is_int($type)) {// not typed |
||
297 | $paramsStr .= PhpInterface::DOLLAR_SIGN . $name; |
||
298 | } else {// typed |
||
299 | $paramsStr .= $type . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . $name; |
||
300 | } |
||
301 | if ($cnt > 0) { |
||
302 | $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE; |
||
303 | } |
||
304 | } |
||
305 | |||
306 | return $paramsStr; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param array $params |
||
311 | * |
||
312 | * @param bool $arrayToJson |
||
313 | * @return string |
||
314 | */ |
||
315 | private function getMethodParamsToPass(array $params, $arrayToJson = true): string |
||
316 | { |
||
317 | $paramsStr = ''; |
||
318 | $cnt = count($params); |
||
319 | foreach ($params as $value) { |
||
320 | --$cnt; |
||
321 | if (is_array($value)) { |
||
322 | $paramsStr .= $arrayToJson ? $this->quoteParam(json_encode($value)) : var_export($value, true); |
||
323 | } else { |
||
324 | $paramsStr .= $this->quoteParam($value); |
||
325 | } |
||
326 | if ($cnt > 0) { |
||
327 | $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | return $paramsStr; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param string $str |
||
336 | */ |
||
337 | public function setEchoString(string $str): void |
||
338 | { |
||
339 | $this->sourceCode .= PhpInterface::ECHO . PhpInterface::SPACE . PhpInterface::QUOTES |
||
340 | . $str . PhpInterface::QUOTES . PhpInterface::SEMICOLON . PHP_EOL; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param string $attribute |
||
345 | */ |
||
346 | public function openRule(string $attribute): void |
||
347 | { |
||
348 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 . |
||
349 | PhpInterface::TAB_PSR4 |
||
350 | . PhpInterface::QUOTES . $attribute . PhpInterface::QUOTES |
||
351 | . PhpInterface::SPACE |
||
352 | . PhpInterface::DOUBLE_ARROW . |
||
353 | PhpInterface::SPACE . PhpInterface::QUOTES; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Close rules in FormRequest |
||
358 | */ |
||
359 | public function closeRule(): void |
||
360 | { |
||
361 | $this->sourceCode .= PhpInterface::QUOTES . PhpInterface::COMMA; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @uses \SoliDry\Blocks\Controllers::setContent |
||
366 | * @uses \SoliDry\Blocks\Config::setContent |
||
367 | * @uses \SoliDry\Blocks\Migrations::setContent |
||
368 | * @uses \SoliDry\Blocks\Entities::setContent |
||
369 | * @uses \SoliDry\Blocks\FormRequest::setContent |
||
370 | * @uses \SoliDry\Blocks\Tests::setContent |
||
371 | * |
||
372 | * Creates entities like *Controller, *FormRequest, BaseModel entities etc |
||
373 | * |
||
374 | * @param string $basePath |
||
375 | * @param string $postFix |
||
376 | */ |
||
377 | public function createEntity(string $basePath, string $postFix = ''): void |
||
378 | { |
||
379 | $this->setContent(); |
||
380 | $file = $this->getEntityFile($basePath, $postFix); |
||
381 | $isCreated = FileManager::createFile( |
||
382 | $file, $this->sourceCode, |
||
383 | FileManager::isRegenerated($this->generator->options) |
||
384 | ); |
||
385 | if ($isCreated) { |
||
386 | Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN); |
||
387 | } |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Gets Laravel <Entity> file |
||
392 | * |
||
393 | * @param string $basePath |
||
394 | * @param string $postFix |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getEntityFile(string $basePath, string $postFix = ''): string |
||
398 | { |
||
399 | $file = $basePath . DIRECTORY_SEPARATOR . $this->className; |
||
400 | if ($postFix !== '') { |
||
401 | $file .= $postFix; |
||
402 | } |
||
403 | $file .= PhpInterface::PHP_EXT; |
||
404 | |||
405 | return $file; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Creates entities like *Controller, *FormRequest, BaseModel entities etc |
||
410 | * |
||
411 | * @param string $basePath |
||
412 | * @param string $postFix |
||
413 | */ |
||
414 | public function recreateEntity(string $basePath, string $postFix = ''): void |
||
424 | } |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Gets array param as string to place in generated methods |
||
429 | * |
||
430 | * @param array $param |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | private function getArrayParam(array $param): string |
||
435 | { |
||
436 | return PhpInterface::OPEN_BRACKET . PhpInterface::QUOTES . |
||
437 | implode(PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $param) |
||
438 | . PhpInterface::QUOTES |
||
439 | . PhpInterface::CLOSE_BRACKET; |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @param string $param |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | public function quoteParam(string $param): string |
||
448 | { |
||
449 | return PhpInterface::QUOTES . $param . PhpInterface::QUOTES; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Sets the source starting code |
||
454 | * |
||
455 | * @param string $entityFile |
||
456 | */ |
||
457 | protected function setBeforeProps(string $entityFile): void |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Sets the source middle code |
||
466 | * |
||
467 | * @param string $till |
||
468 | */ |
||
469 | protected function setAfterProps($till = NULL): void |
||
470 | { |
||
471 | $start = $this->setTabs() . mb_strpos($this->resourceCode, DefaultInterface::PROPS_END, NULL, |
||
472 | PhpInterface::ENCODING_UTF8) - 3; |
||
473 | if ($till === NULL) { |
||
474 | $this->sourceCode .= mb_substr($this->resourceCode, $start, NULL, PhpInterface::ENCODING_UTF8); |
||
475 | } else { |
||
476 | $end = mb_strpos($this->resourceCode, $till, NULL, PhpInterface::ENCODING_UTF8) - 3; |
||
477 | $this->sourceCode .= mb_substr($this->resourceCode, $start, $end - $start, PhpInterface::ENCODING_UTF8); |
||
478 | } |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Sets the source tail |
||
483 | */ |
||
484 | private function setAfterMethods(): void |
||
485 | { |
||
486 | $start = mb_strpos($this->resourceCode, DefaultInterface::METHOD_END, NULL, PhpInterface::ENCODING_UTF8) - 3; |
||
487 | $this->sourceCode .= $this->setTabs() . mb_substr($this->resourceCode, $start, NULL, |
||
488 | PhpInterface::ENCODING_UTF8); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * |
||
493 | * @param string $object |
||
494 | * @param string $method |
||
495 | * @param array $params |
||
496 | * @param bool $arrayToJson |
||
497 | */ |
||
498 | private function methodCallOnObject(string $object, string $method, array $params = [], $arrayToJson = true): void |
||
499 | { |
||
500 | $this->sourceCode .= $this->setTabs(2) . PhpInterface::DOLLAR_SIGN . $object |
||
501 | . PhpInterface::ARROW . $method . PhpInterface::OPEN_PARENTHESES |
||
502 | . $this->getMethodParamsToPass($params, $arrayToJson) |
||
503 | . PhpInterface::CLOSE_PARENTHESES . PhpInterface::SEMICOLON . PHP_EOL; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Sets n new lines |
||
508 | * |
||
509 | * @param int $numLines |
||
510 | */ |
||
511 | protected function setNewLines(int $numLines = 1): void |
||
515 | } |
||
516 | } |
||
517 | } |