CreateFile   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 521
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 135
dl 0
loc 521
rs 7.44
c 0
b 0
f 0
wmc 52

31 Methods

Rating   Name   Duplication   Size   Complexity  
A getSimpleString() 0 3 1
A getContent() 0 3 1
A getHeaderFilesComments() 0 37 3
A getStrToLower() 0 3 1
A __construct() 0 4 1
A getUcfirst() 0 3 1
A setMode() 0 3 1
A getCreated() 0 3 1
A getLeftString() 0 3 1
A renderFile() 0 34 5
A getFolderName() 0 13 3
A getLanguage() 0 11 4
A getMode() 0 3 1
A getDashComment() 0 3 1
A getCamelCase() 0 12 3
A getIncludeOnce() 0 3 1
A setRepositoryPath() 0 3 1
A create() 0 12 5
A getLcfirst() 0 3 1
A getInclude() 0 3 1
A getFileName() 0 3 1
A setFileName() 0 3 1
A getSubDir() 0 3 1
A getRightString() 0 12 3
A getInstance() 0 8 2
A getRepositoryPath() 0 3 1
A getStrToUpper() 0 3 1
A setSubDir() 0 3 1
A getNotCreated() 0 3 1
A getUploadPath() 0 9 2
A setContent() 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 namespace XoopsModules\Tdmcreate\Files;
2
3
use XoopsModules\Tdmcreate;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
/**
15
 * tdmcreate module.
16
 *
17
 * @copyright       XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
19
 *
20
 * @since           2.5.0
21
 *
22
 * @author          Txmod Xoops http://www.txmodxoops.org
23
 *
24
 * @version         $Id: CreateFile.php 12258 2014-01-02 09:33:29Z timgno $
25
 */
26
xoops_load('XoopsFile');
27
/**
28
 * Class CreateFile.
29
 */
