Completed
Push — develop ( 4f7877...1baf28 )
by Dmytro
05:50
created

setup.info.php ➔ parse_docblock()   C

Complexity

Conditions 17
Paths 5

Size

Total Lines 64
Code Lines 46

Duplication

Lines 20
Ratio 31.25 %

Importance

Changes 0
Metric Value
cc 17
eloc 46
nc 5
nop 2
dl 20
loc 64
rs 5.9734
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
//:: EVO Installer Setup file
3
//:::::::::::::::::::::::::::::::::::::::::
4
if (is_file($base_path . 'assets/cache/siteManager.php')) {
5
    include_once($base_path . 'assets/cache/siteManager.php');
6
}
7
if(!defined('MGR_DIR')) define('MGR_DIR', 'manager');
8
9
require_once('../'.MGR_DIR.'/includes/version.inc.php');
10
11
$chunkPath    = $base_path .'install/assets/chunks';
12
$snippetPath  = $base_path .'install/assets/snippets';
13
$pluginPath   = $base_path .'install/assets/plugins';
14
$modulePath   = $base_path .'install/assets/modules';
15
$templatePath = $base_path .'install/assets/templates';
16
$tvPath = $base_path .'install/assets/tvs';
17
18
// setup Template template files - array : name, description, type - 0:file or 1:content, parameters, category
19
$mt = &$moduleTemplates;
20 View Code Duplication
if(is_dir($templatePath) && is_readable($templatePath)) {
21
    $d = dir($templatePath);
22
    while (false !== ($tplfile = $d->read()))
23
    {
24
        if(substr($tplfile, -4) != '.tpl') continue;
25
        $params = parse_docblock($templatePath, $tplfile);
26
        if(is_array($params) && (count($params)>0))
27
        {
28
            $description = empty($params['version']) ? $params['description'] : "<strong>{$params['version']}</strong> {$params['description']}";
29
            $mt[] = array
30
            (
31
                $params['name'],
32
                $description,
33
                // Don't think this is gonna be used ... but adding it just in case 'type'
34
                $params['type'],
35
                "$templatePath/{$params['filename']}",
36
                $params['modx_category'],
37
                $params['lock_template'],
38
                array_key_exists('installset', $params) ? preg_split("/\s*,\s*/", $params['installset']) : false,
39
                isset($params['save_sql_id_as']) ? $params['save_sql_id_as'] : NULL // Nessecary to fix template-ID for demo-site
40
            );
41
        }
42
    }
43
    $d->close();
44
}
45
46
// setup Template Variable template files
47
$mtv = &$moduleTVs;
48 View Code Duplication
if(is_dir($tvPath) && is_readable($tvPath)) {
49
    $d = dir($tvPath);
50
    while (false !== ($tplfile = $d->read())) {
51
        if(substr($tplfile, -4) != '.tpl') continue;
52
        $params = parse_docblock($tvPath, $tplfile);
53
        if(is_array($params) && (count($params)>0)) {
54
            $description = empty($params['version']) ? $params['description'] : "<strong>{$params['version']}</strong> {$params['description']}";
55
            $mtv[] = array(
56
                $params['name'],
57
                $params['caption'],
58
                $description,
59
                $params['input_type'],
60
                $params['input_options'],
61
                $params['input_default'],
62
                $params['output_widget'],
63
                $params['output_widget_params'],
64
                "$templatePath/{$params['filename']}", /* not currently used */
65
                $params['template_assignments']!="*"?$params['template_assignments']:implode(",",array_map(create_function('$v','return $v[0];'),$mt)), /* comma-separated list of template names */
66
                $params['modx_category'],
67
                $params['lock_tv'],  /* value should be 1 or 0 */
68
                array_key_exists('installset', $params) ? preg_split("/\s*,\s*/", $params['installset']) : false
69
            );
70
        }
71
    }
72
    $d->close();
73
}
74
75
// setup chunks template files - array : name, description, type - 0:file or 1:content, file or content
76
$mc = &$moduleChunks;
77 View Code Duplication
if(is_dir($chunkPath) && is_readable($chunkPath)) {
78
    $d = dir($chunkPath);
79
    while (false !== ($tplfile = $d->read())) {
80
        if(substr($tplfile, -4) != '.tpl') {
81
            continue;
82
        }
83
        $params = parse_docblock($chunkPath, $tplfile);
84
        if(is_array($params) && count($params) > 0) {
85
            $mc[] = array(
86
                $params['name'],
87
                $params['description'],
88
                "$chunkPath/{$params['filename']}",
89
                $params['modx_category'],
90
                array_key_exists('overwrite', $params) ? $params['overwrite'] : 'true',
91
                array_key_exists('installset', $params) ? preg_split("/\s*,\s*/", $params['installset']) : false
92
            );
93
        }
94
    }
95
    $d->close();
96
}
97
98
// setup snippets template files - array : name, description, type - 0:file or 1:content, file or content,properties
99
$ms = &$moduleSnippets;
100 View Code Duplication
if(is_dir($snippetPath) && is_readable($snippetPath)) {
101
    $d = dir($snippetPath);
102
    while (false !== ($tplfile = $d->read())) {
103
        if(substr($tplfile, -4) != '.tpl') {
104
            continue;
105
        }
106
        $params = parse_docblock($snippetPath, $tplfile);
107
        if(is_array($params) && count($params) > 0) {
108
            $description = empty($params['version']) ? $params['description'] : "<strong>{$params['version']}</strong> {$params['description']}";
109
            $ms[] = array(
110
                $params['name'],
111
                $description,
112
                "$snippetPath/{$params['filename']}",
113
                $params['properties'],
114
                $params['modx_category'],
115
                array_key_exists('installset', $params) ? preg_split("/\s*,\s*/", $params['installset']) : false
116
            );
117
        }
118
    }
119
    $d->close();
120
}
121
122
// setup plugins template files - array : name, description, type - 0:file or 1:content, file or content,properties
123
$mp = &$modulePlugins;
124 View Code Duplication
if(is_dir($pluginPath) && is_readable($pluginPath)) {
125
    $d = dir($pluginPath);
126
    while (false !== ($tplfile = $d->read())) {
127
        if(substr($tplfile, -4) != '.tpl') {
128
            continue;
129
        }
130
        $params = parse_docblock($pluginPath, $tplfile);
131
        if(is_array($params) && count($params) > 0) {
132
            $description = empty($params['version']) ? $params['description'] : "<strong>{$params['version']}</strong> {$params['description']}";
133
            $mp[] = array(
134
                $params['name'],
135
                $description,
136
                "$pluginPath/{$params['filename']}",
137
                $params['properties'],
138
                $params['events'],
139
                $params['guid'],
140
                $params['modx_category'],
141
                $params['legacy_names'],
142
                array_key_exists('installset', $params) ? preg_split("/\s*,\s*/", $params['installset']) : false,
143
                (int)$params['disabled']
144
            );
145
        }
146
    }
147
    $d->close();
148
}
149
150
// setup modules - array : name, description, type - 0:file or 1:content, file or content,properties, guid,enable_sharedparams
151
$mm = &$moduleModules;
152
$mdp = &$moduleDependencies;
153 View Code Duplication
if(is_dir($modulePath) && is_readable($modulePath)) {
154
    $d = dir($modulePath);
155
    while (false !== ($tplfile = $d->read())) {
156
        if(substr($tplfile, -4) != '.tpl') {
157
            continue;
158
        }
159
        $params = parse_docblock($modulePath, $tplfile);
160
        if(is_array($params) && count($params) > 0) {
161
            $description = empty($params['version']) ? $params['description'] : "<strong>{$params['version']}</strong> {$params['description']}";
162
            $mm[] = array(
163
                $params['name'],
164
                $description,
165
                "$modulePath/{$params['filename']}",
166
                $params['properties'],
167
                $params['guid'],
168
                (int)$params['shareparams'],
169
                $params['modx_category'],
170
                array_key_exists('installset', $params) ? preg_split("/\s*,\s*/", $params['installset']) : false
171
            );
172
        }
173
		if ((int)$params['shareparams'] || !empty($params['dependencies'])) {
174
			$dependencies = explode(',', $params['dependencies']);
175
			foreach ($dependencies as $dependency) {
176
				$dependency = explode(':', $dependency);
177
				switch (trim($dependency[0])) {
178
					case 'template':
179
						$mdp[] = array(
180
							'module' => $params['name'],
181
							'table' => 'templates',
182
							'column' => 'templatename',
183
							'type' => 50,
184
							'name' => trim($dependency[1])
185
						);
186
						break;
187
					case 'tv':
188
					case 'tmplvar':
189
						$mdp[] = array(
190
							'module' => $params['name'],
191
							'table' => 'tmplvars',
192
							'column' => 'name',
193
							'type' => 60,
194
							'name' => trim($dependency[1])
195
						);
196
						break;
197
					case 'chunk':
198
					case 'htmlsnippet':
199
						$mdp[] = array(
200
							'module' => $params['name'],
201
							'table' => 'htmlsnippets',
202
							'column' => 'name',
203
							'type' => 10,
204
							'name' => trim($dependency[1])
205
						);
206
						break;
207
					case 'snippet':
208
						$mdp[] = array(
209
							'module' => $params['name'],
210
							'table' => 'snippets',
211
							'column' => 'name',
212
							'type' => 40,
213
							'name' => trim($dependency[1])
214
						);
215
						break;
216
					case 'plugin':
217
						$mdp[] = array(
218
							'module' => $params['name'],
219
							'table' => 'plugins',
220
							'column' => 'name',
221
							'type' => 30,
222
							'name' => trim($dependency[1])
223
						);
224
						break;
225
					case 'resource':
226
						$mdp[] = array(
227
							'module' => $params['name'],
228
							'table' => 'content',
229
							'column' => 'pagetitle',
230
							'type' => 20,
231
							'name' => trim($dependency[1])
232
						);
233
						break;
234
				}
235
			}
236
		}
237
    }
238
    $d->close();
239
}
240
241
// setup callback function
242
$callBackFnc = "clean_up";
243
244 View Code Duplication
function clean_up($sqlParser) {
0 ignored issues
show
Best Practice introduced by
The function clean_up() has been defined more than once; this definition is ignored, only the first definition in install/cli-install.php (L781-821) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function 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...
245
    $ids = array();
246
247
    // secure web documents - privateweb
248
    mysqli_query($sqlParser->conn,"UPDATE `".$sqlParser->prefix."site_content` SET privateweb = 0 WHERE privateweb = 1");
249
    $sql =  "SELECT DISTINCT sc.id
250
             FROM `".$sqlParser->prefix."site_content` sc
251
             LEFT JOIN `".$sqlParser->prefix."document_groups` dg ON dg.document = sc.id
252
             LEFT JOIN `".$sqlParser->prefix."webgroup_access` wga ON wga.documentgroup = dg.document_group
253
             WHERE wga.id>0";
254
    $ds = mysqli_query($sqlParser->conn,$sql);
255
    if(!$ds) {
256
        echo "An error occurred while executing a query: ".mysqli_error($sqlParser->conn);
257
    }
258
    else {
259
        while($r = mysqli_fetch_assoc($ds)) $ids[]=$r["id"];
260
        if(count($ids)>0) {
261
            mysqli_query($sqlParser->conn,"UPDATE `".$sqlParser->prefix."site_content` SET privateweb = 1 WHERE id IN (".implode(", ",$ids).")");
262
            unset($ids);
263
        }
264
    }
265
266
    // secure manager documents privatemgr
267
    mysqli_query($sqlParser->conn,"UPDATE `".$sqlParser->prefix."site_content` SET privatemgr = 0 WHERE privatemgr = 1");
268
    $sql =  "SELECT DISTINCT sc.id
269
             FROM `".$sqlParser->prefix."site_content` sc
270
             LEFT JOIN `".$sqlParser->prefix."document_groups` dg ON dg.document = sc.id
271
             LEFT JOIN `".$sqlParser->prefix."membergroup_access` mga ON mga.documentgroup = dg.document_group
272
             WHERE mga.id>0";
273
    $ds = mysqli_query($sqlParser->conn,$sql);
274
    if(!$ds) {
275
        echo "An error occurred while executing a query: ".mysqli_error($sqlParser->conn);
276
    }
277
    else {
278
        while($r = mysqli_fetch_assoc($ds)) $ids[]=$r["id"];
279
        if(count($ids)>0) {
280
            mysqli_query($sqlParser->conn,"UPDATE `".$sqlParser->prefix."site_content` SET privatemgr = 1 WHERE id IN (".implode(", ",$ids).")");
281
            unset($ids);
282
        }
283
    }
284
}
285
286 View Code Duplication
function parse_docblock($element_dir, $filename) {
0 ignored issues
show
Best Practice introduced by
The function parse_docblock() has been defined more than once; this definition is ignored, only the first definition in install/cli-install.php (L823-886) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function 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...
287
    $params = array();
288
    $fullpath = $element_dir . '/' . $filename;
289
    if(is_readable($fullpath)) {
290
        $tpl = @fopen($fullpath, "r");
291
        if($tpl) {
292
            $params['filename'] = $filename;
293
            $docblock_start_found = false;
294
            $name_found = false;
295
            $description_found = false;
296
297
            while(!feof($tpl)) {
298
                $line = fgets($tpl);
299
                if(!$docblock_start_found) {
300
                    // find docblock start
301
                    if(strpos($line, '/**') !== false) {
302
                        $docblock_start_found = true;
303
                    }
304
                    continue;
305
                } elseif(!$name_found) {
306
                    // find name
307
                    $ma = null;
308
                    if(preg_match("/^\s+\*\s+(.+)/", $line, $ma)) {
309
                        $params['name'] = trim($ma[1]);
310
                        $name_found = !empty($params['name']);
311
                    }
312
                    continue;
313
                } elseif(!$description_found) {
314
                    // find description
315
                    $ma = null;
316
                    if(preg_match("/^\s+\*\s+(.+)/", $line, $ma)) {
317
                        $params['description'] = trim($ma[1]);
318
                        $description_found = !empty($params['description']);
319
                    }
320
                    continue;
321
                } else {
322
                    $ma = null;
323
                    if(preg_match("/^\s+\*\s+\@([^\s]+)\s+(.+)/", $line, $ma)) {
324
                        $param = trim($ma[1]);
325
                        $val = trim($ma[2]);
326
                        if(!empty($param) && !empty($val)) {
327
                            if($param == 'internal') {
328
                                $ma = null;
329
                                if(preg_match("/\@([^\s]+)\s+(.+)/", $val, $ma)) {
330
                                    $param = trim($ma[1]);
331
                                    $val = trim($ma[2]);
332
                                }
333
                                //if($val !== '0' && (empty($param) || empty($val))) {
334
                                if(empty($param)) {
335
                                    continue;
336
                                }
337
                            }
338
                            $params[$param] = $val;
339
                        }
340
                    } elseif(preg_match("/^\s*\*\/\s*$/", $line)) {
341
                        break;
342
                    }
343
                }
344
            }
345
            @fclose($tpl);
346
        }
347
    }
348
    return $params;
349
}
350