From 8fa7f773025873d9946012e0df080830f126bb71 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 20:17:25 +0200 Subject: [PATCH 1/2] Add statistics for the analysed expressions --- monorepo/HydeStan/HydeStan.php | 50 ++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index de5ba54eb53..10abaf96f9f 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -37,10 +37,11 @@ public function __construct(private readonly bool $debug = false) public function __destruct() { $this->console->newline(); - $this->console->info(sprintf('HydeStan has exited after scanning %s total (and %s aggregate) lines in %s files.', + $this->console->info(sprintf('HydeStan has exited after scanning %s total (and %s aggregate) lines in %s files. Total expressions analysed: %s', number_format($this->scannedLines), number_format($this->aggregateLines), - number_format(count($this->files)) + number_format(count($this->files)), + number_format(AnalysisStatisticsContainer::getExpressionsAnalysed()), )); if (count(self::$warnings) > 0) { @@ -124,6 +125,7 @@ private function analyseFile(string $file, string $contents): void } $analyser->run($file, $contents); + AnalysisStatisticsContainer::countedLines(substr_count($contents, "\n")); foreach (explode("\n", $contents) as $lineNumber => $line) { $lineAnalysers = [ @@ -131,6 +133,7 @@ private function analyseFile(string $file, string $contents): void ]; foreach ($lineAnalysers as $analyser) { + AnalysisStatisticsContainer::countedLine(); $analyser->run($file, $lineNumber, $line); $this->aggregateLines++; } @@ -196,6 +199,7 @@ public function run(string $file, string $contents): void $contents = strtolower($contents); foreach ($searches as $search) { + AnalysisStatisticsContainer::analysedExpression(); if (str_contains($contents, $search)) { // Get line number of marker by counting new \n tags before it $stringBeforeMarker = substr($contents, 0, strpos($contents, $search)); @@ -219,6 +223,7 @@ public function run(string $file, string $contents): void $functionImports = []; foreach ($lines as $line) { + AnalysisStatisticsContainer::analysedExpression(); if (str_starts_with($line, 'use function ')) { $functionImports[] = rtrim(substr($line, 13), ';'); } @@ -228,8 +233,10 @@ public function run(string $file, string $contents): void foreach ($lines as $line) { // Find all function calls preg_match_all('/([a-zA-Z0-9_]+)\(/', $line, $matches); + AnalysisStatisticsContainer::analysedExpressions(count($matches[1])); foreach ($matches[1] as $match) { + AnalysisStatisticsContainer::analysedExpression(); if (! str_contains($line, '->')) { $calledFunctions[] = $match; } @@ -241,6 +248,7 @@ public function run(string $file, string $contents): void $calledFunctions = array_unique($calledFunctions); foreach ($calledFunctions as $calledFunction) { + AnalysisStatisticsContainer::analysedExpression(); if (! in_array($calledFunction, $functionImports)) { echo("Found unimported function '$calledFunction' in ".realpath(__DIR__.'/../../packages/framework/'.$file))."\n"; } @@ -252,6 +260,8 @@ class NoTestReferenceAnalyser extends LineAnalyser { public function run(string $file, int $lineNumber, string $line): void { + AnalysisStatisticsContainer::analysedExpressions(1.5); + if (str_starts_with($line, ' * @see') && str_ends_with($line, 'Test')) { $this->fail(sprintf('Test class %s is referenced in %s:%s', trim(substr($line, 7)), realpath(__DIR__.'/../../packages/framework/'.$file) ?: $file, $lineNumber + 1)); @@ -259,6 +269,42 @@ public function run(string $file, int $lineNumber, string $line): void } } +class AnalysisStatisticsContainer +{ + private static int $linesCounted = 0; + private static float $expressionsAnalysed = 0; + + public static function countedLine(): void + { + self::$linesCounted++; + } + + public static function countedLines(int $count): void + { + self::$linesCounted += $count; + } + + public static function analysedExpression(): void + { + self::$expressionsAnalysed++; + } + + public static function analysedExpressions(float $countOrEstimate): void + { + self::$expressionsAnalysed += $countOrEstimate; + } + + public static function getLinesCounted(): int + { + return self::$linesCounted; + } + + public static function getExpressionsAnalysed(): int + { + return (int) round(self::$expressionsAnalysed); + } +} + interface FileAnalyserContract { public function __construct(string $file, string $contents); From 3a2d7d117decf2db9776ce3daa57e9ff0aeee7bd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 28 Jun 2024 16:02:05 +0200 Subject: [PATCH 2/2] More accurate update instead of estimate --- monorepo/HydeStan/HydeStan.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index 10abaf96f9f..4dd8c0731e5 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -260,9 +260,10 @@ class NoTestReferenceAnalyser extends LineAnalyser { public function run(string $file, int $lineNumber, string $line): void { - AnalysisStatisticsContainer::analysedExpressions(1.5); + AnalysisStatisticsContainer::analysedExpressions(1); if (str_starts_with($line, ' * @see') && str_ends_with($line, 'Test')) { + AnalysisStatisticsContainer::analysedExpressions(1); $this->fail(sprintf('Test class %s is referenced in %s:%s', trim(substr($line, 7)), realpath(__DIR__.'/../../packages/framework/'.$file) ?: $file, $lineNumber + 1)); }