Completed
Push — master ( f87886...b3b862 )
by Michael
02:18
created

ExtgalleryUtility::checkVerXoops()   D

Complexity

Conditions 9
Paths 40

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 40
nop 2
dl 0
loc 39
rs 4.909
c 0
b 0
f 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 8 and the first side effect is on line 3.

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
require_once __DIR__ . '/../include/config.php';
4
5
/**
6
 * Class ExtgalleryUtility
7
 */
8
class ExtgalleryUtility extends XoopsObject
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...
9
{
10
    /**
11
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
12
     *
13
     * @param string $folder The full path of the directory to check
14
     *
15
     * @return void
16
     */
17
    public static function createFolder($folder)
18
    {
19
        try {
20
            if (!file_exists($folder)) {
21
                if (!mkdir($folder) && !is_dir($folder)) {
22
                    throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
23
                } else {
24
                    file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
25
                }
26
            }
27
        } catch (Exception $e) {
28
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br>';
29
        }
30
    }
31
32
    /**
33
     * @param $file
34
     * @param $folder
35
     * @return bool
36
     */
37
    public static function copyFile($file, $folder)
38
    {
39
        return copy($file, $folder);
40
        //        try {
41
        //            if (!is_dir($folder)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
42
        //                throw new \RuntimeException(sprintf('Unable to copy file as: %s ', $folder));
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
43
        //            } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
44
        //                return copy($file, $folder);
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...
45
        //            }
46
        //        } catch (Exception $e) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
47
        //            echo 'Caught exception: ', $e->getMessage(), "\n", "<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
48
        //        }
49
        //        return false;
50
    }
51
52
    /**
53
     * @param $src
54
     * @param $dst
55
     */
56
    public static function recurseCopy($src, $dst)
57
    {
58
        $dir = opendir($src);
59
        //    @mkdir($dst);
60
        while (false !== ($file = readdir($dir))) {
61
            if (($file !== '.') && ($file !== '..')) {
62
                if (is_dir($src . '/' . $file)) {
63
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
64
                } else {
65
                    copy($src . '/' . $file, $dst . '/' . $file);
66
                }
67
            }
68
        }
69
        closedir($dir);
70
    }
71
72
    /**
73
     *
74
     * Verifies XOOPS version meets minimum requirements for this module
75
     * @static
76
     * @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...
77
     * @param null|string        $requiredVer
78
     *
79
     * @return bool true if meets requirements, false if not
80
     */
81
    public static function checkVerXoops(XoopsModule $module = null, $requiredVer = null)
82
    {
83
        $moduleDirName = basename(dirname(__DIR__));
84
        if (null === $module) {
85
            $module        = XoopsModule::getByDirname($moduleDirName);
86
        }
87
        xoops_loadLanguage('admin', $moduleDirName);
88
        //check for minimum XOOPS version
89
        $currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string
90
        $currArray  = explode('.', $currentVer);
91
        if (null === $requiredVer) {
92
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
93
        }
94
        $reqArray = explode('.', $requiredVer);
95
        $success  = true;
96
        foreach ($reqArray as $k => $v) {
97
            if (isset($currArray[$k])) {
98
                if ($currArray[$k] > $v) {
99
                    break;
100
                } elseif ($currArray[$k] == $v) {
101
                    continue;
102
                } else {
103
                    $success = false;
104
                    break;
105
                }
106
            } else {
107
                if ((int)$v > 0) { // handles things like x.x.x.0_RC2
108
                    $success = false;
109
                    break;
110
                }
111
            }
112
        }
113
114
        if (!$success) {
115
            $module->setErrors(sprintf(_AM_TAG_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
116
        }
117
118
        return $success;
119
    }
120
121
    /**
122
     *
123
     * Verifies PHP version meets minimum requirements for this module
124
     * @static
125
     * @param XoopsModule $module
126
     *
127
     * @return bool true if meets requirements, false if not
128
     */
129
    public static function checkVerPhp(XoopsModule $module)
130
    {
131
        xoops_loadLanguage('admin', $module->dirname());
132
        // check for minimum PHP version
133
        $success = true;
134
        $verNum  = PHP_VERSION;
135
        $reqVer  = $module->getInfo('min_php');
136
        if (false !== $reqVer && '' !== $reqVer) {
137
            if (version_compare($verNum, $reqVer, '<')) {
138
                $module->setErrors(sprintf(_AM_TAG_ERROR_BAD_PHP, $reqVer, $verNum));
139
                $success = false;
140
            }
141
        }
142
143
        return $success;
144
    }
145
146
    /**
147
     * @param $option
148
     * @return bool|mixed
149
     */
150 View Code Duplication
    public static function getModuleOption($option)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
151
    {
152
        global $xoopsModuleConfig, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
153
        static $tbloptions = array();
154
        if (is_array($tbloptions) && array_key_exists($option, $tbloptions)) {
155
            return $tbloptions[$option];
156
        }
157
158
        $retval = false;
159
        if (isset($xoopsModuleConfig)
160
            && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') === 'extgallery'
161
                && $xoopsModule->getVar('isactive'))
162
        ) {
163
            if (isset($xoopsModuleConfig[$option])) {
164
                $retval = $xoopsModuleConfig[$option];
165
            }
166
        } else {
167
            /** @var XoopsModuleHandler $moduleHandler */
168
            $moduleHandler = xoops_getHandler('module');
169
            $module        = $moduleHandler->getByDirname('extgallery');
170
171
            /** @var XoopsModuleHandler $moduleHandler */
172
            $configHandler = xoops_getHandler('config');
173
            if ($module) {
174
                $configurator = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
175
                if (isset($configurator[$option])) {
176
                    $retval = $configurator[$option];
177
                }
178
            }
179
        }
180
        $tbloptions[$option] = $retval;
181
182
        return $retval;
183
    }
184
185
    /**
186
     * @param $caption
187
     * @param $name
188
     * @param $value
189
     * @param $rows
190
     * @param $cols
191
     * @param $width
192
     * @param $height
193
     * @param $supplemental
194
     *
195
     * @return bool|XoopsFormEditor
196
     */
197 View Code Duplication
    public static function getWysiwygForm($caption, $name, $value, $rows, $cols, $width, $height, $supplemental)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
198
    {
199
        $editor_option            = strtolower(static::getModuleOption('form_options'));
200
        $editor                   = false;
0 ignored issues
show
Unused Code introduced by
$editor 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...
201
        $editor_configs           = array();
202
        $editor_configs['name']   = $name;
203
        $editor_configs['value']  = $value;
204
        $editor_configs['rows']   = $rows;
205
        $editor_configs['cols']   = $cols;
206
        $editor_configs['width']  = $width;
207
        $editor_configs['height'] = $height;
208
        $editor_configs['editor'] = $editor_option;
209
210
        $editor = new XoopsFormEditor($caption, $name, $editor_configs);
211
212
        return $editor;
213
    }
214
215
}
216