Passed
Branch development (7a3bc2)
by Elk
06:00
created

getFilesChanged()   D

Complexity

Conditions 27
Paths 42

Size

Total Lines 126
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

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

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
 * @package   ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
7
 *
8
 * @version 2.0 dev
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\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\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', dirname(__FILE__));
36
DEFINE('LANGUAGEDIR', SOURCEDIR . '/ElkArte/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
$changed_files_list = getFilesChanged('master', $new_release);
61
$update_files = array();
62
63
// Now we need to grab the current version of the forum from index.php
64
$index = file_get_contents(BOARDDIR . '/bootstrap.php');
65
$index_lines = explode("\n", $index);
66
foreach ($index_lines as $line)
67
{
68
	if (strpos($line, 'define(\'FORUM_VERSION') !== false)
69
	{
70
		preg_match('~\'(ElkArte .*)\'\);$~', $line, $matches);
71
		$forum_version = $matches[1];
72
		break;
73
	}
74
}
75
76
$handle = fopen($output_file_name, 'w');
77
78
// Start with the common thing
79
fwrite($handle, 'window.ourVersions = {');
80
fwrite($handle, "\n\t'Version': '{$forum_version}',\n");
81
82
foreach (array('admin', 'controllers', 'database', 'subs') as $type)
83
{
84
	foreach ($version_info['file_versions_' . $type] as $file => $ver)
85
	{
86
		if ($new_version == $ver)
87
		{
88
			$update_files[] = str_replace('subssubs', 'subs', $type . $file);
89
		}
90
		fwrite($handle, "\t'{$type}{$file}': '{$ver}',\n");
91
	}
92
}
93
94
foreach ($version_info['file_versions_modules'] as $file => $ver)
95
{
96
	if ($new_version == $ver)
97
	{
98
		$update_files[] = 'sources' . $file;
99
	}
100
	fwrite($handle, "\t'{$type}{$file}': '{$ver}',\n");
101
}
102
103
foreach ($version_info['file_versions'] as $file => $ver)
104
{
105
	if ($new_version == $ver)
106
	{
107
		$update_files[] = 'sources' . $file;
108
	}
109
	fwrite($handle, "\t'sources{$file}': '{$ver}',\n");
110
}
111
112
foreach ($version_info['default_template_versions'] as $file => $ver)
113
{
114
	if ($new_version == $ver)
115
	{
116
		$update_files[] = 'default' . $file;
117
	}
118
	fwrite($handle, "\t'default{$file}': '{$ver}',\n");
119
}
120
121
// Let's close the "core" files and start the language files
122
fwrite($handle, '};');
123
fwrite($handle, "\n\nourLanguageVersions = {\n");
124
125
foreach ($version_info['default_language_versions'] as $lang => $files)
126
{
127
	if ($lang == 'english')
128
	{
129
		foreach ($files as $file => $ver)
130
		{
131
			fwrite($handle, "\t'{$file}': '{$ver}',\n");
132
		}
133
		break;
134
	}
135
}
136
137
// And that's all folks!
138
fwrite($handle, '};');
139
140
fclose($handle);
141
if (count(array_diff($update_files, $changed_files_list)) !== 0)
142
{
143
	if (empty($_GET))
144
	{
145
		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";
146
		print_r(array_diff($update_files, $changed_files_list));
147
	}
148
	else
149
	{
150
		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>";
151
		echo implode('<br>', array_diff($update_files, $changed_files_list));
152
	}
153
}
154
155
if (count(array_diff($changed_files_list, $update_files)) !== 0)
156
{
157
	if (empty($_GET))
158
	{
159
		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";
160
		print_r(array_diff($changed_files_list, $update_files));
161
	}
162
	else
163
	{
164
		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>";
165
		echo implode('<br>', array_diff($changed_files_list, $update_files));
166
	}
167
}
168
169
/**
170
 * Get the listing of changed files between two releases
171
 *
172
 * @param string $from
173
 * @param string $to
174
 *
175
 * @return array
176
 */
177
function getFilesChanged($from, $to)
178
{
179
	global $settings;
180
181
	$output = shell_exec('git diff --name-only --pretty=oneline --full-index ' . $from . '..' . $to . ' | sort | uniq');
182
	if (empty($output))
183
	{
184
		echo "The git command failed to return any results\n";
185
		//die;
186
	}
187
188
	$dirs = array(
189
		str_replace(BOARDDIR . '/', '', SOURCEDIR . '/database/') => 'database',
190
		str_replace(BOARDDIR . '/', '', SUBSDIR . '/') => 'subs',
191
		str_replace(BOARDDIR . '/', '', CONTROLLERDIR . '/') => 'controllers',
192
		str_replace(BOARDDIR . '/', '', SOURCEDIR . '/') => 'sources',
193
		str_replace(BOARDDIR . '/', '', ADMINDIR . '/') => 'admin',
194
		str_replace(BOARDDIR . '/', '', ADMINDIR . '/') => 'admin',
195
		str_replace(BOARDDIR . '/', '', $settings['theme_dir'] . '/') => 'default',
196
	);
197
198
	$files = array_filter(explode("\n", $output));
199
	$list = array();
200
	foreach ($files as $file)
201
	{
202
		if ($file[0] === '.')
203
		{
204
			continue;
205
		}
206
207
		if (strpos($file, 'install') !== false)
208
		{
209
			continue;
210
		}
211
212
		if (strpos($file, 'release_tools') !== false)
213
		{
214
			continue;
215
		}
216
217
		if (strpos($file, '/ext') !== false)
218
		{
219
			continue;
220
		}
221
222
		if (strpos($file, 'tests') !== false)
223
		{
224
			continue;
225
		}
226
227
		if (strpos($file, 'fonts') !== false)
228
		{
229
			continue;
230
		}
231
232
		if (strpos($file, '/scripts') !== false)
233
		{
234
			continue;
235
		}
236
237
		if (strpos($file, 'docs/') !== false)
238
		{
239
			continue;
240
		}
241
242
		if (strpos($file, '/images') !== false)
243
		{
244
			continue;
245
		}
246
247
		if (strpos($file, '/css') !== false)
248
		{
249
			continue;
250
		}
251
252
		if (strpos($file, '/languages') !== false)
253
		{
254
			continue;
255
		}
256
257
		if (strpos($file, 'packages') !== false)
258
		{
259
			continue;
260
		}
261
262
		if (strpos($file, '.txt') !== false || strpos($file, '.json') !== false)
263
		{
264
			continue;
265
		}
266
267
		if ($file === 'index.php')
268
		{
269
			continue;
270
		}
271
272
		if ($file === 'ssi_examples.php')
273
		{
274
			continue;
275
		}
276
277
		if ($file === 'ssi_examples.shtml')
278
		{
279
			continue;
280
		}
281
282
		if ($file === 'elkServiceWorker.min.js')
283
		{
284
			continue;
285
		}
286
287
		if ($file === 'SSI.php')
288
		{
289
			$list[] = 'sourcesSSI.php';
290
			continue;
291
		}
292
293
		if ($file === 'subscriptions.php' || $file === 'bootstrap.php' || $file === 'email_imap_cron.php' || $file === 'emailpost.php' || $file === 'emailtopic.php')
294
		{
295
			$list[] = 'sources' . $file;
296
			continue;
297
		}
298
299
		$list[] = strtr($file, $dirs);
300
	}
301
302
	return $list;
303
}
304