Skip to content

Commit

Permalink
Add rule against use against useless methods in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Sep 8, 2024
1 parent 330f089 commit cc56a26
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions monorepo/HydeStan/HydeStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class HydeStan
private const TEST_FILE_ANALYSERS = [
NoFixMeAnalyser::class,
NoUsingAssertEqualsForScalarTypesTestAnalyser::class,
NoSetUpTearDownInUnitTestCaseAnalyser::class,
];

private const LINE_ANALYSERS = [
Expand Down Expand Up @@ -382,3 +383,24 @@ public function run(string $file, int $lineNumber, string $line): void
}
}
}

class NoSetUpTearDownInUnitTestCaseAnalyser extends FileAnalyser
{
public function run(string $file, string $contents): void
{
if (!str_contains($contents, 'extends UnitTestCase')) {
return;
}

$methods = ['setUp', 'tearDown'];

foreach ($methods as $method) {
AnalysisStatisticsContainer::analysedExpression();
if (preg_match("/function\s+$method\s*\(/", $contents)) {
$lineNumber = substr_count(substr($contents, 0, strpos($contents, "function $method")), "\n") + 1;
$this->fail(sprintf("Found %s method in UnitTestCase at %s", $method, fileLink($file, $lineNumber)));
HydeStan::addActionsMessage('error', $file, $lineNumber, "HydeStan: Unnecessary{$method}MethodError", "{$method} method in UnitTestCase performs no operation and should be removed.");
}
}
}
}

0 comments on commit cc56a26

Please sign in to comment.