Completed
Pull Request — master (#72)
by Gino
04:14
created

TDMCreateFile::getFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $tdmcreate = null;
80
81
    /*
82
    * @var string
83
    */
84
    private $phpcode = 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 $string
380
    */
381
    /**
382
     * @param $string
383
     *
384
     * @return string
385
     */
386
    public function getRightString($string)
387
    {
388
        if (strpos($string, '_')) {
389
            $str = strpos($string, '_');
390
            if ($str !== false) {
391
                $ret = substr($string, $str + 1, strlen($string));
392
393
                return $ret;
394
            }
395
        }
396
397
        return $string;
398
    }
399
400
    /*
401
     *  @public function getCamelCase
402
     *  @param $string
403
     */
404
    /**
405
     * @param $string
406
     * @param $ucfirst
407
     * @param $lcfirst
408
     *
409
     * @return string
410
     */
411
    public function getCamelCase($string, $ucfirst = false, $lcfirst = false)
412
    {
413
        $rightString = $this->getRightString($string);
414
        $leftString = $this->getLeftString($string);
415
        if ($ucfirst) {
416
            $ret = $this->getUcfirst($leftString).$this->getUcfirst($rightString);
417
418
            return $ret;
419
        }
420
        if ($lcfirst) {
421
            $ret = $this->getlcfirst($leftString).$this->getUcfirst($rightString);
422
423
            return $ret;
424
        }
425
426
        return $string;
427
    }
428
429
    /*
430
     *  @public function getUcfirst
431
     *  @param $string
432
     */
433
    /**
434
     * @param $string
435
     *
436
     * @return string
437
     */
438
    public function getUcfirst($string)
439
    {
440
        return ucfirst($string);
441
    }
442
443
    /*
444
     *  @public function getLcfirst
445
     *  @param $string
446
     */
447
    /**
448
     * @param $string
449
     *
450
     * @return string
451
     */
452
    public function getLcfirst($string)
453
    {
454
        return lcfirst($string);
455
    }
456
457
    /*
458
     *  @public function getStrToUpper
459
     *  @param $string
460
     */
461
    /**
462
     * @param $string
463
     *
464
     * @return string
465
     */
466
    public function getStrToUpper($string)
467
    {
468
        return strtoupper($string);
469
    }
470
471
    /*
472
     *  @public function getStrToLower
473
     *  @param $string
474
     */
475
    /**
476
     * @param $string
477
     *
478
     * @return string
479
     */
480
    public function getStrToLower($string)
481
    {
482
        return strtolower($string);
483
    }
484
485
    /*
486
    *  @public function getInclude
487
    *  @param $filename
488
    */
489
    /**
490
     * @return string
491
     */
492
    public function getInclude($filename = 'header')
493
    {
494
        return "include __DIR__ . '/{$filename}.php';\n";
495
    }
496
497
    /*
498
    *  @public function getIncludeOnce
499
    *  @param $filename
500
    */
501
    /**
502
     * @return string
503
     */
504
    public function getIncludeOnce($filename = 'header')
505
    {
506
        return "include_once __DIR__ . '/{$filename}.php';\n";
507
    }
508
509
    /*
510
     * @public function getCommentLine
511
     * @param $string     
512
     * @param $var
513
     *
514
     * @return string
515
     */
516
    public function getCommentLine($string = '', $var = '')
517
    {
518
        $value = !empty($var) ? ' '.$var : '';
519
        $ret = "// {$string}{$value}\n";
520
521
        return $ret;
522
    }
523
524
    /**
525
     * @private function getHeaderComment
526
     *
527
     * @param $comment
528
     *
529
     * @return string
530
     */
531
    public function getHeaderComment($comment)
532
    {
533
        return "// ------------------- {$comment} ------------------- //\n";
534
    }
535
536
    /*
537
     * @public function getSimpleString
538
     * @param $string     
539
     *
540
     * @return string
541
     */
542
    public function getSimpleString($string)
543
    {
544
        return "{$string}\n";
545
    }
546
547
    /*
548
    *  @public function getHeaderFilesComments
549
    *  @param string $module
550
    *  @param string $fileName
551
    */
552
    /**
553
     * @param $module
554
     * @param $fileName
555
     * @param $noPhpFile
556
     *
557
     * @return string
558
     */
559
    public function getHeaderFilesComments($module, $fileName, $noPhpFile = null)
560
    {
561
        $name = $module->getVar('mod_name');
562
        $dirname = $module->getVar('mod_dirname');
563
        $version = $module->getVar('mod_version');
564
        $since = $module->getVar('mod_since');
565
        $minXoops = $module->getVar('mod_min_xoops');
566
        $author = $module->getVar('mod_author');
567
        $credits = $module->getVar('mod_credits');
568
        $authorMail = $module->getVar('mod_author_mail');
569
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
570
        $license = $module->getVar('mod_license');
571
        $subversion = $module->getVar('mod_subversion');
572
        $date = date('D Y-m-d H:i:s');
573
        if (is_null($noPhpFile)) {
574
            $ret = "<?php\n";
575
            $ret .= "/*\n";
576
        } elseif (is_string($noPhpFile)) {
577
            $ret = $noPhpFile;
578
            $ret .= "/*\n";
579
        } else {
580
            $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...
581
        }
582
        $filename = TDMC_CLASSES_PATH.'/files/docs/license.txt';
583
        $handle = fopen($filename, 'rb');
584
        $data = fread($handle, filesize($filename));
585
        fclose($handle);
586
        $ret .= $data."\n";
587
        $ret .= "*/\n";
588
        $copyright = array($name => 'module for xoops', '' => '', '@copyright  ' => '   module for xoops', '@license   ' => "    {$license}", '@package   ' => "    {$dirname}",
589
                            '@since    ' => "     {$since}", '@min_xoops   ' => "  {$minXoops}", '@author    ' => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
590
                            '@version    ' => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$", );
591
        $ret .= TDMCreatePhpCode::getInstance()->getPhpCodeCommentMultiLine($copyright);
592
593
        return $ret;
594
    }
595
596
    /*
597
    *  @public function renderFile
598
    *  @param null
599
    */
600
    /**
601
     * @return string
602
     */
603
    public function renderFile()
604
    {
605
        $fileName = $this->getFileName();
606
        $path = $this->getUploadPath().'/'.$fileName;
607
        $created = $this->getCreated();
608
        $notCreated = $this->getNotCreated();
609
        $folderName = $this->getFolderName();
610
        $mode = $this->getMode();
611
        $ret = '';
612
        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...
613
            // Force to create file if not exist
614
            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...
615
                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...
616
                    // Integration of the content in the file
617
                    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...
618
                        $ret .= sprintf($notCreated, $fileName, $folderName);
619
                        $GLOBALS['xoopsTpl']->assign('created', false);
620
                        exit();
621
                    }
622
                    // Created
623
                    $ret .= sprintf($created, $fileName, $folderName);
624
                    $GLOBALS['xoopsTpl']->assign('created', true);
625
                    $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...
626
                } else {
627
                    $ret .= sprintf($notCreated, $fileName, $folderName);
628
                    $GLOBALS['xoopsTpl']->assign('created', false);
629
                }
630
            } else {
631
                $ret .= sprintf($notCreated, $fileName, $folderName);
632
                $GLOBALS['xoopsTpl']->assign('created', false);
633
            }
634
        }
635
636
        return $ret;
637
    }
638
}
639