File tree Expand file tree Collapse file tree
data/MaxLinePerMethodRule Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,5 +4,6 @@ parameters:
44 - src
55rules :
66 - MartinSoenen\PHPStanRules\Rules\MaxLinePerClassRule
7+ - MartinSoenen\PHPStanRules\Rules\MaxLinePerMethodRule
78 - MartinSoenen\PHPStanRules\Rules\NoLaravelObserverRule
89 - MartinSoenen\PHPStanRules\Rules\NoTryCatchRule
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace MartinSoenen \PHPStanRules \Rules ;
4+
5+ use PhpParser \Node ;
6+ use PHPStan \Analyser \Scope ;
7+ use PHPStan \Rules \Rule ;
8+ use PHPStan \Rules \RuleErrorBuilder ;
9+
10+ class MaxLinePerMethodRule implements Rule
11+ {
12+ private const int MAX_LINES = 20 ;
13+
14+ public function getNodeType (): string
15+ {
16+ return Node \Stmt \ClassMethod::class;
17+ }
18+
19+ public function processNode (Node $ node , Scope $ scope ): array
20+ {
21+ $ startLine = $ node ->getStartLine () + 1 ;
22+ $ endLine = $ node ->getEndLine () - 1 ;
23+ $ lineCount = $ endLine - $ startLine ;
24+
25+ if ($ lineCount > self ::MAX_LINES ) {
26+ return [
27+ RuleErrorBuilder::message (
28+ sprintf (
29+ 'The %s function has more than %d code lines. Please reduce it. ' ,
30+ $ node ->name ->toString (),
31+ self ::MAX_LINES
32+ )
33+ )
34+ ->line ($ node ->getLine ())
35+ ->build (),
36+ ];
37+ }
38+
39+ return [];
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace MartinSoenen \PHPStanRules \Tests \Rules ;
4+
5+ use MartinSoenen \PHPStanRules \Rules \MaxLinePerMethodRule ;
6+ use PHPStan \Testing \RuleTestCase ;
7+
8+ /**
9+ * @extends RuleTestCase<MaxLinePerMethodRule>
10+ */
11+ class MaxLinePerMethodRuleTest extends RuleTestCase
12+ {
13+ protected function getRule (): \PHPStan \Rules \Rule
14+ {
15+ // getRule() method needs to return an instance of the tested rule
16+ return new MaxLinePerMethodRule ();
17+ }
18+
19+ public function testWithMultipleLines (): void
20+ {
21+ // first argument: path to the example file that contains some errors that should be reported by MyRule
22+ // second argument: an array of expected errors,
23+ // each error consists of the asserted error message, and the asserted error file line
24+ $ this ->analyse ([__DIR__ . '/data/MaxLinePerMethodRule/MultipleLinesMethods.php ' ], [
25+ [
26+ "The twentyOneLines function has more than 20 code lines. Please reduce it. " , // asserted error message
27+ 53 , // asserted error line
28+ ],
29+ ]);
30+ }
31+
32+ public function testWithOneLine (): void
33+ {
34+ $ this ->analyse ([__DIR__ . '/data/MaxLinePerMethodRule/OneLineMethods.php ' ], []);
35+ }
36+
37+ public function testWithNoMethods (): void
38+ {
39+ $ this ->analyse ([__DIR__ . '/data/MaxLinePerMethodRule/NoMethods.php ' ], []);
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace MartinSoenen \PHPStanRules \Tests \Rules \data \MaxLinePerMethodRule ;
4+
5+ class MultipleLinesMethods {
6+ public function nineteenLines (): void
7+ {
8+ echo 1 ;
9+ echo 2 ;
10+ echo 3 ;
11+ echo 4 ;
12+ echo 5 ;
13+ echo 6 ;
14+ echo 7 ;
15+ echo 8 ;
16+ echo 9 ;
17+ echo 10 ;
18+ echo 11 ;
19+ echo 12 ;
20+ echo 13 ;
21+ echo 14 ;
22+ echo 15 ;
23+ echo 16 ;
24+ echo 17 ;
25+ echo 18 ;
26+ echo 19 ;
27+ }
28+
29+ public function twentyLines (): void
30+ {
31+ echo 1 ;
32+ echo 2 ;
33+ echo 3 ;
34+ echo 4 ;
35+ echo 5 ;
36+ echo 6 ;
37+ echo 7 ;
38+ echo 8 ;
39+ echo 9 ;
40+ echo 10 ;
41+ echo 11 ;
42+ echo 12 ;
43+ echo 13 ;
44+ echo 14 ;
45+ echo 15 ;
46+ echo 16 ;
47+ echo 17 ;
48+ echo 18 ;
49+ echo 19 ;
50+ echo 20 ;
51+ }
52+
53+ public function twentyOneLines (): void
54+ {
55+ echo 1 ;
56+ echo 2 ;
57+ echo 3 ;
58+ echo 4 ;
59+ echo 5 ;
60+ echo 6 ;
61+ echo 7 ;
62+ echo 8 ;
63+ echo 9 ;
64+ echo 10 ;
65+ echo 11 ;
66+ echo 12 ;
67+ echo 13 ;
68+ echo 14 ;
69+ echo 15 ;
70+ echo 16 ;
71+ echo 17 ;
72+ echo 18 ;
73+ echo 19 ;
74+ echo 20 ;
75+ echo 21 ;
76+ }
77+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace MartinSoenen \PHPStanRules \Tests \Rules \data \MaxLinePerMethodRule ;
4+
5+ class NoMethods {
6+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace MartinSoenen \PHPStanRules \Tests \Rules \data \MaxLinePerMethodRule ;
4+
5+ class OneLineMethods {
6+ public function oneLine (): void
7+ {
8+ echo 1 ;
9+ }
10+
11+ public function anotherOneLine (): void
12+ {
13+ echo 1 ;
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments