-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds SlevomatCodingStandard.Classes.ClassLength sniff
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
SlevomatCodingStandard/Sniffs/Classes/ClassLengthSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
use SlevomatCodingStandard\Helpers\FunctionHelper; | ||
use SlevomatCodingStandard\Helpers\SniffSettingsHelper; | ||
use function array_filter; | ||
use function array_keys; | ||
use function array_reduce; | ||
use function array_values; | ||
use function sprintf; | ||
|
||
class ClassLengthSniff implements Sniff | ||
{ | ||
|
||
public const CODE_CLASS_TOO_LONG = 'ClassTooLong'; | ||
|
||
/** @var int */ | ||
public $maxLinesLength = 250; | ||
|
||
/** @var bool */ | ||
public $includeComments = false; | ||
|
||
/** @var bool */ | ||
public $includeWhitespace = false; | ||
|
||
/** | ||
* @return array<int, (int|string)> | ||
*/ | ||
public function register(): array | ||
{ | ||
return array_values(Tokens::$ooScopeTokens); | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @param int $pointer | ||
*/ | ||
public function process(File $phpcsFile, $pointer): void | ||
{ | ||
$this->maxLinesLength = SniffSettingsHelper::normalizeInteger($this->maxLinesLength); | ||
$flags = array_keys(array_filter([ | ||
FunctionHelper::LINE_INCLUDE_COMMENT => $this->includeComments, | ||
FunctionHelper::LINE_INCLUDE_WHITESPACE => $this->includeWhitespace, | ||
])); | ||
$flags = array_reduce($flags, static function ($carry, $flag): int { | ||
return $carry | $flag; | ||
}, 0); | ||
|
||
$length = FunctionHelper::getLineCount($phpcsFile, $pointer, $flags); | ||
|
||
if ($length <= $this->maxLinesLength) { | ||
return; | ||
} | ||
|
||
$errorMessage = sprintf('Your class is too long. Currently using %d lines. Can be up to %d lines.', $length, $this->maxLinesLength); | ||
|
||
$phpcsFile->addError($errorMessage, $pointer, self::CODE_CLASS_TOO_LONG); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use SlevomatCodingStandard\Sniffs\TestCase; | ||
|
||
class ClassLengthSniffTest extends TestCase | ||
{ | ||
|
||
public function testNoErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/classLength.php'); | ||
self::assertNoSniffErrorInFile($report); | ||
} | ||
|
||
public function testErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/classLength.php', [ | ||
'maxLinesLength' => 5, | ||
'includeComments' => true, | ||
]); | ||
|
||
self::assertSame(1, $report->getErrorCount()); | ||
|
||
self::assertSniffError( | ||
$report, | ||
5, | ||
ClassLengthSniff::CODE_CLASS_TOO_LONG, | ||
'Your class is too long. Currently using 13 lines. Can be up to 5 lines.' | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Foo\Bar; | ||
|
||
class ClassLineCountSniffTest | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function someMethod() : string | ||
{ | ||
return 'foo bar'; | ||
} | ||
} |