CreateFile   C
last analyzed

Complexity

Total Complexity 55

Size/Duplication

Total Lines 535
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 146
c 0
b 0
f 0
dl 0
loc 535
rs 6
wmc 55

31 Methods

Rating   Name   Duplication   Size   Complexity  
A setSubDir() 0 3 1
A getMode() 0 3 1
A getContent() 0 3 1
A getRightString() 0 12 3
A getCreated() 0 3 1
A getLeftString() 0 3 1
A getNotCreated() 0 3 1
A getSimpleString() 0 3 1
A __construct() 0 4 1
A setFileName() 0 3 1
A getStrToLower() 0 3 1
A setRepositoryPath() 0 3 1
A getLcfirst() 0 3 1
A create() 0 12 5
A getFileName() 0 3 1
A getDashComment() 0 3 1
A getRequire() 0 3 1
A getInstance() 0 8 2
A getRepositoryPath() 0 3 1
A getStrToUpper() 0 3 1
A getFolderName() 0 13 3
A setMode() 0 3 1
A getSubDir() 0 3 1
A getUploadPath() 0 9 2
A getRequireOnce() 0 3 1
A getLanguage() 0 15 5
A getCamelCase() 0 12 3
B getHeaderFilesComments() 0 52 5
A setContent() 0 4 1
A renderFile() 0 34 5
A getUcfirst() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
namespace XoopsModules\Modulebuilder\Files;
4
5
use XoopsModules\Modulebuilder;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * modulebuilder module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops https://xoops.org 
25
 *                  Goffy https://myxoops.org
26
 *
27
 */
28
\xoops_load('XoopsFile');
29
30
/**
31
 * Class CreateFile.
32
 */
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)
172
    {
173
        $this->subdir = $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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
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()
297
    {
298
        return $this->mode;
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)
344
    {
345
        if (mb_strpos($string, '_')) {
346
            $str = mb_strpos($string, '_');
347
            if (false !== $str) {
348
                $ret = mb_substr($string, $str + 1, mb_strlen($string));
349
350
                return $ret;
351
            }
352
        }
353
354
        return $string;
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 = '')
463
    {
464
        return "{$t}{$string}\n";
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');
485
        $authorMail       = $module->getVar('mod_author_mail');
486
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
487
        $license          = $module->getVar('mod_license');
488
        //$subversion       = $module->getVar('mod_subversion');
489
        //$date             = date('D Y-m-d H:i:s');
490
        if (null === $noPhpFile) {
491
            $ret = "<?php";
492
            $ret .= "\n\ndeclare(strict_types=1);\n";
493
        } elseif (\is_string($noPhpFile)) {
494
            $ret = $noPhpFile;
495
        } else {
496
            $ret = '';
497
        }
498
        $ret .= "\n{$namespace}/*\n";
499
500
        $filename = TDMC_CLASS_PATH . '/Files/Docs/license.txt';
501
        $handle   = fopen($filename, 'rb');
502
        $data     = fread($handle, filesize($filename));
503
        fclose($handle);
504
        $ret       .= $data . "\n";
505
        $ret       .= "*/\n";
506
        $copyright = [
507
            $name           => 'module for xoops',
508
            ''              => '',
509
            '@copyright   '  => '2021 XOOPS Project (https://xoops.org)',
510
            '@license     '   => $license,
511
            '@package     '   => $dirname,
512
            '@since       '    => $since,
513
            '@min_xoops   ' => $minXoops,
514
        ];
515
        $authorLine = $author;
516
        if ('' !== $authorMail) {
517
            $authorLine .= ' - Email:' . $authorMail;
518
        }
519
        if ('' !== $authorWebsiteUrl) {
520
            $authorLine .= ' - Website:' . $authorWebsiteUrl;
521
        }
522
        $copyright['@author      '] = $authorLine;
523
524
        $ret       .= $pc->getPhpCodeCommentMultiLine($copyright);
525
526
        return $ret;
527
    }
528
529
    /**
530
     * @public function renderFile
531
     * @param null
532
     * @return string
533
     */
534
    public function renderFile()
535
    {
536
        $fileName   = $this->getFileName();
537
        $path       = $this->getUploadPath() . '/' . $fileName;
538
        $created    = $this->getCreated();
539
        $notCreated = $this->getNotCreated();
540
        $folderName = $this->getFolderName();
541
        $mode       = $this->getMode();
542
        $ret        = '';
543
        if (!$this->xf->__construct($path, true)) {
544
            // Force to create file if not exist
545
            if ($this->xf->open($mode, true)) {
546
                if ($this->xf->writable()) {
547
                    // Integration of the content in the file
548
                    if (!$this->xf->write($this->getContent(), $mode, true)) {
549
                        $ret .= \sprintf($notCreated, $fileName, $folderName);
0 ignored issues
show
Bug introduced by
$notCreated of type boolean is incompatible with the type string expected by parameter $format of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

549
                        $ret .= \sprintf(/** @scrutinizer ignore-type */ $notCreated, $fileName, $folderName);
Loading history...
550
                        $GLOBALS['xoopsTpl']->assign('created', false);
551
                        return $ret;
552
                    }
553
                    // Created
554
                    $ret .= \sprintf($created, $fileName, $folderName);
555
                    $GLOBALS['xoopsTpl']->assign('created', true);
556
                    $this->xf->close();
557
                } else {
558
                    $ret .= \sprintf($notCreated, $fileName, $folderName);
559
                    $GLOBALS['xoopsTpl']->assign('created', false);
560
                }
561
            } else {
562
                $ret .= \sprintf($notCreated, $fileName, $folderName);
563
                $GLOBALS['xoopsTpl']->assign('created', false);
564
            }
565
        }
566
567
        return $ret;
568
    }
569
}
570