Completed
Push — master ( 097ad2...6ae33d )
by Michael
12s
created

AboutUtility::checkVerPhp()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
 About Utility Class Definition
4
5
 You may not change or alter any portion of this comment or credits of
6
 supporting developers from this source code or any supporting source code
7
 which is considered copyrighted (c) material of the original comment or credit
8
 authors.
9
10
 This program is distributed in the hope that it will be useful, but
11
 WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
/**
15
 * Module:  About
16
 *
17
 * @package      ::    \module\about\class
18
 * @license      http://www.fsf.org/copyleft/gpl.html GNU public license
19
 * @copyright    https://xoops.org 2001-2017 &copy; XOOPS Project
20
 * @author       ZySpec <[email protected]>
21
 * @author       Mamba <[email protected]>
22
 * @since        ::      File available since version 1.54
23
 */
24
25
/**
26
 * AboutUtility
27
 *
28
 * Static utility class to provide common functionality
29
 *
30
 */
31
class AboutUtility
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
     *
35
     * Verifies XOOPS version meets minimum requirements for this module
36
     * @static
37
     * @param XoopsModule $module
0 ignored issues
show
Documentation introduced by
Should the type for parameter $module not be null|XoopsModule?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     * @param null|string $requiredVer
39
     *
40
     * @return bool true if meets requirements, false if not
41
     */
42
    public static function checkVerXoops(XoopsModule $module = null, $requiredVer = null)
43
    {
44
        $moduleDirName = basename(dirname(__DIR__));
45
        if (null === $module) {
46
            $module = XoopsModule::getByDirname($moduleDirName);
47
        }
48
        xoops_loadLanguage('admin', $moduleDirName);
49
        //check for minimum XOOPS version
50
        $currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string
51
        $currArray  = explode('.', $currentVer);
52
        if (null === $requiredVer) {
53
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
54
        }
55
        $reqArray = explode('.', $requiredVer);
56
        $success  = true;
57
        foreach ($reqArray as $k => $v) {
58
            if (isset($currArray[$k])) {
59
                if ($currArray[$k] > $v) {
60
                    break;
61
                } elseif ($currArray[$k] == $v) {
62
                    continue;
63
                } else {
64
                    $success = false;
65
                    break;
66
                }
67
            } else {
68
                if ((int)$v > 0) { // handles versions like x.x.x.0_RC2
69
                    $success = false;
70
                    break;
71
                }
72
            }
73
        }
74
75
        if (false === $success) {
76
            $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
77
        }
78
79
        return $success;
80
    }
81
82
    /**
83
     *
84
     * Verifies PHP version meets minimum requirements for this module
85
     * @static
86
     * @param XoopsModule $module
87
     *
88
     * @return bool true if meets requirements, false if not
89
     */
90
    public static function checkVerPhp(XoopsModule $module)
91
    {
92
        xoops_loadLanguage('admin', $module->dirname());
93
        // check for minimum PHP version
94
        $success = true;
95
        $verNum  = PHP_VERSION;
96
        $reqVer  = $module->getInfo('min_php');
97
        if (false !== $reqVer && '' !== $reqVer) {
98
            if (version_compare($verNum, (string)$reqVer, '<')) {
99
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_PHP, $reqVer, $verNum));
100
                $success = false;
101
            }
102
        }
103
        return $success;
104
    }
105
106
107
    /**
108
     *
109
     * Remove files and (sub)directories
110
     *
111
     * @param string $src source directory to delete
112
     *
113
     * @see Xmf\Module\Helper::getHelper()
114
     * @see Xmf\Module\Helper::isUserAdmin()
115
     *
116
     * @return bool true on success
117
     */
118
    public static function deleteDirectory($src)
0 ignored issues
show
Coding Style introduced by
deleteDirectory 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...
119
    {
120
        // Only continue if user is a 'global' Admin
121 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
122
            return false;
123
        }
124
125
        $success = true;
126
        // remove old files
127
        $dirInfo = new SplFileInfo($src);
128
        // validate is a directory
