Completed
Pull Request — master (#45)
by Gino
05:23
created

TDMCreateFile::getStrToUpper()   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 31 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
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
xoops_load('XoopsFile');
27
28
/**
29
 * Class TDMCreateFile.
30
 */
31
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...
32
{
33
    /*
34
    * @var string
35
    */
36
    private $xoopsFile;
37
38
    /**
39
     * "filename" attribute of the files.
40
     *
41
     * @var mixed
42
     */
43
    private $fileName;
0 ignored issues
show
Unused Code introduced by
The property $fileName is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
44
45
    /**
46
     * "path" attribute of the files.
47
     *
48
     * @var string
49
     */
50
    private $path;
0 ignored issues
show
Unused Code introduced by
The property $path is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
51
52
    /**
53
     * "uploadPath" attribute of the files.
54
     *
55
     * @var string
56
     */
57
    private $uploadPath;
58
59
    /**
60
     * "folder_name" attribute of the files.
61
     *
62
     * @var string
63
     */
64
    private $folderName;
0 ignored issues
show
Unused Code introduced by
The property $folderName is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
65
66
    /*
67
    * @var string
68
    */
69
    private $content;
70
71
    /*
72
    * @var mixed
73
    */
74
    private $created = false;
75
76
    /*
77
    * @var mixed
78
    */
79
    private $notCreated = false;
80
81
    /*
82
    * @var string
83
    */
84
    private $mode;
85
86
    /*
87
    * @var string
88
    */
89
    protected $tdmcfile;
90
91
    /*
92
    *  @public function constructor
93
    *  @param null
94
    */
95
    /**
96
     *
97
     */
98
    public function __construct()
99
    {
100
        parent::__construct();
101
        $this->xoopsFile = XoopsFile::getHandler();
102
        $this->tdmcreate = TDMCreateHelper::getInstance();
103
    }
104
105
    /*
106
    *  @public static function &getInstance
107
    *  @param null
108
    */
109
    /**
110
     * @return TDMCreateFile
111
     */
112
    public static function &getInstance()
113
    {
114
        static $instance = false;
115
        if (!$instance) {
116
            $instance = new self();
117
        }
118
119
        return $instance;
120
    }
121
122
    /**
123
     * @public function create
124
     *
125
     * @param $moduleDirname
126
     * @param $subdir
127
     * @param $fileName
128
     * @param $content
129
     * @param $created
130
     * @param $notCreated
131
     * @param $mode
132
     */
133
    public function create($moduleDirname, $subdir = null, $fileName, $content = '', $created = false, $notCreated = false, $mode = 'w+')
134
    {
135
        $this->setFileName($fileName);
136
        $this->created = $created;
137
        $this->notCreated = $notCreated;
138
        $this->setMode($mode);
139
        $this->setRepositoryPath($moduleDirname);
140
        if (!empty($content) && is_string($content)) {
141
            $this->setContent($content);
142
        }
143
        if (isset($subdir) && is_string($subdir)) {
144
            $this->setSubDir($subdir);
145
        }
146
    }
147
148
    /*
149
    *  @public function write
150
    *  @param string $module
151
    *  @param string $fileName
152
    */
153
    /**
154
     * @param $module
155
     * @param $fileName
156
     */
157
    public function write($module, $fileName)
158
    {
159
        $this->setModule($module);
160
        $this->setFileName($fileName);
161
    }
162
163
    /*
164
    *  @private function setRepositoryPath
165
    *  @param string $moduleDirname
166
    */
167
    /**
168
     * @param $moduleDirname
169
     */
170
    private function setRepositoryPath($moduleDirname)
171
    {
172
        $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH.DIRECTORY_SEPARATOR.$moduleDirname;
173
    }
174
175
    /*
176
    *  @private function getRepositoryPath
177
    *  @param null
178
    */
179
    /**
180
     * @return string
181
     */
182
    private function getRepositoryPath()
183
    {
184
        return $this->uploadPath;
185
    }
186
187
    /*
188
    *  @private function setSubDir
189
    *  @param string $subdir
190
    */
191
    /**
192
     * @param $subdir
193
     */
194
    private function setSubDir($subdir)
195
    {
196
        $this->subdir = $subdir;
0 ignored issues
show
Bug introduced by
The property subdir does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
197
    }
198
199
    /*
200
    *  @private function getSubDir
201
    *  @param null
202
    */
203
    private function getSubDir()
204
    {
205
        return $this->subdir;
206
    }
207
208
    /**
209
     * public function setFileName.
210
     *
211
     * @param $fileName
212
     *
213
     * @internal param mixed $file_name
214
     */
215
    public function setFileName($fileName)
216
    {
217
        $this->filename = $fileName;
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist. Did you mean fileName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
218
    }
219
220
    /*
221
    *  @public function getFileName
222
    *  @param null
223
    */
224
    /**
225
     * @return mixed
226
     */
227
    public function getFileName()
228
    {
229
        return $this->filename;
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist. Did you mean fileName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
230
    }
231
232
    /*
233
    *  @private function setContent
234
    *  @param string $content
235
    */
236
    /**
237
     * @param $content
238
     */
239
    private function setContent($content)
240
    {
241
        $this->content = $content;
242
    }
243
244
    /*
245
    *  @private function setContent
246
    *  @param null
247
    */
248
    /**
249
     */
250
    private function getContent()
251
    {
252
        return $this->content;
253
    }
254
255
    /*
256
    *  @private function getFolderName
257
    *  @param null
258
    */
259
    /**
260
     * @return string
261
     */
262
    private function getFolderName()
263
    {
264
        $path = $this->getUploadPath();
265
        if (strrpos($path, '\\')) {
266
            $str = strrpos($path, '\\');
267
            if ($str !== false) {
268
                return substr($path, $str + 1, strlen($path));
269
            } else {
270
                return substr($path, $str, strlen($path));
271
            }
272
        }
273
        //return 'root module';
274
        return;
275
    }
276
277
    /*
278
    *  @public function getUploadPath
279
    *  @param null
280
    */
281
    /**
282
     * @return string
283
     */
284
    private function getUploadPath()
285
    {
286
        if ($this->getSubDir() != null) {
287
            $ret = $this->getRepositoryPath().DIRECTORY_SEPARATOR.$this->getSubDir();
288
        } else {
289
            $ret = $this->getRepositoryPath();
290
        }
291
292
        return $ret;
293
    }
294
295
    /*
296
    *  @private function getCreated
297
    *  @param null
298
    */
299
    /**
300
     * @return bool
301
     */
302
    private function getCreated()
303
    {
304
        return $this->created;
305
    }
306
307
    /*
308
    *  @private function getNotCreated
309
    *  @param null
310
    */
311
    /**
312
     * @return bool
313
     */
314
    private function getNotCreated()
315
    {
316
        return $this->notCreated;
317
    }
318
319
    /*
320
    *  @private function setMode
321
    *  @param string $mode
322
    */
323
    /**
324
     * @param $mode
325
     */
326
    private function setMode($mode)
327
    {
328
        $this->mode = $mode;
329
    }
330
331
    /*
332
    *  @private function getMode
333
    *  @param null
334
    */
335
    /**
336
     */
337
    private function getMode()
338
    {
339
        return $this->mode;
340
    }
341
342
    /*
343
    *  @public function getLanguage
344
    *  @param string $moduleDirname
345
    *  @param string $prefix
346
    *  @param string $suffix
347
    */
348
    /**
349
     * @param        $moduleDirname
350
     * @param string $prefix
351
     * @param string $suffix
352
     *
353
     * @return string
354
     */
355
    public function getLanguage($moduleDirname, $prefix = '', $suffix = '')
356
    {
357
        $lang = '_'.$prefix.'_'.strtoupper($moduleDirname);
358
        $ret = $lang;
359
        if (!empty($suffix) || $suffix != '_') {
360
            $ret = $lang.'_'.$suffix;
361
        } elseif ($suffix == '_') {
362
            $ret = $lang.'_';
363
        }
364
365
        return $ret;
366
    }
367
368
    /*
369
    *  @public function getLeftString
370
    *  @param string $string
371
    */
372
    /**
373
     * @param $string
374
     *
375
     * @return string
376
     */
377
    public function getLeftString($string)
378
    {
379
        return substr($string, 0, strpos($string, '_'));
380
    }
381
382
    /*
383
    *  @public function getRightString
384
    *  @param string $string
385
    */
386
    /**
387
     * @param $string
388
     *
389
     * @return string
390
     */
391
    public function getRightString($string)
392
    {
393 View Code Duplication
        if (strpos($string, '_')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
394
            $str = strpos($string, '_');
395
            if ($str !== false) {
396
                $ret = substr($string, $str + 1, strlen($string));
397
398
                return $ret;
399
            }
400
        }
401
402
        return $string;
403
    }
404
405
    /*
406
     *  @public function getCamelCase
407
     *  @param $string
408
     */
409
    /**
410
     * @param $string
411
     * @param $ucfirst
412
     * @param $lcfirst
413
     *
414
     * @return string
415
     */
416
    public function getCamelCase($string, $ucfirst = false, $lcfirst = false)
417
    {
418
        $rightString = $this->getRightString($string);
419
        $leftString = $this->getLeftString($string);
420
        if ($ucfirst) {
421
            $ret = $this->getUcfirst($leftString).$this->getUcfirst($rightString);
422
423
            return $ret;
424
        }
425
        if ($lcfirst) {
426
            $ret = $this->getlcfirst($leftString).$this->getUcfirst($rightString);
427
428
            return $ret;
429
        }
430
431
        return $string;
432
    }
433
434
    /*
435
     *  @public function getUcfirst
436
     *  @param $string
437
     */
438
    /**
439
     * @param $string
440
     *
441
     * @return string
442
     */
443
    public function getUcfirst($string)
444
    {
445
        return ucfirst($string);
446
    }
447
448
    /*
449
     *  @public function getLcfirst
450
     *  @param $string
451
     */
452
    /**
453
     * @param $string
454
     *
455
     * @return string
456
     */
457
    public function getLcfirst($string)
458
    {
459
        return lcfirst($string);
460
    }
461
462
    /*
463
     *  @public function getStrToUpper
464
     *  @param $string
465
     */
466
    /**
467
     * @param $string
468
     *
469
     * @return string
470
     */
471
    public function getStrToUpper($string)
472
    {
473
        return strtoupper($string);
474
    }
475
476
    /*
477
     *  @public function getStrToLower
478
     *  @param $string
479
     */
480
    /**
481
     * @param $string
482
     *
483
     * @return string
484
     */
485
    public function getStrToLower($string)
486
    {
487
        return strtolower($string);
488
    }
489
490
    /*
491
    *  @public function getInclude
492
    *  @param $filename
493
    */
494
    /**
495
     * @return string
496
     */
497
    public function getInclude($filename = 'header')
498
    {
499
        return "include __DIR__ . '/{$filename}.php';\n";
500
    }
501
502
    /*
503
    *  @public function getIncludeOnce
504
    *  @param $filename
505
    */
506
    /**
507
     * @return string
508
     */
509
    public function getIncludeOnce($filename = 'header')
510
    {
511
        return "include_once __DIR__ . '/{$filename}.php';\n";
512
    }
513
514
    /*
515
     *  @public function getCommentLine
516
     *  @param $string
517
     */
518
    /**
519
     * @param $string
520
     *
521
     * @return string
522
     */
523
    public function getCommentLine($string)
524
    {
525
        $ret = <<<EOT
526
// {$string}\n
527
EOT;
528
529
        return $ret;
530
    }
531
532
    /*
533
     * @public function getSimpleString
534
     * @param $string     
535
     *
536
     * @return string
537
     */
538
    public function getSimpleString($string)
539
    {
540
        $ret = <<<EOT
541
{$string}\n
542
EOT;
543
544
        return $ret;
545
    }
546
	
547
	/*
548
     * @public function getRemoveCarriageReturn
549
     * @param $string     
550
     *
551
     * @return string
552
     */
553
    public function getRemoveCarriageReturn($string)
554
    {
555
        return str_replace(array("\n", "\r"), '', $string);
556
    }	
557
558
    /*
559
    *  @public function getHeaderFilesComments
560
    *  @param string $module
561
    *  @param string $fileName
562
    */
563
    /**
564
     * @param $module
565
     * @param $fileName
566
     * @param $noPhpFile
567
     *
568
     * @return string
569
     */
570
    public function getHeaderFilesComments($module, $fileName, $noPhpFile = null)
571
    {
572
        $name = $module->getVar('mod_name');
573
        $dirname = $module->getVar('mod_dirname');
574
        $version = $module->getVar('mod_version');
575
        $since = $module->getVar('mod_since');
576
        $minXoops = $module->getVar('mod_min_xoops');
577
        $author = $module->getVar('mod_author');
578
        $credits = $module->getVar('mod_credits');
579
        $authorMail = $module->getVar('mod_author_mail');
580
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
581
        $license = $module->getVar('mod_license');
582
        $subversion = $module->getVar('mod_subversion');
583
        $date = date('D Y-m-d H:i:s');
584
        if (is_null($noPhpFile)) {
585
            $ret = <<<EOT
586
<?php
587
/*\n
588
EOT;
589
        } elseif (is_string($noPhpFile)) {
590
            $ret = <<<EOT
591
{$noPhpFile}
592
/*\n
593
EOT;
594
        } else {
595
            $ret = <<<EOT
596
/*\n
597
EOT;
598
        }
599
        $ret .= <<<EOT
600
 You may not change or alter any portion of this comment or credits
601
 of supporting developers from this source code or any supporting source code
602
 which is considered copyrighted (c) material of the original comment or credit authors.
603
604
 This program is distributed in the hope that it will be useful,
605
 but WITHOUT ANY WARRANTY; without even the implied warranty of
606
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
607
*/
608
/**
609
 * {$name} module for xoops
610
 *
611
 * @copyright       The XOOPS Project (http://xoops.org)
612
 * @license         {$license}
613
 * @package         {$dirname}
614
 * @since           {$since}
615
 * @min_xoops       {$minXoops}
616
 * @author          {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>
617
 * @version         \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$
618
 */\n
619
EOT;
620
621
        return $ret;
622
    }
623
624
    /*
625
    *  @public function renderFile
626
    *  @param null
627
    */
628
    /**
629
     * @return string
630
     */
631
    public function renderFile()
0 ignored issues
show
Coding Style introduced by
renderFile uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
632
    {
633
        $fileName = $this->getFileName();
634
        $path = $this->getUploadPath().DIRECTORY_SEPARATOR.$fileName;
635
        $created = $this->getCreated();
636
        $notCreated = $this->getNotCreated();
637
        $folderName = $this->getFolderName();
638
        $mode = $this->getMode();
639
        $ret = '';
640
        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...
641
            // Force to create file if not exist
642
            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...
643
                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...
644
                    // Integration of the content in the file
645
                    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...
646
                        $ret .= sprintf($notCreated, $fileName, $folderName);
647
                        $GLOBALS['xoopsTpl']->assign('created', false);
648
                        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method renderFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
649
                    }
650
                    // Created
651
                    $ret .= sprintf($created, $fileName, $folderName);
652
                    $GLOBALS['xoopsTpl']->assign('created', true);
653
                    $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...
654
                } else {
655
                    $ret .= sprintf($notCreated, $fileName, $folderName);
656
                    $GLOBALS['xoopsTpl']->assign('created', false);
657
                }
658
            } else {
659
                $ret .= sprintf($notCreated, $fileName, $folderName);
660
                $GLOBALS['xoopsTpl']->assign('created', false);
661
            }
662
        }
663
664
        return $ret;
665
    }
666
}
667