Passed
Push — master ( df23ea...e5cb4f )
by Goffy
03:38
created

function_qualifier()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 254
Code Lines 245

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 245
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 254
rs 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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
/**
14
 * modulebuilder module.
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
18
 *
19
 * @since           2.5.5
20
 *
21
 * @author          Txmod Xoops <[email protected]>
22
 *
23
 */
24
25
// Define main template
26
$templateMain = 'modulebuilder_index.tpl';
27
28
include __DIR__ . '/header.php';
29
// Recovered value of argument op in the URL $
30
$op    = \Xmf\Request::getString('op', 'list');
31
32
$src_path = TDMC_PATH ;
33
$dst_path = TDMC_UPLOAD_REPOSITORY_PATH . '/codecleaned';
34
35
$patKeys   = [];
36
$patValues = [];
37
cloneFileFolder($src_path, $dst_path, $patKeys, $patValues);
38
39
40
include __DIR__ . '/footer.php';
41
42
43
44
function function_qualifier($src_path, $dst_path) {
45
46
    $patterns = [
47
        //remove backslash if alreadyin order to avoid \\
48
        '\array_diff('             => 'array_diff(',
49
        '\array_filter('           => 'array_filter(',
50
        '\array_key_exists('       => 'array_key_exists(',
51
        '\array_keys('             => 'array_keys(',
52
        '\array_search('           => 'array_search(',
53
        '\array_slice('            => 'array_slice(',
54
        '\array_unshift('          => 'array_unshift(',
55
        '\array_values('           => 'array_values(',
56
        '\assert('                 => 'assert(',
57
        '\basename('               => 'basename(',
58
        '\boolval('                => 'boolval(',
59
        '\call_user_func('         => 'call_user_func(',
60
        '\call_user_func_array('   => 'call_user_func_array(',
61
        '\chr('                    => 'chr(',
62
        '\class_exists('           => 'class_exists(',
63
        '\closedir('               => 'closedir(',
64
        '\constant('               => 'constant(',
65
        '\copy('                   => 'copy(',
66
        '\count('                  => 'count(',
67
        '\curl_close('             => 'curl_close(',
68
        '\curl_error('             => 'curl_error(',
69
        '\curl_exec('              => 'curl_exec(',
70
        '\curl_file_create('       => 'curl_file_create(',
71
        '\curl_getinfo('           => 'curl_getinfo(',
72
        '\curl_init('              => 'curl_init(',
73
        '\curl_setopt('            => 'curl_setopt(',
74
        '\define('                 => 'define(',
75
        '\defined('                => 'defined(',
76
        '\dirname('                => 'dirname(',
77
        '\doubleval('              => 'doubleval(',
78
        '\explode('                => 'explode(',
79
        '\extension_loaded('       => 'extension_loaded(',
80
        '\file_exists('            => 'file_exists(',
81
        '\finfo_open('             => 'finfo_open(',
82
        '\floatval('               => 'floatval(',
83
        '\floor('                  => 'floor(',
84
        '\formatTimestamp('        => 'formatTimestamp(',
85
        '\func_get_args('          => 'func_get_args(',
86
        '\func_num_args('          => 'func_num_args(',
87
        '\function_exists('        => 'function_exists(',
88
        '\get_called_class('       => 'get_called_class(',
89
        '\get_class('              => 'get_class(',
90
        '\getimagesize('           => 'getimagesize(',
91
        '\gettype('                => 'gettype(',
92
        '\imagecopyresampled('     => 'imagecopyresampled(',
93
        '\imagecreatefromgif('     => 'imagecreatefromgif(',
94
        '\imagecreatefromjpeg('    => 'imagecreatefromjpeg(',
95
        '\imagecreatefrompng('     => 'imagecreatefrompng(',
96
        '\imagecreatefromstring('  => 'imagecreatefromstring(',
97
        '\imagecreatetruecolor('   => 'imagecreatetruecolor(',
98
        '\imagedestroy('           => 'imagedestroy(',
99
        '\imagegif('               => 'imagegif(',
100
        '\imagejpeg('              => 'imagejpeg(',
101
        '\imagepng('               => 'imagepng(',
102
        '\imagerotate('            => 'imagerotate(',
103
        '\imagesx('                => 'imagesx(',
104
        '\imagesy('                => 'imagesy(',
105
        '\implode('                => 'implode(',
106
        '\in_array('               => 'in_array(',
107
        '\ini_get('                => 'ini_get(',
108
        '\intval('                 => 'intval(',
109
        '\is_array('               => 'is_array(',
110
        '\is_bool('                => 'is_bool(',
111
        '\is_callable('            => 'is_callable(',
112
        '\is_dir('                 => 'is_dir(',
113
        '\is_double('              => 'is_double(',
114
        '\is_float('               => 'is_float(',
115
        '\is_int('                 => 'is_int(',
116
        '\is_integer('             => 'is_integer(',
117
        '\is_link('                => 'is_link(',
118
        '\is_long('                => 'is_long(',
119
        '\is_null('                => 'is_null(',
120
        '\is_object('              => 'is_object(',
121
        '\is_real('                => 'is_real(',
122
        '\is_resource('            => 'is_resource(',
123
        '\is_string('              => 'is_string(',
124
        '\json_decode('            => 'json_decode(',
125
        '\json_encode('            => 'json_encode(',
126
        '\mime_content_type('      => 'mime_content_type(',
127
        '\mkdir('                  => 'mkdir(',
128
        '\opendir('                => 'opendir(',
129
        '\ord('                    => 'ord(',
130
        '\pathinfo('               => 'pathinfo(',
131
        '\preg_match('             => 'preg_match(',
132
        '\preg_match_all('         => 'preg_match_all(',
133
        '\preg_replace('           => 'preg_replace(',
134
        '\readdir('                => 'readdir(',
135
        '\readlink('               => 'readlink(',
136
        '\redirect_header('        => 'redirect_header(',
137
        '\rename('                 => 'rename(',
138
        '\rmdir('                  => 'rmdir(',
139
        '\round('                  => 'round(',
140
        '\scandir('                => 'scandir(',
141
        '\sprintf('                => 'sprintf(',
142
        '\str_replace('            => 'str_replace(',
143
        '\strip_tags('             => 'strip_tags(',
144
        '\strlen('                 => 'strlen(',
145
        '\strpos('                 => 'strpos(',
146
        '\strtotime('              => 'strtotime(',
147
        '\strval('                 => 'strval(',
148
        '\substr('                 => 'substr(',
149
        '\symlink('                => 'symlink(',
150
        '\time()'                  => 'time()',
151
        '\trigger_error('          => 'trigger_error(',
152
        '\trim('                   => 'trim(',
153
        '\ucfirst('                => 'ucfirst(',
154
        '\unlink('                 => 'unlink(',
155
        '\version_compare('        => 'version_compare(',
156
        '\xoops_getHandler('       => 'xoops_getHandler(',
157
        '\xoops_load('             => 'xoops_load(',
158
        '\xoops_loadLanguage('     => 'xoops_loadLanguage(',
159
        '\mb_strtoupper('          => 'mb_strtoupper(',
160
        '\mb_strtolower('          => 'mb_strtolower(',
161
        '\mb_strpos('              => 'mb_strpos(',
162
        '\mb_strlen('              => 'mb_strlen(',
163
        '\mb_strrpos('             => 'mb_strrpos(',
164
165
        //add backslash to all functions
166
        'array_diff('              => '\array_diff(',
167
        'array_filter('            => '\array_filter(',
168
        'array_key_exists('        => '\array_key_exists(',
169
        'array_keys('              => '\array_keys(',
170
        'array_search('            => '\array_search(',
171
        'array_slice('             => '\array_slice(',
172
        'array_unshift('           => '\array_unshift(',
173
        'array_values('            => '\array_values(',
174
        'assert('                  => '\assert(',
175
        'basename('                => '\basename(',
176
        'boolval('                 => '\boolval(',
177
        'call_user_func('          => '\call_user_func(',
178
        'call_user_func_array('    => '\call_user_func_array(',
179
        'chr('                     => '\chr(',
180
        'class_exists('            => '\class_exists(',
181
        'closedir('                => '\closedir(',
182
        'constant('                => '\constant(',
183
        'copy('                    => '\copy(',
184
        'count('                   => '\count(',
185
        'curl_close('              => '\curl_close(',
186
        'curl_error('              => '\curl_error(',
187
        'curl_exec('               => '\curl_exec(',
188
        'curl_file_create('        => '\curl_file_create(',
189
        'curl_getinfo('            => '\curl_getinfo(',
190
        'curl_init('               => '\curl_init(',
191
        'curl_setopt('             => '\curl_setopt(',
192
        'define('                  => '\define(',
193
        'defined('                 => '\defined(',
194
        'dirname('                 => '\dirname(',
195
        'doubleval('               => '\doubleval(',
196
        'explode('                 => '\explode(',
197
        'extension_loaded('        => '\extension_loaded(',
198
        'file_exists('             => '\file_exists(',
199
        'finfo_open('              => '\finfo_open(',
200
        'floatval('                => '\floatval(',
201
        'floor('                   => '\floor(',
202
        'formatTimestamp('         => '\formatTimestamp(',
203
        'func_get_args('           => '\func_get_args(',
204
        'func_num_args('           => '\func_num_args(',
205
        'function_exists('         => '\function_exists(',
206
        'get_called_class('        => '\get_called_class(',
207
        'get_class('               => '\get_class(',
208
        'getimagesize('            => '\getimagesize(',
209
        'gettype('                 => '\gettype(',
210
        'imagecopyresampled('      => '\imagecopyresampled(',
211
        'imagecreatefromgif('      => '\imagecreatefromgif(',
212
        'imagecreatefromjpeg('     => '\imagecreatefromjpeg(',
213
        'imagecreatefrompng('      => '\imagecreatefrompng(',
214
        'imagecreatefromstring('   => '\imagecreatefromstring(',
215
        'imagecreatetruecolor('    => '\imagecreatetruecolor(',
216
        'imagedestroy('            => '\imagedestroy(',
217
        'imagegif('                => '\imagegif(',
218
        'imagejpeg('               => '\imagejpeg(',
219
        'imagepng('                => '\imagepng(',
220
        'imagerotate('             => '\imagerotate(',
221
        'imagesx('                 => '\imagesx(',
222
        'imagesy('                 => '\imagesy(',
223
        'implode('                 => '\implode(',
224
        'in_array('                => '\in_array(',
225
        'ini_get('                 => '\ini_get(',
226
        'intval('                  => '\intval(',
227
        'is_array('                => '\is_array(',
228
        'is_bool('                 => '\is_bool(',
229
        'is_callable('             => '\is_callable(',
230
        'is_dir('                  => '\is_dir(',
231
        'is_double('               => '\is_double(',
232
        'is_float('                => '\is_float(',
233
        'is_int('                  => '\is_int(',
234
        'is_integer('              => '\is_integer(',
235
        'is_link('                 => '\is_link(',
236
        'is_long('                 => '\is_long(',
237
        'is_null('                 => '\is_null(',
238
        'is_object('               => '\is_object(',
239
        'is_real('                 => '\is_real(',
240
        'is_resource('             => '\is_resource(',
241
        'is_string('               => '\is_string(',
242
        'json_decode('             => '\json_decode(',
243
        'json_encode('             => '\json_encode(',
244
        'mb_strtoupper('           => '\mb_strtoupper(',
245
        'mb_strtolower('           => '\mb_strtolower(',
246
        'mb_strpos('               => '\mb_strpos(',
247
        'mb_strlen('               => '\mb_strlen(',
248
        'mb_strrpos('              => '\mb_strrpos(',
249
        'mime_content_type('       => '\mime_content_type(',
250
        'mkdir('                   => '\mkdir(',
251
        'opendir('                 => '\opendir(',
252
        'ord('                     => '\ord(',
253
        'pathinfo('                => '\pathinfo(',
254
        'preg_match('              => '\preg_match(',
255
        'preg_match_all('          => '\preg_match_all(',
256
        'preg_replace('            => '\preg_replace(',
257
        'readdir('                 => '\readdir(',
258
        'readlink('                => '\readlink(',
259
        'redirect_header('         => '\redirect_header(',
260
        'rename('                  => '\rename(',
261
        'rmdir('                   => '\rmdir(',
262
        'round('                   => '\round(',
263
        'scandir('                 => '\scandir(',
264
        'sprintf('                 => '\sprintf(',
265
        'str_replace('             => '\str_replace(',
266
        'strip_tags('              => '\strip_tags(',
267
        'strlen('                  => '\strlen(',
268
        'strpos('                  => '\strpos(',
269
        'strtotime('               => '\strtotime(',
270
        'strval('                  => '\strval(',
271
        'substr('                  => '\substr(',
272
        'symlink('                 => '\symlink(',
273
        'time()'                   => '\time()',
274
        'trigger_error('           => '\trigger_error(',
275
        'trim('                    => '\trim(',
276
        'ucfirst('                 => '\ucfirst(',
277
        'unlink('                  => '\unlink(',
278
        'version_compare('         => '\version_compare(',
279
        'xoops_getHandler('        => '\xoops_getHandler(',
280
        'xoops_load('              => '\xoops_load(',
281
        'xoops_loadLanguage('      => '\xoops_loadLanguage(',
282
283
        //correct errors
284
        'mb_\strlen('              => 'mb_strlen(',
285
        'mb_\substr('              => 'mb_substr(',
286
        'x\copy'                   => 'xcopy',
287
        'r\rmdir'                  => 'rrmdir',
288
        'r\copy'                   => 'rcopy',
289
        '\dirname()'               => 'dirname()',
290
        'assw\ord'                 => 'assword',
291
        'mb_\strpos'               => 'mb_strpos',
292
        'imagecopy('              => 'imagecopy(',
293
    ];
294
295
    $patKeys   = \array_keys($patterns);
296
    $patValues = \array_values($patterns);
297
    cloneFileFolder($src_path, $dst_path, $patKeys, $patValues);
298
299
}
300
301
// recursive cloning script
302
/**
303
 * @param $src_path
304
 * @param $dst_path
305
 * @param array $patKeys
306
 * @param array $patValues
307
 */
