1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
4
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Api\FindFiles; |
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
7
|
|
|
|
8
|
|
|
class FindFilesWithMoreThanOneClass extends Task |
9
|
|
|
{ |
10
|
|
|
protected $taskStep = 's10'; |
11
|
|
|
|
12
|
|
|
public function getTitle() |
13
|
|
|
{ |
14
|
|
|
return 'Finds files with more than one class'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getDescription() |
18
|
|
|
{ |
19
|
|
|
return ' |
20
|
|
|
Goes through all the PHP files and makes sure that only one class is defined. If any are found than the code exits as you should fix this first!'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function runActualTask($params = []) |
24
|
|
|
{ |
25
|
|
|
foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
26
|
|
|
$errors = []; |
27
|
|
|
$searchPath = $this->mu()->findMyCodeDir($moduleDir); |
28
|
|
|
if (file_exists($searchPath)) { |
29
|
|
|
$this->mu()->colourPrint('Searching in ' . $searchPath, 'blue for files with more than one class.'); |
30
|
|
|
$fileFinder = new FindFiles($searchPath); |
31
|
|
|
$flatArray = $fileFinder |
32
|
|
|
->setSearchPath($searchPath) |
33
|
|
|
->setExtensions(['php']) |
34
|
|
|
->getFlatFileArray(); |
35
|
|
|
if (is_array($flatArray) && count($flatArray)) { |
36
|
|
|
foreach ($flatArray as $path) { |
37
|
|
|
$className = basename($path, '.php'); |
|
|
|
|
38
|
|
|
$classNames = []; |
39
|
|
|
$content = file_get_contents($path); |
40
|
|
|
$tokens = token_get_all($content); |
41
|
|
|
$namespace = ''; |
|
|
|
|
42
|
|
|
for ($index = 0; isset($tokens[$index]); $index++) { |
43
|
|
|
if (! isset($tokens[$index][0])) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
if ($tokens[$index][0] === T_CLASS && $tokens[$index + 1][0] === T_WHITESPACE && $tokens[$index + 2][0] === T_STRING) { |
47
|
|
|
$index += 2; // Skip class keyword and whitespace |
48
|
|
|
$classNames[] = $tokens[$index][1]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
if (count($classNames) > 1) { |
52
|
|
|
$errors[] = $path . ': ' . implode(', ', $classNames); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
$this->mu()->colourPrint('Could not find any files in ' . $searchPath, 'red'); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->mu()->colourPrint('Could not find ' . $searchPath, 'blue'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
if (count($errors)) { |
|
|
|
|
63
|
|
|
return 'Found files with multiple classes: ' . implode("\n\n ---\n\n", $errors); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function hasCommitAndPush() |
68
|
|
|
{ |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|