Test Setup Failed
Pull Request — developer (#294)
by Arkadiusz
34:26
created

Composer::clear()   D

Complexity

Conditions 18
Paths 56

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.8666
c 0
b 0
f 0
cc 18
nc 56
nop 0

How to fix   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
namespace App\Installer;
4
5
/**
6
 * Composer installer.
7
 *
8
 * @copyright YetiForce Sp. z o.o
9
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 */
12
class Composer
0 ignored issues
show
Coding Style introduced by
Composer does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
13
{
14
	/**
15
	 * List of public packages.
16
	 *
17
	 * @var atring[]
18
	 */
19
	public static $publicPackage = [
20
		'yetiforce/csrf-magic' => 'move',
21
		'ckeditor/ckeditor' => 'move',
22
	];
23
24
	/**
25
	 * Copy directories.
26
	 *
27
	 * @var array
28
	 */
29
	public static $copyDirectories = [
30
		'public_html/libraries/ckeditor-image-to-base' => 'public_html/vendor/ckeditor/ckeditor/plugins/ckeditor-image-to-base'
31
	];
32
33
	/**
34
	 * Custom copy.
35
	 */
36
	public static function customCopy(): void
37
	{
38
		$list = '';
39
		foreach (static::$copyDirectories as $src => $dest) {
40
			if (!file_exists(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . $dest)) {
41
				mkdir(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . $dest, 0755, true);
42
			}
43
			$i = static::recurseCopy($src, $dest);
44
			$list .= PHP_EOL . "{$src}  >>>  {$dest} | Files: $i";
45
		}
46
		echo "Copy custom directories: $list" . PHP_EOL;
47
	}
48
49
	/**
50
	 * The function copies files.
51
	 *
52
	 * @param string $src
53
	 * @param string $dest
54
	 *
55
	 * @return int
56
	 */
57
	public static function recurseCopy($src, $dest): int
58
	{
59
		$rootDir = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR;
60
		if (!file_exists($rootDir . $src)) {
61
			return 0;
62
		}
63
		if ($dest && '/' !== substr($dest, -1) && '\\' !== substr($dest, -1)) {
64
			$dest = $dest . \DIRECTORY_SEPARATOR;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $dest. This often makes code more readable.
Loading history...
65
		}
66
		$i = 0;
67
		$dest = $rootDir . $dest;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $dest. This often makes code more readable.
Loading history...
68
		foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
69
			if ($item->isDir() && !file_exists($dest . $iterator->getSubPathName())) {
70
				mkdir($dest . $iterator->getSubPathName(), 0755);
71
			} elseif (!$item->isDir()) {
72
				copy($item->getRealPath(), $dest . $iterator->getSubPathName());
73
				++$i;
74
			}
75
		}
76
		return $i;
77
	}
78
79
	/**
80
	 * List of redundant files.
81
	 *
82
	 * @var array
83
	 */
84
	public static $clearFiles = [
85
		'.github',
86
		'.git',
87
		'.gitattributes',
88
		'.gitignore',
89
		'.gitkeep',
90
		'.editorconfig',
91
		'.styleci.yml',
92
		'.travis.yml',
93
		'mkdocs.yml',
94
		'.coveralls.yml',
95
		'.scrutinizer.yml',
96
		'.php_cs.dist',
97
		'build.xml',
98
		'phpunit.xml.dist',
99
		'phpunit.xml',
100
		'changelog.phpexcel.md',
101
		'changelog.md',
102
		'changes.md',
103
		'contributing.md',
104
		'readme.md',
105
		'security.md',
106
		'upgrade.md',
107
		'docs',
108
		'demo',
109
		'examples',
110
		'extras',
111
		'install',
112
		'js-test',
113
		'maintenance',
114
		'migrations',
115
		'news',
116
		'phorum',
117
		'readme',
118
		'sample',
119
		'samples',
120
		'todo',
121
		'test',
122
		'tests',
123
		'whatsnew',
124
		'wysiwyg',
125
		'views',
126
		'_translationstatus.txt',
127
		'composer_release_notes.txt',
128
		'change_log.txt',
129
		'cldr-version.txt',
130
		'inheritance_release_notes.txt',
131
		'metadata-version.txt',
132
		'modx.txt',
133
		'news.txt',
134
		'new_features.txt',
135
		'readme.txt',
136
		'smarty_2_bc_notes.txt',
137
		'smarty_3.0_bc_notes.txt',
138
		'smarty_3.1_notes.txt',
139
		'.sami.php',
140
		'get_oauth_token.php',
141
		'test-settings.sample.php',
142
		'test-settings.travis.php',
143
		'install.fr.utf8',
144
		'jquery.min.js',
145
		'release1-update.php',
146
		'release2-tag.php',
147
		'phpdoc.ini',
148
		'crowdin.yml',
149
		'sonar-project.properties',
150
	];
151
152
	public static $clearFilesModule = [
153
	];
154
155
	/**
156
	 * Post update and post install function.
157
	 *
158
	 * @param \Composer\Script\Event $event
159
	 */
160
	public static function install(\Composer\Script\Event $event)
161
	{
162
		static::clear();
163
		$event->getComposer();
164
		if (isset($_SERVER['SENSIOLABS_EXECUTION_NAME'])) {
165
			return true;
166
		}
167
		$rootDir = realpath(__DIR__ . '/../../') . \DIRECTORY_SEPARATOR . 'public_html' . \DIRECTORY_SEPARATOR;
168
		$types = ['js', 'css', 'woff', 'woff2', 'ttf', 'png', 'gif', 'jpg', 'json'];
169
		foreach (static::$publicPackage as $package => $method) {
170
			$src = 'vendor' . \DIRECTORY_SEPARATOR . $package;
171
			foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
172
				if ($item->isFile() && \in_array($item->getExtension(), $types) && !file_exists($rootDir . $item->getPathname())) {
173
					if (!is_dir($rootDir . $item->getPath())) {
174
						mkdir($rootDir . $item->getPath(), 0755, true);
175
					}
176
					if (!is_writable($rootDir . $item->getPath())) {
177
						continue;
178
					}
179
					if ('move' === $method) {
180
						\rename($item->getRealPath(), $rootDir . $item->getPathname());
181
					} elseif ('copy' === $method) {
182
						\copy($item->getRealPath(), $rootDir . $item->getPathname());
183
					}
184
				}
185
			}
186
		}
187
		self::customCopy();
188
	}
