Total Complexity | 55 |
Total Lines | 535 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CreateFile 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 CreateFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class CreateFile extends CreateTableFields |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $tab = ' '; |
||
39 | |||
40 | /** |
||
41 | * @var mixed |
||
42 | */ |
||
43 | private $xf = null; |
||
44 | /** |
||
45 | * "fileName" attribute of the files. |
||
46 | * |
||
47 | * @var mixed |
||
48 | */ |
||
49 | private $fileName = null; |
||
50 | /** |
||
51 | * "subdir" attribute of the directories. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $subdir = null; |
||
56 | /** |
||
57 | * "uploadPath" attribute of the files. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $uploadPath = null; |
||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $content = null; |
||
66 | /** |
||
67 | * @var mixed |
||
68 | */ |
||
69 | private $created = null; |
||
70 | /** |
||
71 | * @var mixed |
||
72 | */ |
||
73 | private $notCreated = null; |
||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $mode = null; |
||
78 | /** |
||
79 | * @var mixed |
||
80 | */ |
||
81 | protected $phpcode = null; |
||
82 | /** |
||
83 | * @var mixed |
||
84 | */ |
||
85 | protected $htmlcode; |
||
86 | |||
87 | /** |
||
88 | * @public function constructor |
||
89 | * @param null |
||
90 | */ |
||
91 | public function __construct() |
||
92 | { |
||
93 | parent::__construct(); |
||
94 | $this->xf = \XoopsFile::getHandler(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @public static function getInstance |
||
99 | * @param null |
||
100 | * @return Modulebuilder\Files\CreateFile |
||
101 | */ |
||
102 | public static function getInstance() |
||
103 | { |
||
104 | static $instance = false; |
||
105 | if (!$instance) { |
||
106 | $instance = new self(); |
||
107 | } |
||
108 | |||
109 | return $instance; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @public function create |
||
114 | * |
||
115 | * @param $moduleDirname |
||
116 | * @param $subdir |
||
117 | * @param $fileName |
||
118 | * @param $content |
||
119 | * @param $created |
||
120 | * @param $notCreated |
||
121 | * @param $mode |
||
122 | */ |
||
123 | public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+') |
||
124 | { |
||
125 | $this->setFileName($fileName); |
||
126 | $this->created = $created; |
||
127 | $this->notCreated = $notCreated; |
||
128 | $this->setMode($mode); |
||
129 | $this->setRepositoryPath($moduleDirname); |
||
130 | if (!empty($content) && \is_string($content)) { |
||
131 | $this->setContent($content); |
||
132 | } |
||
133 | if (isset($subdir) && \is_string($subdir)) { |
||
134 | $this->setSubDir($subdir); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @public function write |
||
140 | * @param mixed $moduleDirname |
||
141 | */ |
||
142 | /*public function write($module, $fileName) |
||
143 | { |
||
144 | $this->setModule($module); |
||
145 | $this->setFileName($fileName); |
||
146 | }*/ |
||
147 | |||
148 | /** |
||
149 | * @private function setRepositoryPath |
||
150 | * @param string $moduleDirname |
||
151 | */ |
||
152 | private function setRepositoryPath($moduleDirname) |
||
153 | { |
||
154 | $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH . '/' . $moduleDirname; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @private function getRepositoryPath |
||
159 | * @param null |
||
160 | * @return string |
||
161 | */ |
||
162 | private function getRepositoryPath() |
||
163 | { |
||
164 | return $this->uploadPath; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @private function setSubDir |
||
169 | * @param $subdir |
||
170 | */ |
||
171 | private function setSubDir($subdir) |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @private function getSubDir |
||
178 | * @param null |
||
179 | * @return string |
||
180 | */ |
||
181 | private function getSubDir() |
||
182 | { |
||
183 | return $this->subdir; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * public function setFileName. |
||
188 | * |
||
189 | * @param $fileName |
||
190 | */ |
||
191 | public function setFileName($fileName) |
||
192 | { |
||
193 | $this->fileName = $fileName; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @public function getFileName |
||
198 | * @param null |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function getFileName() |
||
202 | { |
||
203 | return $this->fileName; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @private function setContent |
||
208 | * @param $content |
||
209 | */ |
||
210 | private function setContent($content) |
||
211 | { |
||
212 | //replace tabs by 4 spaces |
||
213 | $this->content = preg_replace('/\t/', $this->tab, $content); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @private function setContent |
||
218 | * @param null |
||
219 | * @return string |
||
220 | */ |
||
221 | private function getContent() |
||
222 | { |
||
223 | return $this->content; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @private function getFolderName |
||
228 | * @param null |
||
229 | * @return string |
||
230 | */ |
||
231 | private function getFolderName() |
||
232 | { |
||
233 | $path = $this->getUploadPath(); |
||
234 | if (\mb_strrpos($path, '\\')) { |
||
235 | $str = \mb_strrpos($path, '\\'); |
||
236 | if (false !== $str) { |
||
237 | return mb_substr($path, $str + 1, mb_strlen($path)); |
||
238 | } |
||
239 | |||
240 | return mb_substr($path, $str, mb_strlen($path)); |
||
241 | } |
||
242 | //return 'root module'; |
||
243 | return false; |
||
|
|||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @public function getUploadPath |
||
248 | * @param null |
||
249 | * @return string |
||
250 | */ |
||
251 | private function getUploadPath() |
||
252 | { |
||
253 | if (null != $this->getSubDir()) { |
||
254 | $ret = $this->getRepositoryPath() . '/' . $this->getSubDir(); |
||
255 | } else { |
||
256 | $ret = $this->getRepositoryPath(); |
||
257 | } |
||
258 | |||
259 | return $ret; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @private function getCreated |
||
264 | * @param null |
||
265 | * @return bool |
||
266 | */ |
||
267 | private function getCreated() |
||
268 | { |
||
269 | return $this->created; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @private function getNotCreated |
||
274 | * @param null |
||
275 | * @return bool |
||
276 | */ |
||
277 | private function getNotCreated() |
||
278 | { |
||
279 | return $this->notCreated; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @private function setMode |
||
284 | * @param $mode |
||
285 | */ |
||
286 | private function setMode($mode) |
||
287 | { |
||
288 | $this->mode = $mode; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @private function getMode |
||
293 | * @param null |
||
294 | * @return string |
||
295 | */ |
||
296 | private function getMode() |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @public function getLanguage |
||
303 | * @param string $moduleDirname |
||
304 | * @param string $prefix |
||
305 | * @param string $suffix |
||
306 | * @param string $addFq //add function qualifier |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getLanguage($moduleDirname, $prefix = '', $suffix = '', $addFq = true) |
||
310 | { |
||
311 | $lang = ''; |
||
312 | if ($addFq) { |
||
313 | $lang = '\\'; |
||
314 | } |
||
315 | $lang .= '_' . $prefix . '_' . \mb_strtoupper($moduleDirname); |
||
316 | $ret = $lang; |
||
317 | if (!empty($suffix) || '_' !== $suffix) { |
||
318 | $ret = $lang . '_' . $suffix; |
||
319 | } elseif ('_' === $suffix) { |
||
320 | $ret = $lang . '_'; |
||
321 | } |
||
322 | |||
323 | return $ret; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @public function getLeftString |
||
328 | * @param string $string |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getLeftString($string) |
||
333 | { |
||
334 | return mb_substr($string, 0, mb_strpos($string, '_')); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @public function getRightString |
||
339 | * @param $string |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getRightString($string = null) |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @public function getCamelCase |
||
359 | * @param $string |
||
360 | * @param $ucfirst |
||
361 | * @param $lcfirst |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | public function getCamelCase($string, $ucfirst = false, $lcfirst = false) |
||
366 | { |
||
367 | $rightString = $this->getRightString($string); |
||
368 | $leftString = $this->getLeftString($string); |
||
369 | if ($ucfirst) { |
||
370 | return $this->getUcfirst($leftString) . $this->getUcfirst($rightString); |
||
371 | } |
||
372 | if ($lcfirst) { |
||
373 | return $this->getLcfirst($leftString) . $this->getUcfirst($rightString); |
||
374 | } |
||
375 | |||
376 | return $string; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @public function getUcfirst |
||
381 | * @param $string |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | public function getUcfirst($string) |
||
386 | { |
||
387 | return \ucfirst($string); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @public function getLcfirst |
||
392 | * |
||
393 | * @param $string |
||
394 | * @return string |
||
395 | */ |
||
396 | public function getLcfirst($string) |
||
397 | { |
||
398 | return lcfirst($string); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @public function getStrToUpper |
||
403 | * |
||
404 | * @param $string |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getStrToUpper($string) |
||
408 | { |
||
409 | return \mb_strtoupper($string); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * @public function getStrToLower |
||
414 | * @param $string |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | public function getStrToLower($string) |
||
419 | { |
||
420 | return \mb_strtolower($string); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * @public function getRequire |
||
425 | * @param $filename |
||
426 | * @return string |
||
427 | */ |
||
428 | public function getRequire($filename = 'header') |
||
429 | { |
||
430 | return "require __DIR__ . '/{$filename}.php';\n"; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @public function getRequireOnce |
||
435 | * @param $filename |
||
436 | * @return string |
||
437 | */ |
||
438 | public function getRequireOnce($filename = 'header') |
||
439 | { |
||
440 | return "require_once __DIR__ . '/{$filename}.php';\n"; |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * @private function getDashComment |
||
445 | * |
||
446 | * @param $comment |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | public function getDashComment($comment) |
||
451 | { |
||
452 | return "// ------------------- {$comment} ------------------- //\n"; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @public function getSimpleString |
||
457 | * @param $string |
||
458 | * |
||
459 | * @param string $t |
||
460 | * @return string |
||
461 | */ |
||
462 | public function getSimpleString($string, $t = '') |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @public function getHeaderFilesComments |
||
469 | * @param string $module |
||
470 | * @param $noPhpFile |
||
471 | * |
||
472 | * @param string $namespace |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getHeaderFilesComments($module, $noPhpFile = null, $namespace = '') |
||
476 | { |
||
477 | $pc = Modulebuilder\Files\CreatePhpCode::getInstance(); |
||
478 | $name = $module->getVar('mod_name'); |
||
479 | $dirname = $module->getVar('mod_dirname'); |
||
480 | //$version = $module->getVar('mod_version'); |
||
481 | $since = $module->getVar('mod_since'); |
||
482 | $minXoops = $module->getVar('mod_min_xoops'); |
||
483 | $author = $module->getVar('mod_author'); |
||
484 | //$credits = $module->getVar('mod_credits'); |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * @public function renderFile |
||
531 | * @param null |
||
532 | * @return string |
||
533 | */ |
||
534 | public function renderFile() |
||
568 | } |
||
569 | } |
||
570 |