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

Composer::customCopy()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 1
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
		'libraries/ckeditor-image-to-base' => 'vendor/ckeditor/ckeditor/plugins/ckeditor-image-to-base'
31
	];
32
33
	/**
34
	 * Custom copy.
35
	 *
36
	 * @param string $rootDir
37
	 */
38
	public static function customCopy(string $rootDir): void
39
	{
40
		if (!\defined('ROOT_DIRECTORY')) {
41
			\define('ROOT_DIRECTORY', $rootDir);
42
		}
43
		$list = '';
44
		foreach (static::$copyDirectories as $src => $dest) {
45
			if (!file_exists(ROOT_DIRECTORY . $dest)) {
46
				mkdir(ROOT_DIRECTORY . $dest, 0755, true);
47
			}
48
			$i = static::recurseCopy($src, $dest);
49
			$list .= PHP_EOL . "{$src}  >>>  {$dest} | Files: $i";
50
		}
51
		echo "Copy custom directories: $list" . PHP_EOL;
52
	}
53
54
	/**
55
	 * The function copies files.
56
	 *
57
	 * @param string $src
58
	 * @param string $dest
59
	 *
60
	 * @return int
61
	 */
62
	public static function recurseCopy(string $src, string $dest): int
63
	{
64
		if (!file_exists(ROOT_DIRECTORY . $src)) {
65
			return 0;
66
		}
67
		if ($dest && '/' !== substr($dest, -1) && '\\' !== substr($dest, -1)) {
68
			$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...
69
		}
70
		$i = 0;
71
		$dest = ROOT_DIRECTORY . $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...
72
		foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(ROOT_DIRECTORY . $src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
73
			if ($item->isDir() && !file_exists($dest . $iterator->getSubPathName())) {
74
				mkdir($dest . $iterator->getSubPathName(), 0755);
75
			} elseif (!$item->isDir()) {
76
				copy($item->getRealPath(), $dest . $iterator->getSubPathName());
77
				++$i;
78
			}
79
		}
80
		return $i;
81
	}
82
83
	/**
84
	 * List of redundant files.
85
	 *
86
	 * @var array
87
	 */
88
	public static $clearFiles = [
89
		'.github',
90
		'.git',
91
		'.gitattributes',
92
		'.gitignore',
93
		'.gitkeep',
94
		'.editorconfig',
95
		'.styleci.yml',
96
		'.travis.yml',
97
		'mkdocs.yml',
98
		'.coveralls.yml',
99
		'.scrutinizer.yml',
100
		'.php_cs.dist',
101
		'build.xml',
102
		'phpunit.xml.dist',
103
		'phpunit.xml',
104
		'changelog.phpexcel.md',
105
		'changelog.md',
106
		'changes.md',
107
		'contributing.md',
108
		'readme.md',
109
		'security.md',
110
		'upgrade.md',
111
		'docs',
112
		'demo',
113
		'examples',
114
		'extras',
115
		'install',
116
		'js-test',
117
		'maintenance',
118
		'migrations',
119
		'news',
120
		'phorum',
121
		'readme',
122
		'sample',
123
		'samples',
124
		'todo',
125
		'test',
126
		'tests',
127
		'whatsnew',
128
		'wysiwyg',
129
		'views',
130
		'_translationstatus.txt',
131
		'composer_release_notes.txt',
132
		'change_log.txt',
133
		'cldr-version.txt',
134
		'inheritance_release_notes.txt',
135
		'metadata-version.txt',
136
		'modx.txt',
137
		'news.txt',
138
		'new_features.txt',
139
		'readme.txt',
140
		'smarty_2_bc_notes.txt',
141
		'smarty_3.0_bc_notes.txt',
142
		'smarty_3.1_notes.txt',
143
		'.sami.php',
144
		'get_oauth_token.php',
145
		'test-settings.sample.php',
146
		'test-settings.travis.php',
147
		'install.fr.utf8',
148
		'jquery.min.js',
149
		'release1-update.php',
150
		'release2-tag.php',
151
		'phpdoc.ini',
152
		'crowdin.yml',
153
		'sonar-project.properties',
154
	];
155
156
	public static $clearFilesModule = [
157
	];
158
159
	/**
160
	 * Post update and post install function.
161
	 *
162
	 * @param \Composer\Script\Event $event
163
	 */
164
	public static function install(\Composer\Script\Event $event)
165
	{
166
		static::clear();
167
		$event->getComposer();
168
		if (isset($_SERVER['SENSIOLABS_EXECUTION_NAME'])) {
169
			return true;
170
		}
171
		$rootDir = realpath(__DIR__ . '/../../') . \DIRECTORY_SEPARATOR . 'public_html' . \DIRECTORY_SEPARATOR;
172
		$types = ['js', 'css', 'woff', 'woff2', 'ttf', 'png', 'gif', 'jpg', 'json'];
173
		foreach (static::$publicPackage as $package => $method) {
174
			$src = 'vendor' . \DIRECTORY_SEPARATOR . $package;
175
			foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
176
				if ($item->isFile() && \in_array($item->getExtension(), $types) && !file_exists($rootDir . $item->getPathname())) {
177
					if (!is_dir($rootDir . $item->getPath())) {
178
						mkdir($rootDir . $item->getPath(), 0755, true);
179
					}
180
					if (!is_writable($rootDir . $item->getPath())) {
181
						continue;
182
					}
183
					if ('move' === $method) {
184
						\rename($item->getRealPath(), $rootDir . $item->getPathname());
185
					} elseif ('copy' === $method) {
186
						\copy($item->getRealPath(), $rootDir . $item->getPathname());
187
					}
188
				}
189
			}
190
		}
191
		self::customCopy($rootDir);
192
	}
193
194
	/**
195
	 * Delete redundant files.
196
	 */
197
	public static function clear()
198
	{
199
		$rootDir = realpath(__DIR__ . '/../../') . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR;
200
		$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootDir), \RecursiveIteratorIterator::SELF_FIRST);
201
		$deleted = [];
202
		foreach ($objects as $object) {
203
			if ('.' === $object->getFilename() || '..' === $object->getFilename()) {
204
				continue;
205
			}
206
			if ((\in_array(strtolower($object->getFilename()), self::$clearFiles)) && (is_dir($object->getPathname()) || file_exists($object->getPathname()))) {
207
				$deleted[] = $object->getPathname();
208
			}
209
		}
210
		$deletedCount = 0;
211
		$deleted = array_unique($deleted);
212
		arsort($deleted);
213
		foreach ($deleted as $delete) {
214
			\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...
215
			++$deletedCount;
216
		}
217
		foreach (new \DirectoryIterator($rootDir) as $level1) {
218
			if ($level1->isDir() && !$level1->isDot()) {
219
				foreach (new \DirectoryIterator($level1->getPathname()) as $level2) {
220
					if ($level2->isDir() && !$level2->isDot()) {
221
						foreach (new \DirectoryIterator($level2->getPathname()) as $level3) {
222
							if (isset(self::$clearFilesModule[$level1->getFileName() . '/' . $level2->getFilename()]) && !$level3->isDot() && \in_array(strtolower($level3->getFilename()), self::$clearFilesModule[$level1->getFileName() . '/' . $level2->getFilename()])) {
223
								\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...
224
								++$deletedCount;
225
							}
226
						}
227
					}
228
				}
229
			}
230
		}
231
		echo "Cleaned files: $deletedCount" . PHP_EOL;
232
	}
233
}
234