Passed
Push — master ( d9e5dd...36764d )
by Spuds
01:07 queued 26s
created

getFilesChanged()   D

Complexity

Conditions 27
Paths 42

Size

Total Lines 126
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 27
eloc 57
c 4
b 1
f 1
nc 42
nop 2
dl 0
loc 126
rs 4.1666

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
3
/**
4
 * @name      ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
7
 *
8
 * @version 1.1
9
 *
10
 */
11
12
$output_file_name = 'detailed-version.js';
13
if (empty($argv[1]) && empty($_GET['b']))
14
{
15
	echo "Please specify a branch to compare against master, (for example: b=patch_1-1-9)\n";
16
	die();
17
}
18
else
19
{
20
	$new_release = $argv[1] ?? $_GET['b'];
21
}
22
23
if (empty($argv[2]) && empty($_GET['v']))
24
{
25
	echo "Please specify a version to check (for example v=1.1.9)\n";
26
	die();
27
}
28
else
29
{
30
	$new_version = $argv[2] ?? $_GET['v'];
31
}
32
33
// Some constants and $settings needed to let getFileVersions do it's magic
34
DEFINE('ELK', '1');
35
DEFINE('BOARDDIR', __DIR__);
36
DEFINE('LANGUAGEDIR', BOARDDIR . '/themes/default/languages');
37
DEFINE('SOURCEDIR', BOARDDIR . '/sources');
38
DEFINE('ADMINDIR', SOURCEDIR . '/admin');
39
DEFINE('EXTDIR', SOURCEDIR . '/ext');
40
DEFINE('CONTROLLERDIR', SOURCEDIR . '/controllers');
41
DEFINE('SUBSDIR', SOURCEDIR . '/subs');
42
43
global $settings;
44
$settings['default_theme_dir'] = BOARDDIR . '/themes/default';
45
$settings['theme_dir'] = BOARDDIR . '/themes/default';
46
$settings['theme_id'] = 1;
47
48
// Call the function that'll get all the version info we need.
49
require_once(SUBSDIR . '/Admin.subs.php');
50
$versionOptions = array(
51
	'include_ssi' => true,
52
	'include_subscriptions' => true,
53
	'sort_results' => true,
54
);
55
56
// Use our function to read all file headers and get the stated version
57
$version_info = getFileVersions($versionOptions);
58
59
// Use git to get our list of file changed by commit
60
echo 'Getting changed files from branch ' . $new_release . ' compared to master branch<br>';
61
$changed_files_list = getFilesChanged('master', $new_release);
62
$update_files = array();
63
64
// Now we need to grab the current version of the forum from index.php
65
$index = file_get_contents(BOARDDIR . '/bootstrap.php');
66
$index_lines = explode("\n", $index);
67
foreach ($index_lines as $line)
68
{
69
	if (strpos($line, 'define(\'FORUM_VERSION') !== false)
70
	{
71
		preg_match('~\'ElkArte (.*)\'\);$~', $line, $matches);
72
		$forum_version = $matches[1];
73
		break;
74
	}
75
}
76
77
echo 'Checking Changed File headers match expected ' . $forum_version . '<br>';
78
79
$handle = fopen($output_file_name, 'wb');
80
81
// Start with the common thing
82
fwrite($handle, 'window.ourVersions = {');
83
fwrite($handle, "\n\t'Version': '{$forum_version}',\n");
84
85
foreach (array('admin', 'controllers', 'database', 'subs') as $type)
86
{
87
	foreach ($version_info['file_versions_' . $type] as $file => $ver)
88
	{
89
		if ($new_version === $ver)
90
		{
91
			$update_files[] = str_replace('subssubs', 'subs', $type . $file);
92
		}
93
		fwrite($handle, "\t'{$type}{$file}': '{$ver}',\n");
94
	}
95
}
96
97
foreach ($version_info['file_versions_modules'] as $file => $ver)
98
{
99
	if ($new_version === $ver)
100
	{
101
		$update_files[] = 'sources' . $file;
102
	}
103
	fwrite($handle, "\t'{$type}{$file}': '{$ver}',\n");
104
}
105
106
foreach ($version_info['file_versions'] as $file => $ver)
107
{
108
	if ($new_version === $ver)
109
	{
110
		$update_files[] = 'sources' . $file;
111
	}
112
	fwrite($handle, "\t'sources{$file}': '{$ver}',\n");
113
}
114
115
foreach ($version_info['default_template_versions'] as $file => $ver)
116
{
117
	if ($new_version === $ver)
118
	{
119
		$update_files[] = 'default' . $file;
120
	}
121
	fwrite($handle, "\t'default{$file}': '{$ver}',\n");
122
}
123
124
// Let's close the "core" files and start the language files
125
fwrite($handle, '};');
126
fwrite($handle, "\n\nourLanguageVersions = {\n");
127
128
foreach ($version_info['default_language_versions'] as $lang => $files)
129
{
130
	if ($lang === 'english')
131
	{
132
		foreach ($files as $file => $ver)
133
		{
134
			fwrite($handle, "\t'{$file}': '{$ver}',\n");
135
		}
136
		break;
137
	}
138
}
139
140
// And that's all folks!
141
fwrite($handle, '};');
142
fclose($handle);
143
if (count(array_diff($update_files, $changed_files_list)) !== 0)
144
{
145
	if (empty($_GET))
146
	{
147
		echo "Something is wrong: at least one of the files updated is not in the list of those changed since the lastest version.\nThis is a list of the files affected by the problem:\n";
148
		print_r(array_diff($update_files, $changed_files_list));
149
	}
150
	else
151
	{
152
		echo "Something is wrong:<br>At least one of the files updated is not in the list of those changed since the lastest version.<br>This is a list of the files affected by the problem:<br>";
153
		echo implode('<br>', array_diff($update_files, $changed_files_list));
154
	}
155
}
156
157
if (count(array_diff($changed_files_list, $update_files)) !== 0)
158
{
159
	if (empty($_GET))
160
	{
161
		echo "Something is wrong: at least one of the files changed since the last released version has not been updated in the repository.\nThis is a list of the files affected by the problem:\n";
162
		print_r(array_diff($changed_files_list, $update_files));
163
	}
164
	else
165
	{
166
		echo "Something is wrong:<br>At least one of the files changed since the last released version has not been updated in the repository.<br>This is a list of the files affected by the problem:<br>";
167
		echo implode('<br>', array_diff($changed_files_list, $update_files));
168
	}
169
}
170
else
171
{
172
	echo 'Successfully created detailed-version.js!';
173
}
174
175
/**
176
 * Get the listing of changed files between two releases
177
 *
178
 * @param string $from
179
 * @param string $to
180
 *
181
 * @return array
182
 */
