CreateFile::getCamelCase()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files;
4
5
use XoopsModules\Tdmcreate;
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
 * tdmcreate module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops http://www.txmodxoops.org
25
 *
26
 */
27
xoops_load('XoopsFile');
28
29
/**
30
 * Class CreateFile.
31
 */
32
class CreateFile extends CreateTableFields
33
{
34
    /**
35
     * @var string
36
     */
37
    private $xf = null;
38
39
    /**
40
     * "fileName" attribute of the files.
41
     *
42
     * @var mixed
43
     */
44
    private $fileName = null;
45
46
    /**
47
     * "subdir" attribute of the directories.
48
     *
49
     * @var string
50
     */
51
    private $subdir = null;
52
53
    /**
54
     * "uploadPath" attribute of the files.
55
     *
56
     * @var string
57
     */
58
    private $uploadPath = null;
59
60
    /**
61
     * @var string
62
     */
63
    private $content = null;
64
65
    /**
66
     * @var mixed
67
     */
68
    private $created = null;
69
70
    /**
71
     * @var mixed
72
     */
73
    private $notCreated = null;
74
75
    /**
76
     * @var string
77
     */
78
    private $mode = null;
79
80
    /**
81
     * @var string
82
     */
83
    protected $phpcode = null;
84
85
    /**
86
     * @var string
87
     */
88
    protected $htmlcode;
89
90
    /**
91
     * @public function constructor
92
     * @param null
93
     */
94
    public function __construct()
95
    {
96
        parent::__construct();
97
        $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...
98
    }
99
100
    /**
101
     * @public static function getInstance
102
     * @param null
103
     * @return Tdmcreate\Files\CreateFile
104
     */
105
    public static function getInstance()
106
    {
107
        static $instance = false;
108
        if (!$instance) {
109
            $instance = new self();
110
        }
111
112
        return $instance;
113
    }
114
115
    /**
116
     * @public function create
117
     *
118
     * @param $moduleDirname
119
     * @param $subdir
120
     * @param $fileName
121
     * @param $content
122
     * @param $created
123
     * @param $notCreated
124
     * @param $mode
125
     */
126
    public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+')
127
    {
128
        $this->setFileName($fileName);
129
        $this->created    = $created;
130
        $this->notCreated = $notCreated;
131
        $this->setMode($mode);
132
        $this->setRepositoryPath($moduleDirname);
133
        if (!empty($content) && is_string($content)) {
134
            $this->setContent($content);
135
        }
136
        if (isset($subdir) && is_string($subdir)) {
137
            $this->setSubDir($subdir);
138
        }
139
    }
140
141
    /**
142
     * @public function write
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 "require __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 "require_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        $noPhpFile
469
     *
470
     * @param string $namespace
471
     * @return string
472
     */
473
    public function getHeaderFilesComments($module, $noPhpFile = null, $namespace = '')
474
    {
475
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
476
        $name             = $module->getVar('mod_name');
477
        $dirname          = $module->getVar('mod_dirname');
478
        $version          = $module->getVar('mod_version');
0 ignored issues
show
Unused Code introduced by
The assignment to $version is dead and can be removed.
Loading history...
479
        $since            = $module->getVar('mod_since');
480
        $minXoops         = $module->getVar('mod_min_xoops');
481
        $author           = $module->getVar('mod_author');
482
        $credits          = $module->getVar('mod_credits');
0 ignored issues
show
Unused Code introduced by
The assignment to $credits is dead and can be removed.
Loading history...
483
        $authorMail       = $module->getVar('mod_author_mail');
484
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
485
        $license          = $module->getVar('mod_license');
486
        $subversion       = $module->getVar('mod_subversion');
0 ignored issues
show
Unused Code introduced by
The assignment to $subversion is dead and can be removed.
Loading history...
487
        $date             = date('D Y-m-d H:i:s');
0 ignored issues
show
Unused Code introduced by
The assignment to $date is dead and can be removed.
Loading history...
488
        if (null === $noPhpFile) {
489
            $ret = "<?php";
490
        } elseif (is_string($noPhpFile)) {
491
            $ret = $noPhpFile;
492
        } else {
493
            $ret = '';
494
        }
495
        $ret .= "\n{$namespace}/*\n";
496
497
498
        $filename = TDMC_CLASS_PATH . '/files/docs/license.txt';
499
        $handle   = fopen($filename, 'rb');
500
        $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

500
        $data     = fread(/** @scrutinizer ignore-type */ $handle, filesize($filename));
Loading history...
501
        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

501
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
502
        $ret       .= $data . "\n";
503
        $ret       .= "*/\n";
504
        $copyright = [
505
            $name           => 'module for xoops',
506
            ''              => '',
507
            '@copyright  '  => '  2020 XOOPS Project (https://xooops.org)',
508
            '@license   '   => "    {$license}",
509
            '@package   '   => "    {$dirname}",
510
            '@since    '    => "     {$since}",
511
            '@min_xoops   ' => "  {$minXoops}",
512
            '@author    '   => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
513
//            '@version    '  => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$",
514
        ];
515
        $ret       .= $pc->getPhpCodeCommentMultiLine($copyright);
516
517
        return $ret;
518
    }
519
520
    /**
521
     * @public function renderFile
522
     * @param null
523
     * @return string
524
     */
525
    public function renderFile()
526
    {
527
        $fileName   = $this->getFileName();
528
        $path       = $this->getUploadPath() . '/' . $fileName;
529
        $created    = $this->getCreated();
530
        $notCreated = $this->getNotCreated();
531
        $folderName = $this->getFolderName();
532
        $mode       = $this->getMode();
533
        $ret        = '';
534
        if (!$this->xf->__construct($path, true)) {
535
            // Force to create file if not exist
536
            if ($this->xf->open($mode, true)) {
537
                if ($this->xf->writable()) {
538
                    // Integration of the content in the file
539
                    if (!$this->xf->write($this->getContent(), $mode, true)) {
540
                        $ret .= sprintf($notCreated, $fileName, $folderName);
541
                        $GLOBALS['xoopsTpl']->assign('created', false);
542
                        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...
543
                    }
544
                    // Created
545
                    $ret .= sprintf($created, $fileName, $folderName);
546
                    $GLOBALS['xoopsTpl']->assign('created', true);
547
                    $this->xf->close();
548
                } else {
549
                    $ret .= sprintf($notCreated, $fileName, $folderName);
550
                    $GLOBALS['xoopsTpl']->assign('created', false);
551
                }
552
            } else {
553
                $ret .= sprintf($notCreated, $fileName, $folderName);
554
                $GLOBALS['xoopsTpl']->assign('created', false);
555
            }
556
        }
557
558
        return $ret;
559
    }
560
}
561