Completed
Branch master (48ec27)
by Michael
03:44
created

ExtgalleryUtilities::copyFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 14
rs 9.4285
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
include_once __DIR__ . '/../include/config.php';
4
5
/**
6
 * Class MyalbumUtilities
7
 */
8
class ExtgalleryUtilities 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
        }
28
        catch (Exception $e) {
29
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br/>';
30
        }
31
    }
32
33
    /**
34
     * @param $file
35
     * @param $folder
36
     * @return bool
37
     */
38
    public static function copyFile($file, $folder)
39
    {
40
        return copy($file, $folder);
41
        //        try {
42
        //            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...
43
        //                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...
44
        //            } 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...
45
        //                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...
46
        //            }
47
        //        } 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...
48
        //            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...
49
        //        }
50
        //        return false;
51
    }
52
53
    /**
54
     * @param $src
55
     * @param $dst
56
     */
57
    public static function recurseCopy($src, $dst)
58
    {
59
        $dir = opendir($src);
60
        //    @mkdir($dst);
61
        while (false !== ($file = readdir($dir))) {
62
            if (($file !== '.') && ($file !== '..')) {
63
                if (is_dir($src . '/' . $file)) {
64
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
65
                } else {
66
                    copy($src . '/' . $file, $dst . '/' . $file);
67
                }
68
            }
69
        }
70
        closedir($dir);
71
    }
72
73
    /**
74
     *
75
     * Verifies XOOPS version meets minimum requirements for this module
76
     * @static
77
     * @param XoopsModule $module
78
     *
79
     * @return bool true if meets requirements, false if not
80
     */
81
    public static function checkXoopsVer(XoopsModule $module)
82
    {
83
        xoops_loadLanguage('admin', $module->dirname());
84
        //check for minimum XOOPS version
85
        $currentVer  = substr(XOOPS_VERSION, 6); // get the numeric part of string
86
        $currArray   = explode('.', $currentVer);
87
        $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
88
        $reqArray    = explode('.', $requiredVer);
89
        $success     = true;
90
        foreach ($reqArray as $k => $v) {
91
            if (isset($currArray[$k])) {
92
                if ($currArray[$k] > $v) {
93
                    break;
94
                } elseif ($currArray[$k] == $v) {
95
                    continue;
96
                } else {
97
                    $success = false;
98
                    break;
99
                }
100
            } else {
101
                if ((int)$v > 0) { // handles things like x.x.x.0_RC2
102
                    $success = false;
103
                    break;
104
                }
105
            }
106
        }
107
108
        if (!$success) {
109
            $module->setErrors(sprintf(_AM_TAG_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
110
        }
111
112
        return $success;
113
    }
114
115
    /**
116
     *
117
     * Verifies PHP version meets minimum requirements for this module
118
     * @static
119
     * @param XoopsModule $module
120
     *
121
     * @return bool true if meets requirements, false if not
122
     */
123
    public static function checkPhpVer(XoopsModule $module)
124
    {
125
        xoops_loadLanguage('admin', $module->dirname());
126
        // check for minimum PHP version
127
        $success = true;
128
        $verNum  = phpversion();
129
        $reqVer  =& $module->getInfo('min_php');
130
        if (false !== $reqVer && '' !== $reqVer) {
131
            if (version_compare($verNum, $reqVer, '<')) {
132
                $module->setErrors(sprintf(_AM_TAG_ERROR_BAD_PHP, $reqVer, $verNum));
133
                $success = false;
134
            }
135
        }
136
137
        return $success;
138
    }
139
140 View Code Duplication
    public static function getModuleOption($option)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
141
    {
142
        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...
143
        static $tbloptions = array();
144
        if (is_array($tbloptions) && array_key_exists($option, $tbloptions)) {
145
            return $tbloptions[$option];
146
        }
147
148
        $retval = false;
149
        if (isset($xoopsModuleConfig)
150
            && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') === 'extgallery'
151
                && $xoopsModule->getVar('isactive'))
152
        ) {
153
            if (isset($xoopsModuleConfig[$option])) {
154
                $retval = $xoopsModuleConfig[$option];
155
            }
156
        } else {
157
            /** @var XoopsModuleHandler $moduleHandler */
158
            $moduleHandler = xoops_getHandler('module');
159
            $module        = $moduleHandler->getByDirname('extgallery');
160
161
            /** @var XoopsConfigHandler $configHandler */
162
            $configHandler = xoops_getHandler('config');
163
            if ($module) {
164
                $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
165
                if (isset($moduleConfig[$option])) {
166
                    $retval = $moduleConfig[$option];
167
                }
168
            }
169
        }
170
        $tbloptions[$option] = $retval;
171
172
        return $retval;
173
    }
174
175
    /**
176
     * @param $caption
177
     * @param $name
178
     * @param $value
179
     * @param $rows
180
     * @param $cols
181
     * @param $width
182
     * @param $height
183
     * @param $supplemental
184
     *
185
     * @return bool|XoopsFormEditor
186
     */
187 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...
188
    {
189
        $editor_option            = strtolower(static::getModuleOption('form_options'));
190
        $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...
191
        $editor_configs           = array();
192
        $editor_configs['name']   = $name;
193
        $editor_configs['value']  = $value;
194
        $editor_configs['rows']   = $rows;
195
        $editor_configs['cols']   = $cols;
196
        $editor_configs['width']  = $width;
197
        $editor_configs['height'] = $height;
198
        $editor_configs['editor'] = $editor_option;
199
200
        $editor = new XoopsFormEditor($caption, $name, $editor_configs);
201
202
        return $editor;
203
    }
204
}
205