308
function cloneFileFolder($src_path, $dst_path, $patKeys = [], $patValues =[])
309
{
310
    // open the source directory
311
    $dir = \opendir($src_path);
312
    // Make the destination directory if not exist
313
    @\mkdir($dst_path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

313
    /** @scrutinizer ignore-unhandled */ @\mkdir($dst_path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
314
    // Loop through the files in source directory
315
    while( $file = \readdir($dir) ) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

315
    while( $file = \readdir(/** @scrutinizer ignore-type */ $dir) ) {
Loading history...
316
        if (( $file != '.' ) && ( $file != '..' )) {
317
            if ( \is_dir($src_path . '/' . $file) ) {
318
                // Recursively calling custom copy function for sub directory
319
                cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues);
320
            } else {
321
                cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues);
322
            }
323
        }
324
    }
325
    \closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

325
    \closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
326
}
327
328
/**
329
 * @param $src_file
330
 * @param $dst_file
331
 * @param array $patKeys
332
 * @param array $patValues
333
 */
334
function cloneFile($src_file, $dst_file, $patKeys = [], $patValues =[])
335
{
336
    $replace_code = false;
337
    $changeExtensions = ['php'];
338
    if (in_array(mb_strtolower(\pathinfo($src_file, PATHINFO_EXTENSION)), $changeExtensions)) {
339
        $replace_code = true;
340
    }
341
    if (\strpos( $dst_file, basename(__FILE__)) > 0) {
342
        //skip myself
343
        $replace_code = false;
344
    }
345
    if ($replace_code) {
346
        // file, read it and replace text
347
        $content = file_get_contents($src_file);
348
        $content = \str_replace($patKeys, $patValues, $content);
349
        //check file name whether it contains replace code
350
        $path_parts = \pathinfo($dst_file);
351
        $path = $path_parts['dirname'];
352
        $file =  $path_parts['basename'];
353
        $dst_file = $path . '/' . \str_replace($patKeys, $patValues, $file);
354
        file_put_contents($dst_file, $content);
355
    } else {
356
        \copy($src_file, $dst_file);
357
    }
358
}
359
360