Skip to content

Commit

Permalink
Merge pull request #1750 from hydephp/update-hydestan
Browse files Browse the repository at this point in the history
Internal: Add statistics output for analysed expressions
  • Loading branch information
caendesilva authored Jun 28, 2024
2 parents 6c8712a + fa9aaf1 commit 445eeb9
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions monorepo/HydeStan/HydeStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -124,13 +125,15 @@ 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 = [
new NoTestReferenceAnalyser($file, $lineNumber, $line),
];

foreach ($lineAnalysers as $analyser) {
AnalysisStatisticsContainer::countedLine();
$analyser->run($file, $lineNumber, $line);
$this->aggregateLines++;
}
Expand Down Expand Up @@ -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));
Expand All @@ -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), ';');
}
Expand All @@ -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;
}
Expand All @@ -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";
}
Expand All @@ -252,13 +260,52 @@ class NoTestReferenceAnalyser extends LineAnalyser
{
public function run(string $file, int $lineNumber, string $line): void
{
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));
}
}
}

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);
Expand Down

0 comments on commit 445eeb9

Please sign in to comment.