Total Complexity | 56 |
Total Lines | 449 |
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 |
||
20 | trait ContentManager |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Sets <?php open tag for source code |
||
25 | */ |
||
26 | protected function setTag(): void |
||
27 | { |
||
28 | $this->sourceCode = PhpInterface::PHP_OPEN_TAG . PHP_EOL; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param string $postfix |
||
33 | */ |
||
34 | protected function setNamespace(string $postfix): void |
||
35 | { |
||
36 | $this->sourceCode .= PhpInterface::PHP_NAMESPACE . PhpInterface::SPACE . |
||
37 | $this->generator->modulesDir . PhpInterface::BACKSLASH . |
||
38 | strtoupper($this->generator->version) . |
||
39 | PhpInterface::BACKSLASH . $postfix . PhpInterface::SEMICOLON . PHP_EOL . PHP_EOL; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param string $path |
||
44 | * @param bool $isTrait |
||
45 | * @param bool $isLast |
||
46 | */ |
||
47 | protected function setUse(string $path, bool $isTrait = false, bool $isLast = false): void |
||
48 | { |
||
49 | $this->sourceCode .= (($isTrait === false) ? '' : PhpInterface::TAB_PSR4) . |
||
50 | PhpInterface::PHP_USE . PhpInterface::SPACE . $path . PhpInterface::SEMICOLON . |
||
51 | PHP_EOL . (($isLast === false) ? '' : PHP_EOL); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param string $name |
||
56 | * @param null $extends |
||
|
|||
57 | */ |
||
58 | protected function startClass(string $name, $extends = NULL): void |
||
59 | { |
||
60 | $this->sourceCode .= PhpInterface::PHP_CLASS . PhpInterface::SPACE . $name |
||
61 | . PhpInterface::SPACE; |
||
62 | if ($extends !== NULL) { |
||
63 | $this->sourceCode .= |
||
64 | PhpInterface::PHP_EXTENDS |
||
65 | . PhpInterface::SPACE . $extends . PhpInterface::SPACE; |
||
66 | } |
||
67 | $this->sourceCode .= PHP_EOL . PhpInterface::OPEN_BRACE . PHP_EOL; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Ends class declaration |
||
72 | */ |
||
73 | protected function endClass(): void |
||
74 | { |
||
75 | $this->sourceCode .= PhpInterface::CLOSE_BRACE . PHP_EOL; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param MethodOptions $methodOptions |
||
80 | */ |
||
81 | protected function startMethod(MethodOptions $methodOptions): void |
||
82 | { |
||
83 | // get params |
||
84 | $params = $this->getMethodParams($methodOptions->getParams()); |
||
85 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $methodOptions->getModifier() . PhpInterface::SPACE . |
||
86 | (($methodOptions->isStatic() !== false) ? PhpInterface::PHP_STATIC . PhpInterface::SPACE : |
||
87 | '') . |
||
88 | PhpInterface::PHP_FUNCTION . PhpInterface::SPACE . |
||
89 | $methodOptions->getName() |
||
90 | . PhpInterface::OPEN_PARENTHESES . $params . PhpInterface::CLOSE_PARENTHESES . |
||
91 | ((empty($methodOptions->getReturnType())) ? '' : |
||
92 | PhpInterface::COLON . PhpInterface::SPACE . $methodOptions->getReturnType()) . |
||
93 | PhpInterface::SPACE . PHP_EOL . PhpInterface::TAB_PSR4 |
||
94 | . PhpInterface::OPEN_BRACE . PHP_EOL; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Sets return stmt for any generated method |
||
99 | * |
||
100 | * @param string $value |
||
101 | * @param bool $isString |
||
102 | */ |
||
103 | protected function setMethodReturn(string $value, $isString = false): void |
||
104 | { |
||
105 | $this->setTabs(2); |
||
106 | $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE . (($isString === false) ? $value : |
||
107 | PhpInterface::DOUBLE_QUOTES . $value . PhpInterface::DOUBLE_QUOTES) . PhpInterface::SEMICOLON . PHP_EOL; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Ends method declaration |
||
112 | * |
||
113 | * @param int $eolCnt |
||
114 | */ |
||
115 | protected function endMethod(int $eolCnt = 2): void |
||
116 | { |
||
117 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::CLOSE_BRACE; |
||
118 | for ($i = $eolCnt; $i > 0; --$i) { |
||
119 | $this->sourceCode .= PHP_EOL; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Starts an array declaration in string notation |
||
125 | */ |
||
126 | protected function startArray(): void |
||
127 | { |
||
128 | $this->setTabs(2); |
||
129 | $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE . |
||
130 | PhpInterface::OPEN_BRACKET . PHP_EOL; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Ends an array declaration after values had been set |
||
135 | */ |
||
136 | protected function endArray(): void |
||
137 | { |
||
138 | $this->sourceCode .= PHP_EOL . PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 |
||
139 | . PhpInterface::CLOSE_BRACKET . PhpInterface::SEMICOLON . PHP_EOL; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Creates simple key=value map array property |
||
144 | * |
||
145 | * @param $key |
||
146 | * @param $value |
||
147 | */ |
||
148 | private function setArrayProperty($key, array $value): void |
||
149 | { |
||
150 | $val = $this->setArrayToString($value); |
||
151 | $this->sourceCode .= $this->quoteParam($key) |
||
152 | . PhpInterface::SPACE . PhpInterface::DOUBLE_ARROW |
||
153 | . PhpInterface::SPACE . $val . PhpInterface::COMMA . PHP_EOL; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $prop |
||
158 | * @param string $modifier |
||
159 | * @param string $value |
||
160 | * @param bool $isString |
||
161 | */ |
||
162 | protected function createProperty( |
||
163 | string $prop, |
||
164 | string $modifier, |
||
165 | $value = PhpInterface::PHP_TYPES_NULL, |
||
166 | bool $isString = false |
||
167 | ): void { |
||
168 | if ($value === PhpInterface::PHP_TYPES_NULL) { // drop null assignments as they are already nullable by default |
||
169 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . |
||
170 | $prop . PhpInterface::SEMICOLON . PHP_EOL; |
||
171 | } else { |
||
172 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . |
||
173 | $prop . PhpInterface::SPACE . PhpInterface::EQUALS . PhpInterface::SPACE |
||
174 | . (($isString === false) ? $value : PhpInterface::QUOTES . $value . PhpInterface::QUOTES) . PhpInterface::SEMICOLON . PHP_EOL; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $prop |
||
180 | * @param string $modifier |
||
181 | * @param array $value |
||
182 | */ |
||
183 | protected function createPropertyArray(string $prop, string $modifier, array $value): void |
||
184 | { |
||
185 | $val = $this->setArrayToString($value); |
||
186 | $this->sourceCode .= PhpInterface::TAB_PSR4 . $modifier . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . |
||
187 | $prop . PhpInterface::SPACE . PhpInterface::EQUALS . PhpInterface::SPACE . $val . PhpInterface::SEMICOLON . PHP_EOL; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param array $value |
||
192 | * @return string |
||
193 | */ |
||
194 | private function setArrayToString(array $value): string |
||
195 | { |
||
196 | $val = PhpInterface::OPEN_BRACKET; |
||
197 | $val .= PhpInterface::QUOTES . implode( |
||
198 | PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $value |
||
199 | ) . PhpInterface::QUOTES; |
||
200 | $val .= PhpInterface::CLOSE_BRACKET; |
||
201 | |||
202 | return $val; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param string $comment |
||
207 | * @param int $tabs |
||
208 | */ |
||
209 | protected function setComment(string $comment, int $tabs = 1): void |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Sets an amount of tabs to source code |
||
217 | * @param int $amount |
||
218 | */ |
||
219 | protected function setTabs(int $amount = 1): void |
||
220 | { |
||
221 | for ($i = $amount; $i > 0; --$i) { |
||
222 | $this->sourceCode .= PhpInterface::TAB_PSR4; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Sets an amount of backslashes to source code |
||
228 | * @param int $amount |
||
229 | */ |
||
230 | protected function setBackslashes(int $amount = 1): void |
||
231 | { |
||
232 | for ($i = $amount; $i > 0; --$i) { |
||
233 | $this->sourceCode .= PhpInterface::BACKSLASH; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param array $attrVal |
||
239 | */ |
||
240 | public function setDescription(array $attrVal): void |
||
241 | { |
||
242 | foreach ($attrVal as $k => $v) { |
||
243 | if ($k === ApiInterface::RAML_KEY_DESCRIPTION) { |
||
244 | $this->setTabs(3); |
||
245 | $this->setComment($v); |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param array $params |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | private function getMethodParams(array $params): string |
||
256 | { |
||
257 | $paramsStr = ''; |
||
258 | $cnt = count($params); |
||
259 | foreach ($params as $type => $name) { |
||
260 | --$cnt; |
||
261 | if (is_int($type)) {// not typed |
||
262 | $paramsStr .= PhpInterface::DOLLAR_SIGN . $name; |
||
263 | } else {// typed |
||
264 | $paramsStr .= $type . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . $name; |
||
265 | } |
||
266 | if ($cnt > 0) { |
||
267 | $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE; |
||
268 | } |
||
269 | } |
||
270 | |||
271 | return $paramsStr; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param array $params |
||
276 | * |
||
277 | * @param bool $arrayToJson |
||
278 | * @return string |
||
279 | */ |
||
280 | private function getMethodParamsToPass(array $params, $arrayToJson = true): string |
||
281 | { |
||
282 | $paramsStr = ''; |
||
283 | $cnt = count($params); |
||
284 | foreach ($params as $value) { |
||
285 | --$cnt; |
||
286 | if (is_array($value)) { |
||
287 | $paramsStr .= $arrayToJson ? $this->quoteParam(json_encode($value)) : var_export($value, true); |
||
288 | } else { |
||
289 | $paramsStr .= $this->quoteParam($value); |
||
290 | } |
||
291 | if ($cnt > 0) { |
||
292 | $paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | return $paramsStr; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param string $str |
||
301 | */ |
||
302 | public function setEchoString(string $str): void |
||
303 | { |
||
304 | $this->sourceCode .= PhpInterface::ECHO . PhpInterface::SPACE . PhpInterface::QUOTES |
||
305 | . $str . PhpInterface::QUOTES . PhpInterface::SEMICOLON . PHP_EOL; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param string $attribute |
||
310 | */ |
||
311 | public function openRule(string $attribute): void |
||
312 | { |
||
313 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 . |
||
314 | PhpInterface::TAB_PSR4 |
||
315 | . PhpInterface::QUOTES . $attribute . PhpInterface::QUOTES |
||
316 | . PhpInterface::SPACE |
||
317 | . PhpInterface::DOUBLE_ARROW . |
||
318 | PhpInterface::SPACE . PhpInterface::QUOTES; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Close rules in FormRequest |
||
323 | */ |
||
324 | public function closeRule(): void |
||
325 | { |
||
326 | $this->sourceCode .= PhpInterface::QUOTES . PhpInterface::COMMA; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @uses \SoliDry\Blocks\Controllers::setContent |
||
331 | * @uses \SoliDry\Blocks\Config::setContent |
||
332 | * @uses \SoliDry\Blocks\Migrations::setContent |
||
333 | * @uses \SoliDry\Blocks\Entities::setContent |
||
334 | * @uses \SoliDry\Blocks\FormRequest::setContent |
||
335 | * @uses \SoliDry\Blocks\Tests::setContent |
||
336 | * |
||
337 | * Creates entities like *Controller, *FormRequest, BaseModel entities etc |
||
338 | * |
||
339 | * @param string $basePath |
||
340 | * @param string $postFix |
||
341 | */ |
||
342 | public function createEntity(string $basePath, string $postFix = ''): void |
||
343 | { |
||
344 | $this->setContent(); |
||
345 | $file = $this->getEntityFile($basePath, $postFix); |
||
346 | $isCreated = FileManager::createFile( |
||
347 | $file, $this->sourceCode, |
||
348 | FileManager::isRegenerated($this->generator->options) |
||
349 | ); |
||
350 | if ($isCreated) { |
||
351 | Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Gets Laravel <Entity> file |
||
357 | * |
||
358 | * @param string $basePath |
||
359 | * @param string $postFix |
||
360 | * @return string |
||
361 | */ |
||
362 | public function getEntityFile(string $basePath, string $postFix = ''): string |
||
363 | { |
||
364 | $file = $basePath . DIRECTORY_SEPARATOR . $this->className; |
||
365 | if ($postFix !== '') { |
||
366 | $file .= $postFix; |
||
367 | } |
||
368 | $file .= PhpInterface::PHP_EXT; |
||
369 | |||
370 | return $file; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Creates entities like *Controller, *FormRequest, BaseModel entities etc |
||
375 | * |
||
376 | * @param string $basePath |
||
377 | * @param string $postFix |
||
378 | */ |
||
379 | public function recreateEntity(string $basePath, string $postFix = ''): void |
||
389 | } |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Gets array param as string to place in generated methods |
||
394 | * |
||
395 | * @param array $param |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | private function getArrayParam(array $param): string |
||
400 | { |
||
401 | return PhpInterface::OPEN_BRACKET . PhpInterface::QUOTES . |
||
402 | implode(PhpInterface::QUOTES . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::QUOTES, $param) |
||
403 | . PhpInterface::QUOTES |
||
404 | . PhpInterface::CLOSE_BRACKET; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * @param string $param |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | public function quoteParam(string $param): string |
||
413 | { |
||
414 | return PhpInterface::QUOTES . $param . PhpInterface::QUOTES; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Sets the source starting code |
||
419 | * |
||
420 | * @param string $entityFile |
||
421 | */ |
||
422 | private function setBeforeProps(string $entityFile): void |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Sets the source middle code |
||
431 | * |
||
432 | * @param string $till |
||
433 | */ |
||
434 | private function setAfterProps($till = NULL): void |
||
435 | { |
||
436 | $start = $this->setTabs() . mb_strpos($this->resourceCode, DefaultInterface::PROPS_END, NULL, |
||
437 | PhpInterface::ENCODING_UTF8) - 3; |
||
438 | if ($till === NULL) { |
||
439 | $this->sourceCode .= mb_substr($this->resourceCode, $start, NULL, PhpInterface::ENCODING_UTF8); |
||
440 | } else { |
||
441 | $end = mb_strpos($this->resourceCode, $till, NULL, PhpInterface::ENCODING_UTF8) - 3; |
||
442 | $this->sourceCode .= mb_substr($this->resourceCode, $start, $end - $start, PhpInterface::ENCODING_UTF8); |
||
443 | } |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Sets the source tail |
||
448 | */ |
||
449 | private function setAfterMethods(): void |
||
450 | { |
||
451 | $start = mb_strpos($this->resourceCode, DefaultInterface::METHOD_END, NULL, PhpInterface::ENCODING_UTF8) - 3; |
||
452 | $this->sourceCode .= $this->setTabs() . mb_substr($this->resourceCode, $start, NULL, |
||
453 | PhpInterface::ENCODING_UTF8); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * |
||
458 | * @param string $object |
||
459 | * @param string $method |
||
460 | * @param array $params |
||
461 | * @param bool $arrayToJson |
||
462 | */ |
||
463 | private function methodCallOnObject(string $object, string $method, array $params = [], $arrayToJson = true): void |
||
469 | } |
||
470 | } |