189
190
	/**
191
	 * Delete redundant files.
192
	 */
193
	public static function clear()
194
	{
195
		$rootDir = realpath(__DIR__ . '/../../') . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR;
196
		$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootDir), \RecursiveIteratorIterator::SELF_FIRST);
197
		$deleted = [];
198
		foreach ($objects as $object) {
199
			if ('.' === $object->getFilename() || '..' === $object->getFilename()) {
200
				continue;
201
			}
202
			if ((\in_array(strtolower($object->getFilename()), self::$clearFiles)) && (is_dir($object->getPathname()) || file_exists($object->getPathname()))) {
203
				$deleted[] = $object->getPathname();
204
			}
205
		}
206
		$deletedCount = 0;
207
		$deleted = array_unique($deleted);
208
		arsort($deleted);
209
		foreach ($deleted as $delete) {
210
			\App\Utils::recurseDelete($delete, true);
0 ignored issues
show
Unused Code introduced by
The call to Utils::recurseDelete() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
211
			++$deletedCount;
212
		}
213
		foreach (new \DirectoryIterator($rootDir) as $level1) {
214
			if ($level1->isDir() && !$level1->isDot()) {
215
				foreach (new \DirectoryIterator($level1->getPathname()) as $level2) {
216
					if ($level2->isDir() && !$level2->isDot()) {
217
						foreach (new \DirectoryIterator($level2->getPathname()) as $level3) {
218
							if (isset(self::$clearFilesModule[$level1->getFileName() . '/' . $level2->getFilename()]) && !$level3->isDot() && \in_array(strtolower($level3->getFilename()), self::$clearFilesModule[$level1->getFileName() . '/' . $level2->getFilename()])) {
219
								\App\Utils::recurseDelete($level3->getPathname(), true);
0 ignored issues
show
Unused Code introduced by
The call to Utils::recurseDelete() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
220
								++$deletedCount;
221
							}
222
						}
223
					}
224
				}
225
			}
226
		}
227
		echo "Cleaned files: $deletedCount" . PHP_EOL;
228
	}
229
}
230