Skip to content

Commit 445eeb9

Browse files
authored
Merge pull request #1750 from hydephp/update-hydestan
Internal: Add statistics output for analysed expressions
2 parents 6c8712a + fa9aaf1 commit 445eeb9

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

monorepo/HydeStan/HydeStan.php

+49-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ public function __construct(private readonly bool $debug = false)
3737
public function __destruct()
3838
{
3939
$this->console->newline();
40-
$this->console->info(sprintf('HydeStan has exited after scanning %s total (and %s aggregate) lines in %s files.',
40+
$this->console->info(sprintf('HydeStan has exited after scanning %s total (and %s aggregate) lines in %s files. Total expressions analysed: %s',
4141
number_format($this->scannedLines),
4242
number_format($this->aggregateLines),
43-
number_format(count($this->files))
43+
number_format(count($this->files)),
44+
number_format(AnalysisStatisticsContainer::getExpressionsAnalysed()),
4445
));
4546

4647
if (count(self::$warnings) > 0) {
@@ -124,13 +125,15 @@ private function analyseFile(string $file, string $contents): void
124125
}
125126

126127
$analyser->run($file, $contents);
128+
AnalysisStatisticsContainer::countedLines(substr_count($contents, "\n"));
127129

128130
foreach (explode("\n", $contents) as $lineNumber => $line) {
129131
$lineAnalysers = [
130132
new NoTestReferenceAnalyser($file, $lineNumber, $line),
131133
];
132134

133135
foreach ($lineAnalysers as $analyser) {
136+
AnalysisStatisticsContainer::countedLine();
134137
$analyser->run($file, $lineNumber, $line);
135138
$this->aggregateLines++;
136139
}
@@ -196,6 +199,7 @@ public function run(string $file, string $contents): void
196199
$contents = strtolower($contents);
197200

198201
foreach ($searches as $search) {
202+
AnalysisStatisticsContainer::analysedExpression();
199203
if (str_contains($contents, $search)) {
200204
// Get line number of marker by counting new \n tags before it
201205
$stringBeforeMarker = substr($contents, 0, strpos($contents, $search));
@@ -219,6 +223,7 @@ public function run(string $file, string $contents): void
219223

220224
$functionImports = [];
221225
foreach ($lines as $line) {
226+
AnalysisStatisticsContainer::analysedExpression();
222227
if (str_starts_with($line, 'use function ')) {
223228
$functionImports[] = rtrim(substr($line, 13), ';');
224229
}
@@ -228,8 +233,10 @@ public function run(string $file, string $contents): void
228233
foreach ($lines as $line) {
229234
// Find all function calls
230235
preg_match_all('/([a-zA-Z0-9_]+)\(/', $line, $matches);
236+
AnalysisStatisticsContainer::analysedExpressions(count($matches[1]));
231237

232238
foreach ($matches[1] as $match) {
239+
AnalysisStatisticsContainer::analysedExpression();
233240
if (! str_contains($line, '->')) {
234241
$calledFunctions[] = $match;
235242
}
@@ -241,6 +248,7 @@ public function run(string $file, string $contents): void
241248
$calledFunctions = array_unique($calledFunctions);
242249

243250
foreach ($calledFunctions as $calledFunction) {
251+
AnalysisStatisticsContainer::analysedExpression();
244252
if (! in_array($calledFunction, $functionImports)) {
245253
echo("Found unimported function '$calledFunction' in ".realpath(__DIR__.'/../../packages/framework/'.$file))."\n";
246254
}
@@ -252,13 +260,52 @@ class NoTestReferenceAnalyser extends LineAnalyser
252260
{
253261
public function run(string $file, int $lineNumber, string $line): void
254262
{
263+
AnalysisStatisticsContainer::analysedExpressions(1);
264+
255265
if (str_starts_with($line, ' * @see') && str_ends_with($line, 'Test')) {
266+
AnalysisStatisticsContainer::analysedExpressions(1);
256267
$this->fail(sprintf('Test class %s is referenced in %s:%s', trim(substr($line, 7)),
257268
realpath(__DIR__.'/../../packages/framework/'.$file) ?: $file, $lineNumber + 1));
258269
}
259270
}
260271
}
261272

273+
class AnalysisStatisticsContainer
274+
{
275+
private static int $linesCounted = 0;
276+
private static float $expressionsAnalysed = 0;
277+
278+
public static function countedLine(): void
279+
{
280+
self::$linesCounted++;
281+
}
282+
283+
public static function countedLines(int $count): void
284+
{
285+
self::$linesCounted += $count;
286+
}
287+
288+
public static function analysedExpression(): void
289+
{
290+
self::$expressionsAnalysed++;
291+
}
292+
293+
public static function analysedExpressions(float $countOrEstimate): void
294+
{
295+
self::$expressionsAnalysed += $countOrEstimate;
296+
}
297+
298+
public static function getLinesCounted(): int
299+
{
300+
return self::$linesCounted;
301+
}
302+
303+
public static function getExpressionsAnalysed(): int
304+
{
305+
return (int) round(self::$expressionsAnalysed);
306+
}
307+
}
308+
262309
interface FileAnalyserContract
263310
{
264311
public function __construct(string $file, string $contents);

0 commit comments

Comments
 (0)