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