Passed
Push — master ( e5334d...a8ea98 )
by Gino
06:21 queued 02:59
created

TDMCreateFile::getCamelCase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: TDMCreateFile.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
xoops_load('XoopsFile');
25
/**
26
 * Class TDMCreateFile.
27
 */
28
class TDMCreateFile extends TDMCreateTableFields
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    * @var string
32
    */
33
    private $xoopsFile = null;
34
35
    /**
36
     * "fileName" attribute of the files.
37
     *
38
     * @var mixed
39
     */
40
    private $fileName = null;
41
42
    /**
43
     * "subdir" attribute of the directories.
44
     *
45
     * @var string
46
     */
47
    private $subdir = null;
48
49
    /**
50
     * "uploadPath" attribute of the files.
51
     *
52
     * @var string
53
     */
54
    private $uploadPath = null;
55
56
    /*
57
    * @var string
58
    */
59
    private $content = null;
60
61
    /*
62
    * @var mixed
63
    */
64
    private $created = null;
65
66
    /*
67
    * @var mixed
68
    */
69
    private $notCreated = null;
70
71
    /*
72
    * @var string
73
    */
74
    private $mode = null;
75
76
    /*
77
    * @var string
78
    */
79
    protected $phpcode = null;
80
81
    /*
82
    * @var string
83
    */
84
    private $tdmcreate = null;
85
86
    /*
87
    *  @public function constructor
88
    *  @param null
89
    */
90
    /**
91
     *
92
     */
93
    public function __construct()
94
    {
95
        parent::__construct();
96
        $this->xoopsFile = XoopsFile::getHandler();
97
        $this->tdmcreate = TDMCreateHelper::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateHelper::getInstance() of type object<TDMCreateHelper> is incompatible with the declared type string of property $tdmcreate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
559
        }
560
        $filename = TDMC_CLASSES_PATH.'/files/docs/license.txt';
561
        $handle = fopen($filename, 'rb');
562
        $data = fread($handle, filesize($filename));
563
        fclose($handle);
564
        $ret .= $data."\n";
565
        $ret .= "*/\n";
566
        $copyright = array($name => 'module for xoops', '' => '', '@copyright  ' => '   module for xoops', '@license   ' => "    {$license}", '@package   ' => "    {$dirname}",
567
                            '@since    ' => "     {$since}", '@min_xoops   ' => "  {$minXoops}", '@author    ' => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
568
                            '@version    ' => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$", );
569
        $ret .= TDMCreatePhpCode::getInstance()->getPhpCodeCommentMultiLine($copyright);
570
571
        return $ret;
572
    }
573
574
    /*
575
    *  @public function renderFile
576
    *  @param null
577
    */
578
    /**
579
     * @return string
580
     */
581
    public function renderFile()
582
    {
583
        $fileName = $this->getFileName();
584
        $path = $this->getUploadPath().'/'.$fileName;
585
        $created = $this->getCreated();
586
        $notCreated = $this->getNotCreated();
587
        $folderName = $this->getFolderName();
588
        $mode = $this->getMode();
589
        $ret = '';
590
        if (!$this->xoopsFile->__construct($path, true)) {
0 ignored issues
show
Bug introduced by
The method __construct cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
591
            // Force to create file if not exist
592
            if ($this->xoopsFile->open($mode, true)) {
0 ignored issues
show
Bug introduced by
The method open cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
593
                if ($this->xoopsFile->writable()) {
0 ignored issues
show
Bug introduced by
The method writable cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
594
                    // Integration of the content in the file
595
                    if (!$this->xoopsFile->write($this->getContent(), $mode, true)) {
0 ignored issues
show
Bug introduced by
The method write cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
596
                        $ret .= sprintf($notCreated, $fileName, $folderName);
597
                        $GLOBALS['xoopsTpl']->assign('created', false);
598
                        exit();
599
                    }
600
                    // Created
601
                    $ret .= sprintf($created, $fileName, $folderName);
602
                    $GLOBALS['xoopsTpl']->assign('created', true);
603
                    $this->xoopsFile->close();
0 ignored issues
show
Bug introduced by
The method close cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
604
                } else {
605
                    $ret .= sprintf($notCreated, $fileName, $folderName);
606
                    $GLOBALS['xoopsTpl']->assign('created', false);
607
                }
608
            } else {
609
                $ret .= sprintf($notCreated, $fileName, $folderName);
610
                $GLOBALS['xoopsTpl']->assign('created', false);
611
            }
612
        }
613
614
        return $ret;
615
    }
616
}
617