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 FindFilesWithSimpleUseStatements extends Task |
9
|
|
|
{ |
10
|
|
|
protected $taskStep = 's60'; |
11
|
|
|
|
12
|
|
|
protected $listOfOKOnes = [ |
13
|
|
|
'Page', |
14
|
|
|
'PageController', |
15
|
|
|
'Exception', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public function getTitle() |
19
|
|
|
{ |
20
|
|
|
return 'Finds files simple use statements (may indicate error!)'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getDescription() |
24
|
|
|
{ |
25
|
|
|
return ' |
26
|
|
|
Goes through all the PHP files and |
27
|
|
|
makes sure that there are no simple use statements, |
28
|
|
|
apart from things like "use \\Page;". '; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function runActualTask($params = []): ?string |
32
|
|
|
{ |
33
|
|
|
$errors = []; |
34
|
|
|
foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
|
|
|
|
35
|
|
|
$this->mu()->colourPrint('Searching ' . $moduleDir, 'grey'); |
|
|
|
|
36
|
|
|
$fileFinder = new FindFiles(); |
37
|
|
|
$searchPath = $this->mu()->findMyCodeDir($moduleDir); |
|
|
|
|
38
|
|
|
if (file_exists($searchPath)) { |
39
|
|
|
$flatArray = $fileFinder |
40
|
|
|
->setSearchPath($searchPath) |
41
|
|
|
->setExtensions(['php']) |
42
|
|
|
->getFlatFileArray(); |
43
|
|
|
if (is_array($flatArray) && count($flatArray)) { |
44
|
|
|
foreach ($flatArray as $path) { |
45
|
|
|
$this->mu()->colourPrint('Searching ' . $path, 'grey'); |
46
|
|
|
// $className = basename($path, '.php'); |
47
|
|
|
$content = file_get_contents($path); |
48
|
|
|
$tokens = token_get_all($content); |
49
|
|
|
for ($index = 0; isset($tokens[$index]); $index++) { |
50
|
|
|
if (! isset($tokens[$index][0])) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
if ($tokens[$index][0] === T_USE && |
54
|
|
|
$tokens[$index + 1][0] === T_WHITESPACE && |
55
|
|
|
$tokens[$index + 2][0] === T_STRING && |
56
|
|
|
$tokens[$index + 3] === ';' |
57
|
|
|
) { |
58
|
|
|
$string = $tokens[$index + 2][1]; |
59
|
|
|
if (! in_array($string, $this->listOfOKOnes, true)) { |
60
|
|
|
$testPhrase = ltrim($string, '\\'); |
61
|
|
|
if (! strpos($testPhrase, '\\')) { |
62
|
|
|
$errors[] = $path . ': ' . $tokens[$index][1] . |
63
|
|
|
$tokens[$index + 1][1] . $tokens[$index + 2][1] . ';'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$index += 3; // Skip checked ones ... |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$this->mu()->colourPrint('Could not find any files in ' . $searchPath, 'red'); |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
$this->mu()->colourPrint('Could not find ' . $searchPath, 'blue'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
if (count($errors)) { |
78
|
|
|
$error = 'Found errors in use statements: ' . "\n---\n---\n---\n" . implode("\n ---\n", $errors); |
79
|
|
|
if (count($errors) > 10) { |
80
|
|
|
return $error; |
81
|
|
|
} |
82
|
|
|
$this->mu()->colourPrint($error, 'red'); |
83
|
|
|
} else { |
84
|
|
|
$this->mu()->colourPrint('Clean bill of health in terms of use statements.', 'green'); |
85
|
|
|
} |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function hasCommitAndPush() |
90
|
|
|
{ |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function testme() |
95
|
|
|
{ |
96
|
|
|
// $string = "<?php |
97
|
|
|
// echo 'xxx';"; |
98
|
|
|
// /* Use tab and newline as tokenizing characters as well */ |
99
|
|
|
// $tok = token_get_all($string); |
100
|
|
|
// |
101
|
|
|
// for ($index = 0; isset($tok[$index]); $index++) { |
102
|
|
|
// print_r($tok[$index]); |
103
|
|
|
// echo '-----'; |
104
|
|
|
// } |
105
|
|
|
// die('xxx'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|