Passed
Branch master (31be8b)
by Gino
03:04
created

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