Total Complexity | 79 |
Total Lines | 502 |
Duplicated Lines | 0 % |
Changes | 50 | ||
Bugs | 10 | Features | 2 |
Complex classes like UpdateModules often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UpdateModules, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class UpdateModules extends BuildTask |
||
9 | { |
||
10 | protected $enabled = true; |
||
11 | |||
12 | protected $title = "Update Modules"; |
||
13 | |||
14 | protected $description = "Adds files necessary for publishing a module to GitHub. The list of modules is specified in standard config or else it retrieves a list of modules from GitHub."; |
||
15 | |||
16 | /** |
||
17 | * e.g. |
||
18 | * - moduleA |
||
19 | * - moduleB |
||
20 | * - moduleC |
||
21 | * |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private static $modules_to_update = array(); |
||
|
|||
26 | |||
27 | /** |
||
28 | * e.g. |
||
29 | * - ClassNameForUpdatingFileA |
||
30 | * - ClassNameForUpdatingFileB |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private static $files_to_update = []; |
||
35 | /** |
||
36 | * e.g. |
||
37 | * - ClassNameForUpdatingFileA |
||
38 | * - ClassNameForUpdatingFileB |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private static $commands_to_run = array(); |
||
43 | |||
44 | public static $unsolvedItems = array(); |
||
45 | |||
46 | public function run($request) |
||
47 | { |
||
48 | increase_time_limit_to(3600); |
||
49 | |||
50 | //Check temp module folder is empty |
||
51 | $tempFolder = GitHubModule::Config()->get('absolute_temp_folder'); |
||
52 | if(! file_exists($tempFolder)) { |
||
53 | FileSystem::makeFolder($tempFolder); |
||
54 | } |
||
55 | $tempDirFiles = scandir($tempFolder); |
||
56 | if (count($tempDirFiles) > 2) { |
||
57 | die('<h2>' . $tempFolder . ' is not empty, please delete or move files </h2>'); |
||
58 | } |
||
59 | |||
60 | //Get list of all modules from GitHub |
||
61 | $gitUserName = $this->Config()->get('github_user_name'); |
||
62 | |||
63 | $modules = GitRepoFinder::get_all_repos(); |
||
64 | |||
65 | |||
66 | /* |
||
67 | * Get files to add to modules |
||
68 | * */ |
||
69 | $files = ClassInfo::subclassesFor('AddFileToModule'); |
||
70 | array_shift($files); |
||
71 | $limitedFileClasses = $this->Config()->get('files_to_update'); |
||
72 | if ($limitedFileClasses === []) { |
||
73 | //do nothing |
||
74 | } elseif ($limitedFileClasses === 'none') { |
||
75 | $files = []; |
||
76 | } elseif (is_array($limitedFileClasses) && count($limitedFileClasses)) { |
||
77 | $files = array_intersect($files, $limitedFileClasses); |
||
78 | } |
||
79 | |||
80 | /* |
||
81 | * Get commands to run on modules |
||
82 | * */ |
||
83 | |||
84 | $commands = ClassInfo::subclassesFor('RunCommandLineMethodOnModule'); |
||
85 | array_shift($commands); |
||
86 | $limitedCommands = $this->Config()->get('commands_to_run'); |
||
87 | if ($limitedCommands === 'none') { |
||
88 | $commands = []; |
||
89 | } elseif (is_array($limitedCommands) && count($limitedCommands)) { |
||
90 | $commands = array_intersect($commands, $limitedCommands); |
||
91 | } |
||
92 | |||
93 | |||
94 | set_error_handler('errorHandler', E_ALL); |
||
95 | foreach ($modules as $count => $module) { |
||
96 | $this->currentModule = $module; |
||
97 | try { |
||
98 | $this->processOneModule($module, $count, $files, $commands); |
||
99 | } catch (Exception $e) { |
||
100 | GeneralMethods::output_to_screen("<li> Could not complete processing $module: " . $e->getMessage() . " </li>"); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | restore_error_handler(); |
||
105 | |||
106 | $this->writeLog(); |
||
107 | //to do .. |
||
108 | } |
||
109 | |||
110 | protected function errorHandler(int $errno, string $errstr) |
||
111 | { |
||
112 | GeneralMethods::output_to_screen("<li> Could not complete processing module: " . $errstr . " </li>"); |
||
113 | |||
114 | UpdateModules::addUnsolvedProblem($this->currentModule, "Could not complete processing module: " . $errstr); |
||
115 | |||
116 | return true; |
||
117 | } |
||
118 | |||
119 | protected function processOneModule($module, $count, $files, $commands) |
||
120 | { |
||
121 | if (stripos($module, 'silverstripe-') === false) { |
||
122 | $module = "silverstripe-" . $module; |
||
123 | } |
||
124 | echo "<h2>" . ($count+1) . ". ".$module."</h2>"; |
||
125 | |||
126 | |||
127 | $moduleObject = GitHubModule::get_or_create_github_module($module); |
||
128 | |||
129 | $this->checkUpdateTag($moduleObject); |
||
130 | |||
131 | $updateComposerJson = $this->Config()->get('update_composer_json'); |
||
132 | |||
133 | // Check if all necessary files are perfect on GitHub repo already, |
||
134 | // if so we can skip that module. But! ... if there are commands to run |
||
135 | // over the files in the repo, then we need to clone the repo anyhow, |
||
136 | // so skip the check |
||
137 | if (count($commands) == 0 && ! $updateComposerJson) { |
||
138 | $moduleFilesOK = true; |
||
139 | |||
140 | foreach ($files as $file) { |
||
141 | $fileObj = $file::create($moduleObject); |
||
142 | $checkFileName = $fileObj->getFileLocation(); |
||
143 | $GitHubFileText = $moduleObject -> getRawFileFromGithub($checkFileName); |
||
144 | if ($GitHubFileText) { |
||
145 | $fileCheck = $fileObj->compareWithText($GitHubFileText); |
||
146 | if (! $fileCheck) { |
||
147 | $moduleFilesOK = false; |
||
148 | } |
||
149 | } else { |
||
150 | $moduleFilesOK = false; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $repository = $moduleObject->checkOrSetGitCommsWrapper($forceNew = true); |
||
156 | |||
157 | |||
158 | $this->moveOldReadMe($moduleObject); |
||
159 | |||
160 | |||
161 | $checkConfigYML = $this->Config()->get('check_config_yml'); |
||
162 | if ($checkConfigYML) { |
||
163 | $this->checkConfigYML($moduleObject); |
||
164 | } |
||
165 | |||
166 | if ($updateComposerJson) { |
||
167 | $composerJsonObj = new ComposerJson($moduleObject); |
||
168 | $composerJsonObj->updateJsonFile(); |
||
169 | $moduleObject->setDescription($composerJsonObj->getDescription()); |
||
170 | } |
||
171 | |||
172 | $excludedWords = $this->Config()->get('excluded_words'); |
||
173 | |||
174 | |||
175 | if (count($excludedWords) > 0) { |
||
176 | $folder = GitHubModule::Config()->get('absolute_temp_folder') . '/' . $moduleObject->moduleName . '/'; |
||
177 | |||
178 | $results = $this->checkDirExcludedWords($folder.'/'.$moduleObject->modulename, $excludedWords); |
||
179 | |||
180 | |||
181 | if ($results && count($results > 0)) { |
||
182 | $msg = "<h4>The following excluded words were found: </h4><ul>"; |
||
183 | foreach ($results as $file => $words) { |
||
184 | foreach ($words as $word) { |
||
185 | $msg .= "<li>$word in $file</li>"; |
||
186 | } |
||
187 | } |
||
188 | $msg .= '</ul>'; |
||
189 | |||
190 | //trigger_error ("excluded words found in files(s)"); |
||
191 | GeneralMethods::output_to_screen($msg); |
||
192 | UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | |||
197 | foreach ($files as $file) { |
||
198 | //run file update |
||
199 | |||
200 | $obj = $file::create($moduleObject); |
||
201 | $obj->run(); |
||
202 | } |
||
203 | |||
204 | $moduleDir = $moduleObject->Directory(); |
||
205 | |||
206 | foreach ($commands as $command) { |
||
207 | //run file update |
||
208 | |||
209 | |||
210 | $obj = $command::create($moduleDir); |
||
211 | $obj->run(); |
||
212 | |||
213 | |||
214 | //run command |
||
215 | } |
||
216 | |||
217 | //Update Repository description |
||
218 | //$moduleObject->updateGitHubInfo(array()); |
||
219 | |||
220 | if (! $moduleObject->add()) { |
||
221 | $msg = "Could not add files module to Repo"; |
||
222 | GeneralMethods::output_to_screen($msg); |
||
223 | UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
||
224 | return; |
||
225 | } |
||
226 | if (! $moduleObject->commit()) { |
||
227 | $msg = "Could not commit files to Repo"; |
||
228 | GeneralMethods::output_to_screen($msg); |
||
229 | UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
||
230 | return; |
||
231 | } |
||
232 | |||
233 | if (! $moduleObject->push()) { |
||
234 | $msg = "Could not push files to Repo"; |
||
235 | GeneralMethods::output_to_screen($msg); |
||
236 | UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
||
237 | return; |
||
238 | } |
||
239 | if (! $moduleObject->removeClone()) { |
||
240 | $msg = "Could not remove local copy of repo"; |
||
241 | GeneralMethods::output_to_screen($msg); |
||
242 | UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
||
243 | } |
||
244 | |||
245 | $addRepoToScrutinzer = $this->Config()->get('add_to_scrutinizer'); |
||
246 | if ($addRepoToScrutinzer) { |
||
247 | $moduleObject->addRepoToScrutinzer(); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | |||
252 | |||
253 | protected function renameTest($moduleObject) |
||
254 | { |
||
255 | $oldName = $moduleObject->Directory() . "/tests/ModuleTest.php"; |
||
256 | |||
257 | if (! file_exists($oldName)) { |
||
258 | print_r($oldName); |
||
259 | return false; |
||
260 | } |
||
261 | |||
262 | |||
263 | |||
264 | $newName = $moduleObject->Directory() . "tests/" . $moduleObject->ModuleName . "Test.php"; |
||
265 | |||
266 | GeneralMethods::output_to_screen("Renaming $oldName to $newName"); |
||
267 | |||
268 | unlink($newName); |
||
269 | |||
270 | rename($oldName, $newName); |
||
271 | } |
||
272 | |||
273 | public static function addUnsolvedProblem($moduleName, $problemString) |
||
274 | { |
||
275 | if (!isset(UpdateModules::$unsolvedItems[$moduleName])) { |
||
276 | UpdateModules::$unsolvedItems[$moduleName] = array(); |
||
277 | } |
||
278 | array_push(UpdateModules::$unsolvedItems[$moduleName], $problemString); |
||
279 | } |
||
280 | |||
281 | protected function writeLog() |
||
322 | } |
||
323 | } |
||
324 | |||
325 | protected function checkConfigYML($module) |
||
326 | { |
||
327 | $configYml = ConfigYML::create($module)->reWrite(); |
||
328 | } |
||
329 | |||
330 | private function checkFile($module, $filename) |
||
331 | { |
||
332 | $folder = GitHubModule::Config()->get('absolute_temp_folder'); |
||
333 | return file_exists($folder.'/'.$module.'/'.$filename); |
||
334 | } |
||
335 | |||
336 | private function checkReadMe($module) |
||
337 | { |
||
338 | return $this->checkFile($module, "README.MD"); |
||
339 | } |
||
340 | |||
341 | private function checkDirExcludedWords($directory, $wordArray) |
||
342 | { |
||
343 | $filesAndFolders = scandir($directory); |
||
344 | |||
345 | $problem_files = array(); |
||
346 | foreach ($filesAndFolders as $fileOrFolder) { |
||
347 | if ($fileOrFolder == '.' || $fileOrFolder == '..' || $fileOrFolder == '.git') { |
||
348 | continue; |
||
349 | } |
||
350 | |||
351 | $fileOrFolderFullPath = $directory . '/' . $fileOrFolder; |
||
352 | if (is_dir($fileOrFolderFullPath)) { |
||
353 | $dir = $fileOrFolderFullPath; |
||
354 | $problem_files = array_merge($this->checkDirExcludedWords($dir, $wordArray), $problem_files); |
||
355 | } |
||
356 | if (is_file($fileOrFolderFullPath)) { |
||
357 | $file = $fileOrFolderFullPath; |
||
358 | $matchedWords = $this->checkFileExcludedWords($file, $wordArray); |
||
359 | |||
360 | if ($matchedWords) { |
||
361 | $problem_files[$file] = $matchedWords; |
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | |||
366 | return $problem_files; |
||
367 | } |
||
368 | |||
369 | private function checkFileExcludedWords($fileName, $wordArray) |
||
370 | { |
||
371 | $matchedWords = array(); |
||
372 | |||
373 | $fileName = str_replace('////', '/', $fileName); |
||
374 | if (filesize($fileName) == 0) { |
||
375 | return $matchedWords; |
||
376 | } |
||
377 | |||
378 | |||
379 | $fileContent = file_get_contents($fileName); |
||
380 | if (!$fileContent) { |
||
381 | $msg = "Could not open $fileName to check for excluded words"; |
||
382 | |||
383 | GeneralMethods::output_to_screen($msg); |
||
384 | UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
||
385 | } |
||
386 | |||
387 | foreach ($wordArray as $word) { |
||
388 | $matches = array(); |
||
389 | $matchCount = preg_match_all('/' . $word . '/i', $fileContent); |
||
390 | |||
391 | |||
392 | |||
393 | |||
394 | |||
395 | if ($matchCount > 0) { |
||
396 | array_push($matchedWords, $word); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | return $matchedWords; |
||
401 | } |
||
402 | |||
403 | private function checkUpdateTag($moduleObject) |
||
453 | } |
||
454 | |||
455 | protected function findNextTag($tag, $changeType) |
||
456 | { |
||
457 | switch ($changeType) { |
||
479 | } |
||
480 | |||
481 | protected function moveOldReadMe($moduleObject) |
||
510 | } |
||
511 | } |
||
512 | } |
||
513 |