Passed
Push — master ( 7331d1...59e477 )
by Goffy
05:28
created

CreateFile   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 532
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 140
c 0
b 0
f 0
dl 0
loc 532
rs 6.96
wmc 53

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
A getHeaderFilesComments() 0 46 3
A setContent() 0 3 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 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)
176
    {
177
        $this->subdir = $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;
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...
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()
300
    {
301
        return $this->mode;
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)
347
    {
348
        if (mb_strpos($string, '_')) {
349
            $str = mb_strpos($string, '_');
350
            if (false !== $str) {
351
                $ret = mb_substr($string, $str + 1, mb_strlen($string));
352
353
                return $ret;
354
            }
355
        }
356
357
        return $string;
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 = '')
466
    {
467
        return "{$t}{$string}\n";
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');
488
        $authorMail       = $module->getVar('mod_author_mail');
489
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
490
        $license          = $module->getVar('mod_license');
491
        //$subversion       = $module->getVar('mod_subversion');
492
        //$date             = date('D Y-m-d H:i:s');
493
        if (null === $noPhpFile) {
494
            $ret = "<?php";
495
            $ret .= "\n\ndeclare(strict_types=1);\n";
496
        } elseif (\is_string($noPhpFile)) {
497
            $ret = $noPhpFile;
498
        } else {
499
            $ret = '';
500
        }
501
        $ret .= "\n{$namespace}/*\n";
502
503
504
        $filename = TDMC_CLASS_PATH . '/Files/Docs/license.txt';
505
        $handle   = fopen($filename, 'rb');
506
        $data     = fread($handle, filesize($filename));
507
        fclose($handle);
508
        $ret       .= $data . "\n";
509
        $ret       .= "*/\n";
510
        $copyright = [
511
            $name           => 'module for xoops',
512
            ''              => '',
513
            '@copyright  '  => '   2021 XOOPS Project (https://xoops.org)',
514
            '@license   '   => "    {$license}",
515
            '@package   '   => "    {$dirname}",
516
            '@since    '    => "     {$since}",
517
            '@min_xoops   ' => "  {$minXoops}",
518
            '@author    '   => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
519
//            '@version    '  => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$",
520
        ];
521
        $ret       .= $pc->getPhpCodeCommentMultiLine($copyright);
522
523
        return $ret;
524
    }
525
526
    /**
527
     * @public function renderFile
528
     * @param null
529
     * @return string
530
     */
531
    public function renderFile()
532
    {
533
        $fileName   = $this->getFileName();
534
        $path       = $this->getUploadPath() . '/' . $fileName;
535
        $created    = $this->getCreated();
536
        $notCreated = $this->getNotCreated();
537
        $folderName = $this->getFolderName();
538
        $mode       = $this->getMode();
539
        $ret        = '';
540
        if (!$this->xf->__construct($path, true)) {
541
            // Force to create file if not exist
542
            if ($this->xf->open($mode, true)) {
543
                if ($this->xf->writable()) {
544
                    // Integration of the content in the file
545
                    if (!$this->xf->write($this->getContent(), $mode, true)) {
546
                        $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

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