Skip to content

Commit 0bfd4c8

Browse files
author
Florian Krämer
committed
Add TooManyArgumentsClass and related tests for argument validation
- Introduced TooManyArgumentsClass with a valid method and two new classes: TooManyArgsService and TooManyArgsOther, each containing methods with excessive arguments. - Updated TooManyArgumentsRuleTest to include tests for the new classes, ensuring proper error reporting for methods exceeding the allowed argument limit. - Enhanced ClassMustBeReadonlyRuleTest with a test for valid readonly classes to prevent false positives.
1 parent f1c2613 commit 0bfd4c8

10 files changed

+206
-17
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
readonly class ValidReadonlyController
8+
{
9+
// This class is already readonly, so no error should be reported
10+
}
11+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class MaxLineLengthMultipleLines // This line is exactly 95 characters long to test multiple violations in one file
6+
{
7+
public function methodWithMultipleVeryLongLinesThatExceedTheMaximumAllowedLengthOfEightyCharacters(): void
8+
{
9+
$variable = 'This is a short line';
10+
// All good here
11+
}
12+
}
13+

data/TooManyArgumentsClass.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,29 @@ public function methodWithTooManyArguments(int $arg1, int $arg2, int $arg3, int
88
{
99
// Method implementation
1010
}
11+
12+
public function validMethod(int $arg1, int $arg2): void
13+
{
14+
// Valid method with acceptable number of arguments
15+
}
16+
}
17+
18+
namespace App\Service;
19+
20+
class TooManyArgsService
21+
{
22+
public function methodWithTooManyArguments(int $a, int $b, int $c, int $d, int $e): void
23+
{
24+
// This should trigger error when pattern matches Service
25+
}
26+
}
27+
28+
namespace App\Other;
29+
30+
class TooManyArgsOther
31+
{
32+
public function methodWithTooManyArguments(int $a, int $b, int $c, int $d, int $e): void
33+
{
34+
// This should NOT trigger error when pattern doesn't match
35+
}
1136
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\PHPStanRules\Tests\TestCases\Architecture;
6+
7+
use Phauthentic\PHPStanRules\Architecture\ClassMustBeFinalRule;
8+
use PHPStan\Testing\RuleTestCase;
9+
10+
/**
11+
* @extends RuleTestCase<ClassMustBeFinalRule>
12+
*/
13+
class ClassMustBeFinalRuleWithAbstractNotIgnoredTest extends RuleTestCase
14+
{
15+
protected function getRule(): \PHPStan\Rules\Rule
16+
{
17+
// Do NOT ignore abstract classes
18+
return new ClassMustBeFinalRule(
19+
patterns: ['/Service$/'],
20+
ignoreAbstractClasses: false
21+
);
22+
}
23+
24+
public function testConcreteClassMustBeFinal(): void
25+
{
26+
// Test that concrete (non-abstract) classes are checked when ignoreAbstractClasses is false
27+
// This ensures the flag doesn't prevent checking of regular classes
28+
$this->analyse([__DIR__ . '/../../../data/Service/MissingFinalRuleService.php'], [
29+
[
30+
'Class App\Service\MissingFinalRuleService must be final.',
31+
5,
32+
],
33+
]);
34+
}
35+
}
36+

tests/TestCases/Architecture/ClassMustBeReadonlyRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public function testRule(): void
3434
// the test fails, if the expected error does not occur,
3535
// or if there are other errors reported beside the expected one
3636
}
37+
38+
public function testValidReadonlyClass(): void
39+
{
40+
// Test that readonly classes don't trigger errors
41+
$this->analyse([__DIR__ . '/../../../data/Controller/ValidReadonlyController.php'], []);
42+
}
3743
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\PHPStanRules\Tests\TestCases\CleanCode;
6+
7+
use Phauthentic\PHPStanRules\CleanCode\MaxLineLengthRule;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Testing\RuleTestCase;
10+
11+
/**
12+
* @extends RuleTestCase<MaxLineLengthRule>
13+
*/
14+
class MaxLineLengthRuleIgnoreUseTest extends RuleTestCase
15+
{
16+
protected function getRule(): Rule
17+
{
18+
// Ignore use statements (3rd parameter is true)
19+
return new MaxLineLengthRule(80, [], true);
20+
}
21+
22+
public function testUseStatementsAreIgnored(): void
23+
{
24+
// All use statements in this file are very long, but should be ignored
25+
$this->analyse([__DIR__ . '/../../../data/MaxLineLengthUseStatementsClass.php'], []);
26+
}
27+
}
28+