183
function getFilesChanged($from, $to)
184
{
185
	global $settings;
186
187
	echo 'Running Command: git diff --name-only --pretty=oneline --full-index ElkArte/' . $from . '..ElkArte/' . $to . ' | sort | uniq<br>';
188
189
	$output = shell_exec('git diff --name-only --pretty=oneline --full-index ElkArte/' . $from . '..ElkArte/' . $to . ' | sort | uniq');
190
	if (empty($output))
191
	{
192
		echo "The git command failed to return any results\n";
193
		//die;
194
	}
195
196
	$dirs = array(
197
		str_replace(BOARDDIR . '/', '', SOURCEDIR . '/database/') => 'database',
198
		str_replace(BOARDDIR . '/', '', SUBSDIR . '/') => 'subs',
199
		str_replace(BOARDDIR . '/', '', CONTROLLERDIR . '/') => 'controllers',
200
		str_replace(BOARDDIR . '/', '', SOURCEDIR . '/') => 'sources',
201
		str_replace(BOARDDIR . '/', '', ADMINDIR . '/') => 'admin',
202
		str_replace(BOARDDIR . '/', '', ADMINDIR . '/') => 'admin',
203
		str_replace(BOARDDIR . '/', '', $settings['theme_dir'] . '/') => 'default',
204
	);
205
206
	$files = array_filter(explode("\n", $output));
207
	$list = array();
208
	foreach ($files as $file)
209
	{
210
		if ($file[0] === '.')
211
		{
212
			continue;
213
		}
214
215
		if (strpos($file, 'README') !== false)
216
		{
217
			continue;
218
		}
219
220
		if (strpos($file, 'install') !== false)
221
		{
222
			continue;
223
		}
224
225
		if (strpos($file, 'release_tools') !== false)
226
		{
227
			continue;
228
		}
229
230
		if (strpos($file, '/ext') !== false)
231
		{
232
			continue;
233
		}
234
235
		if (strpos($file, 'tests') !== false)
236
		{
237
			continue;
238
		}
239
240
		if (strpos($file, 'fonts') !== false)
241
		{
242
			continue;
243
		}
244
245
		if (strpos($file, '/scripts') !== false)
246
		{
247
			continue;
248
		}
249
250
		if (strpos($file, 'docs/') !== false)
251
		{
252
			continue;
253
		}
254
255
		if (strpos($file, '/images') !== false)
256
		{
257
			continue;
258
		}
259
260
		if (strpos($file, '/css') !== false)
261
		{
262
			continue;
263
		}
264
265
		if (strpos($file, '/languages') !== false)
266
		{
267
			continue;
268
		}
269
270
		if (strpos($file, 'packages') !== false)
271
		{
272
			continue;
273
		}
274
275
		if (strpos($file, '.txt') !== false || strpos($file, '.json') !== false)
276
		{
277
			continue;
278
		}
279
280
		if ($file === 'index.php')
281
		{
282
			continue;
283
		}
284
285
		if ($file === 'ssi_examples.php')
286
		{
287
			continue;
288
		}
289
290
		if ($file === 'ssi_examples.shtml')
291
		{
292
			continue;
293
		}
294
295
		if ($file === 'elkServiceWorker.min.js')
296
		{
297
			continue;
298
		}
299
300
		if ($file === 'SSI.php')
301
		{
302
			$list[] = 'sourcesSSI.php';
303
			continue;
304
		}
305
306
		if ($file === 'subscriptions.php' || $file === 'bootstrap.php' || $file === 'email_imap_cron.php' || $file === 'emailpost.php' || $file === 'emailtopic.php')
307
		{
308
			$list[] = 'sources' . $file;
309
			continue;
310
		}
311
312
		$list[] = strtr($file, $dirs);
313
	}
314
315
	echo 'Found '. count($list) . ' Changed Files<br>';
316
317
	return $list;
318
}
319