diff --git a/data/MaxLineLengthLongUseStatementsClass.php b/data/MaxLineLengthLongUseStatementsClass.php new file mode 100644 index 0000000..1bd3644 --- /dev/null +++ b/data/MaxLineLengthLongUseStatementsClass.php @@ -0,0 +1,21 @@ +> + * Cache of which lines contain use statements per file + */ + private array $useStatementLines = []; + /** * @param string[] $excludePatterns */ @@ -76,14 +82,24 @@ public function processNode(Node $node, Scope $scope): array return []; } - // Skip use statements if configured to ignore them - if ($this->ignoreUseStatements && $node instanceof Use_) { - return []; - } - $filePath = $scope->getFile(); $lineNumber = $node->getLine(); + // Track use statement lines for this file + if ($node instanceof Use_) { + $this->markLineAsUseStatement($filePath, $lineNumber); + + // If ignoring use statements, skip processing this node + if ($this->ignoreUseStatements) { + return []; + } + } + + // Skip if this line is a use statement and we're ignoring them + if ($this->ignoreUseStatements && $this->isUseStatementLine($filePath, $lineNumber)) { + return []; + } + // Skip if we've already processed this line if ($this->isLineProcessed($filePath, $lineNumber)) { return []; @@ -137,6 +153,20 @@ private function markLineAsProcessed(string $filePath, int $lineNumber): void $this->processedLines[$filePath][$lineNumber] = true; } + private function isUseStatementLine(string $filePath, int $lineNumber): bool + { + return isset($this->useStatementLines[$filePath][$lineNumber]); + } + + private function markLineAsUseStatement(string $filePath, int $lineNumber): void + { + if (!isset($this->useStatementLines[$filePath])) { + $this->useStatementLines[$filePath] = []; + } + + $this->useStatementLines[$filePath][$lineNumber] = true; + } + private function getLineLength(string $filePath, int $lineNumber): int { // Cache file line lengths to avoid reading the same file multiple times diff --git a/tests/TestCases/CleanCode/MaxLineLengthRuleDetectUseLongLinesTest.php b/tests/TestCases/CleanCode/MaxLineLengthRuleDetectUseLongLinesTest.php new file mode 100644 index 0000000..cf8d129 --- /dev/null +++ b/tests/TestCases/CleanCode/MaxLineLengthRuleDetectUseLongLinesTest.php @@ -0,0 +1,59 @@ + + */ +class MaxLineLengthRuleDetectUseLongLinesTest extends RuleTestCase +{ + protected function getRule(): Rule + { + // Do NOT ignore use statements (3rd parameter is false/default) + return new MaxLineLengthRule(80, [], false); + } + + /** + * Test that long use statements ARE detected when ignoreUseStatements is false + */ + public function testLongUseStatementsAreDetectedWhenNotIgnored(): void + { + // Lines 5, 6, 7 have use statements that exceed 80 characters - should be detected + // Line 16 has a method signature that exceeds 80 characters - should be detected + // Line 18 has a variable assignment that exceeds 80 characters - should be detected + $this->analyse([__DIR__ . '/../../../data/MaxLineLengthLongUseStatementsClass.php'], [ + [ + 'Line 5 exceeds the maximum length of 80 characters (found 93 characters).', + 5, + ], + [ + 'Line 6 exceeds the maximum length of 80 characters (found 93 characters).', + 6, + ], + [ + 'Line 7 exceeds the maximum length of 80 characters (found 94 characters).', + 7, + ], + [ + 'Line 16 exceeds the maximum length of 80 characters (found 117 characters).', + 16, + ], + [ + 'Line 18 exceeds the maximum length of 80 characters (found 114 characters).', + 18, + ], + ]); + } +} + diff --git a/tests/TestCases/CleanCode/MaxLineLengthRuleIgnoreUseLongLinesTest.php b/tests/TestCases/CleanCode/MaxLineLengthRuleIgnoreUseLongLinesTest.php new file mode 100644 index 0000000..82475f1 --- /dev/null +++ b/tests/TestCases/CleanCode/MaxLineLengthRuleIgnoreUseLongLinesTest.php @@ -0,0 +1,51 @@ + + */ +class MaxLineLengthRuleIgnoreUseLongLinesTest extends RuleTestCase +{ + protected function getRule(): Rule + { + // Ignore use statements (3rd parameter is true) + return new MaxLineLengthRule(80, [], true); + } + + /** + * Test that long use statements are ignored when ignoreUseStatements is true, + * but other long lines are still detected. + * + * This is a regression test for a bug where use statements were not properly + * ignored because the check only looked at the node type, not the line itself. + */ + public function testLongUseStatementsAreIgnoredButOtherLongLinesAreDetected(): void + { + // Lines 5, 6, 7 have use statements that exceed 80 characters - should be ignored + // Line 16 has a method signature that exceeds 80 characters - should be detected + // Line 18 has a variable assignment that exceeds 80 characters - should be detected + $this->analyse([__DIR__ . '/../../../data/MaxLineLengthLongUseStatementsClass.php'], [ + [ + 'Line 16 exceeds the maximum length of 80 characters (found 117 characters).', + 16, + ], + [ + 'Line 18 exceeds the maximum length of 80 characters (found 114 characters).', + 18, + ], + ]); + } +} +