tests/TestCases/CleanCode/MaxLineLengthRuleTest.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,18 @@ public function testRule(): void
2828
]);
2929
}
3030

31-
public function testRuleWithExcludePatterns(): void
31+
public function testMultipleLongLinesInFile(): void
3232
{
33-
$rule = new MaxLineLengthRule(80, ['/.*Excluded.*/']);
34-
35-
$this->analyse([__DIR__ . '/../../../data/MaxLineLengthExcludedClass.php'], [
33+
// Test that multiple long lines in the same file are all detected
34+
$this->analyse([__DIR__ . '/../../../data/MaxLineLengthMultipleLines.php'], [
3635
[
37-
'Line 7 exceeds the maximum length of 80 characters (found 81 characters).',
38-
7,
36+
'Line 5 exceeds the maximum length of 80 characters (found 115 characters).',
37+
5,
3938
],
4039
[
41-
'Line 9 exceeds the maximum length of 80 characters (found 86 characters).',
42-
9,
40+
'Line 7 exceeds the maximum length of 80 characters (found 110 characters).',
41+
7,
4342
],
4443
]);
4544
}
46-
47-
48-
49-
public function testRuleWithIgnoreUseStatements(): void
50-
{
51-
$rule = new MaxLineLengthRule(80, [], true);
52-
53-
$this->analyse([__DIR__ . '/../../../data/MaxLineLengthUseStatementsClass.php'], []);
54-
}
5545
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\PHPStanRules\Tests\TestCases\CleanCode;
6+
7+
use Phauthentic\PHPStanRules\CleanCode\MaxLineLengthRule;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Testing\RuleTestCase;
10+
11+
/**
12+
* @extends RuleTestCase<MaxLineLengthRule>
13+
*/
14+
class MaxLineLengthRuleWithExclusionTest extends RuleTestCase
15+
{
16+
protected function getRule(): Rule
17+
{
18+
// Exclude files matching "Excluded" in their path
19+
return new MaxLineLengthRule(80, ['/.*Excluded.*/']);
20+
}
21+
22+
public function testExcludedFileIsNotChecked(): void
23+
{
24+
// This file should be excluded, so no errors should be reported
25+
$this->analyse([__DIR__ . '/../../../data/MaxLineLengthExcludedClass.php'], []);
26+
}
27+
28+
public function testNonExcludedFileIsChecked(): void
29+
{
30+
// This file is NOT excluded, so errors should be reported
31+
$this->analyse([__DIR__ . '/../../../data/MaxLineLengthTestClass.php'], [
32+
[
33+
'Line 7 exceeds the maximum length of 80 characters (found 84 characters).',
34+
7,
35+
],
36+
]);
37+
}
38+
}
39+

tests/TestCases/CleanCode/TooManyArgumentsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function testRule(): void
2525
'Method App\TooManyArgumentsClass::methodWithTooManyArguments has too many arguments (4). Maximum allowed is 3.',
2626
7,
2727
],
28+
[
29+
'Method App\Service\TooManyArgsService::methodWithTooManyArguments has too many arguments (5). Maximum allowed is 3.',
30+
22,
31+
],
32+
[
33+
'Method App\Other\TooManyArgsOther::methodWithTooManyArguments has too many arguments (5). Maximum allowed is 3.',
34+
32,
35+
],
2836
]);
2937
}
3038
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\PHPStanRules\Tests\TestCases\CleanCode;
6+
7+
use Phauthentic\PHPStanRules\CleanCode\TooManyArgumentsRule;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Testing\RuleTestCase;
10+
11+
/**
12+
* @extends RuleTestCase<TooManyArgumentsRule>
13+
*/
14+
class TooManyArgumentsRuleWithPatternsTest extends RuleTestCase
15+
{
16+
protected function getRule(): Rule
17+
{
18+
// Only apply to classes ending with "Service"
19+
return new TooManyArgumentsRule(3, ['/Service$/']);
20+
}
21+
22+
public function testRuleWithPatterns(): void
23+
{
24+
$this->analyse([__DIR__ . '/../../../data/TooManyArgumentsClass.php'], [
25+
[
26+
'Method App\Service\TooManyArgsService::methodWithTooManyArguments has too many arguments (5). Maximum allowed is 3.',
27+
22,
28+
],
29+
// TooManyArgumentsClass and TooManyArgsOther should NOT trigger errors because patterns don't match
30+
]);
31+
}
32+
}
33+

0 commit comments

Comments
 (0)