129
        if ($dirInfo->isDir()) {
130
            $fileList = array_diff(scandir($src, SCANDIR_SORT_NONE), array('..', '.'));
131
            foreach ($fileList as $k => $v) {
132
                $fileInfo = new SplFileInfo("{$src}/{$v}");
133
                if ($fileInfo->isDir()) {
134
                    // recursively handle subdirectories
135
                    if (!$success = self::deleteDirectory($fileInfo->getRealPath())) {
136
                        break;
137
                    }
138
                } else {
139
                    // delete the file
140
                    if (!($success = unlink($fileInfo->getRealPath()))) {
141
                        break;
142
                    }
143
                }
144
            }
145
            // now delete this (sub)directory if all the files are gone
146
            if ($success) {
147
                $success = rmdir($dirInfo->getRealPath());
148
            }
149
        } else {
150
            // input is not a valid directory
151
            $success = false;
152
        }
153
        return $success;
154
    }
155
156
    /**
157
     *
158
     * Recursively remove directory
159
     *
160
     * @todo currently won't remove directories with hidden files, should it?
161
     *
162
     * @param string $src directory to remove (delete)
163
     *
164
     * @return bool true on success
165
     */
166
    public static function rrmdir($src)
0 ignored issues
show
Coding Style introduced by
rrmdir 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...
167
    {
168
        // Only continue if user is a 'global' Admin
169 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
170
            return false;
171
        }
172
173
        // If source is not a directory stop processing
174
        if (!is_dir($src)) {
175
            return false;
176
        }
177
178
        $success = true;
0 ignored issues
show
Unused Code introduced by
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
180
        // Open the source directory to read in files
181
        $iterator = new DirectoryIterator($src);
182
       foreach ($iterator as $fObj) {
183
            if ($fObj->isFile()) {
184
                $filename = $fObj->getPathname();
185
                $fObj = null; // clear this iterator object to close the file
186
                if (!unlink($filename)) {
187
                    return false; // couldn't delete the file
188
                }
189
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
190
                // Try recursively on directory
191
                self::rrmdir($fObj->getPathname());
192
            }
193
        }
194
        $iterator = null;   // clear iterator Obj to close file/directory
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
        return rmdir($src); // remove the directory & return results
196
    }
197
198
    /**
199
     * Recursively move files from one directory to another
200
     *
201
     * @param string $src - Source of files being moved
202
     * @param string $dest - Destination of files being moved
203
     *
204
     * @return bool true on success
205
     */
206
    public static function rmove($src, $dest)
0 ignored issues
show
Coding Style introduced by
rmove 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...
207
    {
208
        // Only continue if user is a 'global' Admin
209 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
210
            return false;
211
        }
212
213
        // If source is not a directory stop processing
214
        if (!is_dir($src)) {
215
            return false;
216
        }
217
218
        // If the destination directory does not exist and could not be created stop processing
219
        if (!is_dir($dest) && !mkdir($dest, 0755)) {
220
            return false;
221
        }
222
223
        // Open the source directory to read in files
224
        $iterator = new DirectoryIterator($src);
225 View Code Duplication
        foreach ($iterator as $fObj) {
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...
226
            if ($fObj->isFile()) {
227
                rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
228
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
229
                // Try recursively on directory
230
                self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
231
//                rmdir($fObj->getPath()); // now delete the directory
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
232
            }
233
        }
234
        $iterator = null;   // clear iterator Obj to close file/directory
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
235
        return rmdir($src); // remove the directory & return results
236
    }
237
238
    /**
239
     * Recursively copy directories and files from one directory to another
240
     *
241
     * @param string $src  - Source of files being moved
242
     * @param string $dest - Destination of files being moved
243
     *
244
     * @see Xmf\Module\Helper::getHelper()
245
     * @see Xmf\Module\Helper::isUserAdmin()
246
     *
247
     * @return bool true on success
248
     */
249
    public static function rcopy($src, $dest)
0 ignored issues
show
Coding Style introduced by
rcopy 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...
250
    {
251
        // Only continue if user is a 'global' Admin
252 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
253
            return false;
254
        }
255
256
        // If source is not a directory stop processing
257
        if (!is_dir($src)) {
258
            return false;
259
        }
260
261
        // If the destination directory does not exist and could not be created stop processing
262
        if (!is_dir($dest) && !mkdir($dest, 0755)) {
263
            return false;
264
        }
265
266
        // Open the source directory to read in files
267
        $iterator = new DirectoryIterator($src);
268 View Code Duplication
        foreach($iterator as $fObj) {
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...
269
            if($fObj->isFile()) {
270
                copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
271
            } else if(!$fObj->isDot() && $fObj->isDir()) {
272
                self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj-getFilename());
273
            }
274
        }
275
        return true;
276
    }
277
}
278