30
class CreateFile extends CreateTableFields
31
{
32
    /**
33
     * @var string
34
     */
35
    private $xf = null;
36
37
    /**
38
     * "fileName" attribute of the files.
39
     *
40
     * @var mixed
41
     */
42
    private $fileName = null;
43
44
    /**
45
     * "subdir" attribute of the directories.
46
     *
47
     * @var string
48
     */
49
    private $subdir = null;
50
51
    /**
52
     * "uploadPath" attribute of the files.
53
     *
54
     * @var string
55
     */
56
    private $uploadPath = null;
57
58
    /**
59
     * @var string
60
     */
61
    private $content = null;
62
63
    /**
64
     * @var mixed
65
     */
66
    private $created = null;
67
68
    /**
69
     * @var mixed
70
     */
71
    private $notCreated = null;
72
73
    /**
74
     * @var string
75
     */
76
    private $mode = null;
77
78
    /**
79
     * @var string
80
     */
81
    protected $phpcode = null;
82
83
    /**
84
     * @var string
85
     */
86
    protected $htmlcode;
87
88
    /**
89
     *  @public function constructor
90
     *  @param null
91
     */
92
    public function __construct()
93
    {
94
        parent::__construct();
95
        $this->xf = \XoopsFile::getHandler();
0 ignored issues
show
Documentation Bug introduced by
It seems like XoopsFile::getHandler() can also be of type XoopsFileHandler. However, the property $xf is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
96
    }
97
98
    /**
99
     *  @public static function getInstance
100
     *  @param null
101
     * @return Tdmcreate\Files\CreateFile
102
     */
103
    public static function getInstance()
104
    {
105
        static $instance = false;
106
        if (!$instance) {
107
            $instance = new self();
108
        }
109
110
        return $instance;
111
    }
112
113
    /**
114
     * @public function create
115
     *
116
     * @param $moduleDirname
117
     * @param $subdir
118
     * @param $fileName
119
     * @param $content
120
     * @param $created
121
     * @param $notCreated
122
     * @param $mode
123
     */
124
    public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+')
125
    {
126
        $this->setFileName($fileName);
127
        $this->created = $created;
128
        $this->notCreated = $notCreated;
129
        $this->setMode($mode);
130
        $this->setRepositoryPath($moduleDirname);
131
        if (!empty($content) && is_string($content)) {
132
            $this->setContent($content);
133
        }
134
        if (isset($subdir) && is_string($subdir)) {
135
            $this->setSubDir($subdir);
136
        }
137
    }
138
139
    /**
140
     *  @public function write
141
     *  @param string $module
142
     *  @param string $fileName
143
     * @param mixed $moduleDirname
144
     */
145
    /*public function write($module, $fileName)
146
    {
147
        $this->setModule($module);
148
        $this->setFileName($fileName);
149
    }*/
150
151
    /**
152
     *  @private function setRepositoryPath
153
     *  @param string $moduleDirname
154
     */
155
    private function setRepositoryPath($moduleDirname)
156
    {
157
        $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH . '/' . $moduleDirname;
158
    }
159
160
    /**
161
     *  @private function getRepositoryPath
162
     *  @param null
163
     * @return string
164
     */
165
    private function getRepositoryPath()
166
    {
167
        return $this->uploadPath;
168
    }
169
170
    /**
171
     * @private function setSubDir
172
     * @param $subdir
173
     */
174
    private function setSubDir($subdir)
175
    {
176
        $this->subdir = $subdir;
177
    }
178
179
    /**
180
     * @private function getSubDir
181
     * @param null
182
     * @return string
183
     */
184
    private function getSubDir()
185
    {
186
        return $this->subdir;
187
    }
188
189
    /**
190
     * public function setFileName.
191
     *
192
     * @param $fileName
193
     */
194
    public function setFileName($fileName)
195
    {
196
        $this->fileName = $fileName;
197
    }
198
199
    /**
200
     *  @public function getFileName
201
     *  @param null
202
     * @return mixed
203
     */
204
    public function getFileName()
205
    {
206
        return $this->fileName;
207
    }
208
209
    /**
210
     * @private function setContent
211
     * @param $content
212
     */
213
    private function setContent($content)
214
    {
215
        $this->content = $content;
216
    }
217
218
    /**
219
     * @private function setContent
220
     * @param null
221
     * @return string
222
     */
223
    private function getContent()
224
    {
225
        return $this->content;
226
    }
227
228
    /**
229
     *  @private function getFolderName
230
     *  @param null
231
     * @return string
232
     */
233
    private function getFolderName()
234
    {
235
        $path = $this->getUploadPath();
236
        if (mb_strrpos($path, '\\')) {
237
            $str = mb_strrpos($path, '\\');
238
            if (false !== $str) {
239
                return mb_substr($path, $str + 1, mb_strlen($path));
240
            }
241
242
            return mb_substr($path, $str, mb_strlen($path));
243
        }
244
        //return 'root module';
245
        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...
246
    }
247
248
    /**
249
     *  @public function getUploadPath
250
     *  @param null
251
     * @return string
252
     */
253
    private function getUploadPath()
254
    {
255
        if (null != $this->getSubDir()) {
256
            $ret = $this->getRepositoryPath() . '/' . $this->getSubDir();
257
        } else {
258
            $ret = $this->getRepositoryPath();
259
        }
260
261
        return $ret;
262
    }
263
264
    /**
265
     *  @private function getCreated
266
     *  @param null
267
     * @return bool
268
     */
269
    private function getCreated()
270
    {
271
        return $this->created;
272
    }
273
274
    /**
275
     *  @private function getNotCreated
276
     *  @param null
277
     * @return bool
278
     */
279
    private function getNotCreated()
280
    {
281
        return $this->notCreated;
282
    }
283
284
    /**
285
     * @private function setMode
286
     * @param $mode
287
     */
288
    private function setMode($mode)
289
    {
290
        $this->mode = $mode;
291
    }
292
293
    /**
294
     * @private function getMode
295
     * @param null
296
     * @return string
297
     */
298
    private function getMode()
299
    {
300
        return $this->mode;
301
    }
302
303
    /**
304
     *  @public function getLanguage
305
     *  @param string $moduleDirname
306
     *  @param string $prefix
307
     *  @param string $suffix
308
     *
309
     * @return string
310
     */
311
    public function getLanguage($moduleDirname, $prefix = '', $suffix = '')
312
    {
313
        $lang = '_' . $prefix . '_' . mb_strtoupper($moduleDirname);
314
        $ret = $lang;
315
        if (!empty($suffix) || '_' !== $suffix) {
316
            $ret = $lang . '_' . $suffix;
317
        } elseif ('_' === $suffix) {
318
            $ret = $lang . '_';
319
        }
320
321
        return $ret;
322
    }
323
324
    /**
325
     *  @public function getLeftString
326
     *  @param string $string
327
     *
328
     * @return string
329
     */
330
    public function getLeftString($string)
331
    {
332
        return mb_substr($string, 0, mb_strpos($string, '_'));
333
    }
334
335
    /**
336
     *  @public function getRightString
337
     *  @param $string
338
     *
339
     * @return string
340
     */
341
    public function getRightString($string = null)
342
    {
343
        if (mb_strpos($string, '_')) {
344
            $str = mb_strpos($string, '_');
345
            if (false !== $str) {
346
                $ret = mb_substr($string, $str + 1, mb_strlen($string));
347
348
                return $ret;
349
            }
350
        }
351
352
        return $string;
353
    }
354
355
    /**
356
     *  @public function getCamelCase
357
     *  @param $string
358
     * @param $ucfirst
359
     * @param $lcfirst
360
     *
361
     * @return string
362
     */
363
    public function getCamelCase($string, $ucfirst = false, $lcfirst = false)
364
    {
365
        $rightString = $this->getRightString($string);
366
        $leftString = $this->getLeftString($string);
367
        if ($ucfirst) {
368
            return $this->getUcfirst($leftString) . $this->getUcfirst($rightString);
369
        }
370
        if ($lcfirst) {
371
            return $this->getLcfirst($leftString) . $this->getUcfirst($rightString);
372
        }
373
374
        return $string;
375
    }
376
377
    /**
378
     *  @public function getUcfirst
379
     *  @param $string
380
     *
381
     * @return string
382
     */
383
    public function getUcfirst($string)
384
    {
385
        return ucfirst($string);
386
    }
387
388
    /**
389
     * @public function getLcfirst
390
     *
391
     * @param $string
392
     * @return string
393
     */
394
    public function getLcfirst($string)
395
    {
396
        return lcfirst($string);
397
    }
398
399
    /**
400
     * @public function getStrToUpper
401
     *
402
     * @param $string
403
     * @return string
404
     */
405
    public function getStrToUpper($string)
406
    {
407
        return mb_strtoupper($string);
408
    }
409
410
    /**
411
     *  @public function getStrToLower
412
     *  @param $string
413
     *
414
     * @return string
415
     */
416
    public function getStrToLower($string)
417
    {
418
        return mb_strtolower($string);
419
    }
420
421
    /**
422
     *  @public function getInclude
423
     *  @param $filename
424
     * @return string
425
     */
426
    public function getInclude($filename = 'header')
427
    {
428
        return "include __DIR__ . '/{$filename}.php';\n";
429
    }
430
431
    /**
432
     *  @public function getIncludeOnce
433
     *  @param $filename
434
     * @return string
435
     */
436
    public function getIncludeOnce($filename = 'header')
437
    {
438
        return "include_once __DIR__ . '/{$filename}.php';\n";
439
    }
440
441
    /**
442
     * @private function getDashComment
443
     *
444
     * @param $comment
445
     *
446
     * @return string
447
     */
448
    public function getDashComment($comment)
449
    {
450
        return "// ------------------- {$comment} ------------------- //\n";
451
    }
452
453
    /**
454
     * @public function getSimpleString
455
     * @param        $string
456
     *
457
     * @param string $t
458
     * @return string
459
     */
460
    public function getSimpleString($string, $t = '')
461
    {
462
        return "{$t}{$string}\n";
463
    }
464
465
    /**
466
     *  @public function getHeaderFilesComments
467
     *  @param string $module
468
     *  @param string $fileName
469
     * @param $noPhpFile
470
     *
471
     * @return string
472
     */
473
    public function getHeaderFilesComments($module, $fileName, $noPhpFile = null)
474
    {
475
        $name = $module->getVar('mod_name');
476
        $dirname = $module->getVar('mod_dirname');
477
        $version = $module->getVar('mod_version');
478
        $since = $module->getVar('mod_since');
479
        $minXoops = $module->getVar('mod_min_xoops');
480
        $author = $module->getVar('mod_author');
481
        $credits = $module->getVar('mod_credits');
482
        $authorMail = $module->getVar('mod_author_mail');
483
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
484
        $license = $module->getVar('mod_license');
485
        $subversion = $module->getVar('mod_subversion');
486
        $date = date('D Y-m-d H:i:s');
487
        if (null === $noPhpFile) {
488
            $ret = "<?php\n";
489
            $ret .= "/*\n";
490
        } elseif (is_string($noPhpFile)) {
491
            $ret = $noPhpFile;
492
            $ret .= "\n/*\n";
493
        } else {
494
            $ret .= "\n/*\n";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret seems to be never defined.
Loading history...
495
        }
496
        $filename = TDMC_CLASS_PATH . '/files/docs/license.txt';
497
        $handle = fopen($filename, 'rb');
498
        $data = fread($handle, filesize($filename));
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

498
        $data = fread(/** @scrutinizer ignore-type */ $handle, filesize($filename));
Loading history...
499
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

499
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
500
        $ret .= $data . "\n";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
501
        $ret .= "*/\n";
502
        $copyright = [
503
            $name => 'module for xoops', '' => '', '@copyright  ' => '   module for xoops', '@license   ' => "    {$license}", '@package   ' => "    {$dirname}",
504
            '@since    ' => "     {$since}", '@min_xoops   ' => "  {$minXoops}", '@author    ' => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
505
            '@version    ' => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$",
506
        ];
507
        $ret .= Tdmcreate\Files\CreatePhpCode::getInstance()->getPhpCodeCommentMultiLine($copyright);
508
509
        return $ret;
510
    }
511
512
    /**
513
     *  @public function renderFile
514
     *  @param null
515
     * @return string
516
     */
517
    public function renderFile()
518
    {
519
        $fileName = $this->getFileName();
520
        $path = $this->getUploadPath() . '/' . $fileName;
521
        $created = $this->getCreated();
522
        $notCreated = $this->getNotCreated();
523
        $folderName = $this->getFolderName();
524
        $mode = $this->getMode();
525
        $ret = '';
526
        if (!$this->xf->__construct($path, true)) {
527
            // Force to create file if not exist
528
            if ($this->xf->open($mode, true)) {
529
                if ($this->xf->writable()) {
530
                    // Integration of the content in the file
531
                    if (!$this->xf->write($this->getContent(), $mode, true)) {
532
                        $ret .= sprintf($notCreated, $fileName, $folderName);
533
                        $GLOBALS['xoopsTpl']->assign('created', false);
534
                        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
535
                    }
536
                    // Created
537
                    $ret .= sprintf($created, $fileName, $folderName);
538
                    $GLOBALS['xoopsTpl']->assign('created', true);
539
                    $this->xf->close();
540
                } else {
541
                    $ret .= sprintf($notCreated, $fileName, $folderName);
542
                    $GLOBALS['xoopsTpl']->assign('created', false);
543
                }
544
            } else {
545
                $ret .= sprintf($notCreated, $fileName, $folderName);
546
                $GLOBALS['xoopsTpl']->assign('created', false);
547
            }
548
        }
549
550
        return $ret;
551
    }
552
}
553