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