1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @internal |
7
|
|
|
*/ |
8
|
|
|
class HydeStan |
9
|
|
|
{ |
10
|
|
|
const VERSION = '0.0.0-dev'; |
11
|
|
|
protected array $errors = []; |
12
|
|
|
protected array $files; |
13
|
|
|
protected Console $console; |
14
|
|
|
|
15
|
|
|
public function __construct(protected bool $debug = false) |
16
|
|
|
{ |
17
|
|
|
$this->console = new Console(); |
18
|
|
|
|
19
|
|
|
$this->console->info(sprintf('HydeStan v%s is running!', self::VERSION)); |
20
|
|
|
$this->console->newline(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function __destruct() |
24
|
|
|
{ |
25
|
|
|
$this->console->newline(); |
26
|
|
|
$this->console->info('HydeStan has exited.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function run(): void |
30
|
|
|
{ |
31
|
|
|
$time = microtime(true); |
32
|
|
|
|
33
|
|
|
$this->files = $this->getFiles(); |
34
|
|
|
|
35
|
|
|
foreach ($this->files as $file) { |
36
|
|
|
if ($this->debug) { |
37
|
|
|
$this->console->debug('Analysing file: '.$file); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->analyseFile($file, $this->getFileContents($file)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$endTime = microtime(true) - $time; |
44
|
|
|
$this->console->info(sprintf('HydeStan has finished in %s seconds (%sms) using %s KB RAM', |
45
|
|
|
number_format($endTime, 2), |
46
|
|
|
number_format($endTime * 1000, 2), |
47
|
|
|
number_format(memory_get_peak_usage(true) / 1024, 2)) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
if ($this->hasErrors()) { |
51
|
|
|
$this->console->error(sprintf('HydeStan has found %s errors!', count($this->errors))); |
52
|
|
|
|
53
|
|
|
foreach ($this->errors as $error) { |
54
|
|
|
$this->console->warn($error); |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$this->console->info('HydeStan has found no errors!'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getErrors(): array |
62
|
|
|
{ |
63
|
|
|
return $this->errors; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getFiles(): array |
67
|
|
|
{ |
68
|
|
|
$files = []; |
69
|
|
|
|
70
|
|
|
$directory = new RecursiveDirectoryIterator(BASE_PATH.'/src'); |
71
|
|
|
$iterator = new RecursiveIteratorIterator($directory); |
72
|
|
|
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH); |
73
|
|
|
|
74
|
|
|
foreach ($regex as $file) { |
75
|
|
|
$files[] = substr($file[0], strlen(BASE_PATH) + 1); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $files; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function analyseFile(string $file, string $contents): void |
82
|
|
|
{ |
83
|
|
|
foreach ($this->analysers() as $analyser) { |
84
|
|
|
if ($this->debug) { |
85
|
|
|
$this->console->debugComment('Running '.$analyser::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$result = $analyser->run($file, $contents); |
89
|
|
|
foreach ($result as $error) { |
90
|
|
|
if ($this->debug) { |
91
|
|
|
$this->console->debugComment('Adding error: '.$error); |
92
|
|
|
} |
93
|
|
|
$this->errors[] = $error; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getFileContents(string $file): string |
99
|
|
|
{ |
100
|
|
|
return file_get_contents(BASE_PATH.'/'.$file); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function analysers(): array |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
new NoFixMeAnalyser(), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function hasErrors(): bool |
111
|
|
|
{ |
112
|
|
|
return count($this->errors) > 0; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
class NoFixMeAnalyser |
117
|
|
|
{ |
118
|
|
|
public function run(string $file, string $contents): array |
119
|
|
|
{ |
120
|
|
|
$errors = []; |
121
|
|
|
|
122
|
|
|
$searches = [ |
123
|
|
|
'fixme', |
124
|
|
|
'fix me', |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$contents = strtolower($contents); |
128
|
|
|
|
129
|
|
|
foreach ($searches as $search) { |
130
|
|
|
if (str_contains($contents, $search)) { |
131
|
|
|
$errors[] = 'Found '.$search.' in '.$file; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $errors; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function dd(...$args): never |
140
|
|
|
{ |
141
|
|
|
foreach ($args as $arg) { |
142
|
|
|
var_dump($arg); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
exit(1); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|