Skip to content

Commit 3e6b61a

Browse files
bkdotcomkukulich
authored andcommitted
Adds SlevomatCodingStandard.Classes.ClassLength sniff
1 parent fd5336f commit 3e6b61a

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ Sniff provides the following settings:
494494
* `spacesCountBeforeColon`: the number of spaces before `:`.
495495
* `spacesCountBeforeType`: the number of spaces before type.
496496

497+
#### SlevomatCodingStandard.Classes.ClassLength
498+
499+
Disallows long classes. This sniff provides the following settings:
500+
501+
* `includeComments`: should comments be included in the count (default value is false).
502+
* `includeWhitespace`: shoud empty lines be included in the count (default value is false).
503+
* `maxLinesLength`: specifies max allowed function lines length (default value is 250).
504+
497505
#### SlevomatCodingStandard.Classes.ClassMemberSpacing 🔧
498506

499507
Checks lines count between different class members, eg. between last property and first method.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Classes;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
8+
use SlevomatCodingStandard\Helpers\FunctionHelper;
9+
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
10+
use function array_filter;
11+
use function array_keys;
12+
use function array_reduce;
13+
use function array_values;
14+
use function sprintf;
15+
16+
class ClassLengthSniff implements Sniff
17+
{
18+
19+
public const CODE_CLASS_TOO_LONG = 'ClassTooLong';
20+
21+
/** @var int */
22+
public $maxLinesLength = 250;
23+
24+
/** @var bool */
25+
public $includeComments = false;
26+
27+
/** @var bool */
28+
public $includeWhitespace = false;
29+
30+
/**
31+
* @return array<int, (int|string)>
32+
*/
33+
public function register(): array
34+
{
35+
return array_values(Tokens::$ooScopeTokens);
36+
}
37+
38+
/**
39+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
40+
* @param int $pointer
41+
*/
42+
public function process(File $phpcsFile, $pointer): void
43+
{
44+
$this->maxLinesLength = SniffSettingsHelper::normalizeInteger($this->maxLinesLength);
45+
$flags = array_keys(array_filter([
46+
FunctionHelper::LINE_INCLUDE_COMMENT => $this->includeComments,
47+
FunctionHelper::LINE_INCLUDE_WHITESPACE => $this->includeWhitespace,
48+
]));
49+
$flags = array_reduce($flags, static function ($carry, $flag): int {
50+
return $carry | $flag;
51+
}, 0);
52+
53+
$length = FunctionHelper::getLineCount($phpcsFile, $pointer, $flags);
54+
55+
if ($length <= $this->maxLinesLength) {
56+
return;
57+
}
58+
59+
$errorMessage = sprintf('Your class is too long. Currently using %d lines. Can be up to %d lines.', $length, $this->maxLinesLength);
60+
61+
$phpcsFile->addError($errorMessage, $pointer, self::CODE_CLASS_TOO_LONG);
62+
}
63+
64+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Classes;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class ClassLengthSniffTest extends TestCase
8+
{
9+
10+
public function testNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/classLength.php');
13+
self::assertNoSniffErrorInFile($report);
14+
}
15+
16+
public function testErrors(): void
17+
{
18+
$report = self::checkFile(__DIR__ . '/data/classLength.php', [
19+
'maxLinesLength' => 5,
20+
'includeComments' => true,
21+
]);
22+
23+
self::assertSame(1, $report->getErrorCount());
24+
25+
self::assertSniffError(
26+
$report,
27+
5,
28+
ClassLengthSniff::CODE_CLASS_TOO_LONG,
29+
'Your class is too long. Currently using 13 lines. Can be up to 5 lines.'
30+
);
31+
}
32+
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Foo\Bar;
4+
5+
class ClassLineCountSniffTest
6+
{
7+
public function __construct()
8+
{
9+
}
10+
11+
public function __get($name)
12+
{
13+
}
14+
15+
/**
16+
*
17+
*/
18+
public function someMethod() : string
19+
{
20+
return 'foo bar';
21+
}
22+
}

0 commit comments

Comments
 (0)