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. |
21
|
|
|
If any are found than the code exits as you should fix this first! |
22
|
|
|
'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function runActualTask($params = []): ?string |
26
|
|
|
{ |
27
|
|
|
$errors = []; |
28
|
|
|
foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
|
|
|
|
29
|
|
|
$searchPath = $this->mu()->findMyCodeDir($moduleDir); |
|
|
|
|
30
|
|
|
if (file_exists($searchPath)) { |
31
|
|
|
$this->mu()->colourPrint( |
|
|
|
|
32
|
|
|
'Searching in ' . $searchPath . ' for files with more than one class.', |
33
|
|
|
'blue' |
34
|
|
|
); |
35
|
|
|
$fileFinder = new FindFiles(); |
36
|
|
|
$flatArray = $fileFinder |
37
|
|
|
->setSearchPath($searchPath) |
38
|
|
|
->setExtensions(['php']) |
39
|
|
|
->getFlatFileArray(); |
40
|
|
|
if (is_array($flatArray) && count($flatArray)) { |
41
|
|
|
foreach ($flatArray as $path) { |
42
|
|
|
// $className = basename($path, '.php'); |
43
|
|
|
$classNames = []; |
44
|
|
|
$content = file_get_contents($path); |
45
|
|
|
$tokens = token_get_all($content); |
46
|
|
|
for ($index = 0; isset($tokens[$index]); $index++) { |
47
|
|
|
if (! isset($tokens[$index][0])) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
if ($tokens[$index][0] === T_CLASS && |
51
|
|
|
$tokens[$index + 1][0] === T_WHITESPACE && |
52
|
|
|
$tokens[$index + 2][0] === T_STRING |
53
|
|
|
) { |
54
|
|
|
$index += 2; // Skip class keyword and whitespace |
55
|
|
|
$classNames[] = $tokens[$index][1]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
if (count($classNames) > 1) { |
59
|
|
|
$errors[] = $path . ': ' . implode(', ', $classNames); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
$this->mu()->colourPrint( |
64
|
|
|
'Could not find any files in ' . $searchPath, |
65
|
|
|
'red' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} elseif ($searchPath) { |
69
|
|
|
$this->mu()->colourPrint( |
70
|
|
|
'Could not find the following path: "' . $searchPath, |
71
|
|
|
'blue' |
72
|
|
|
); |
73
|
|
|
} else { |
74
|
|
|
$this->mu()->colourPrint( |
75
|
|
|
'empty search path', |
76
|
|
|
'blue' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (count($errors)) { |
81
|
|
|
return 'Found files with multiple classes: ' . implode("\n\n ---\n\n", $errors); |
82
|
|
|
} |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function hasCommitAndPush() |
87
|
|
|
{ |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|