/
AbstractDataStructureLengthSniff.php
48 lines (41 loc) · 1.29 KB
/
AbstractDataStructureLengthSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace ObjectCalisthenics;
use ObjectCalisthenics\Helper\Structure\StructureMetrics;
use PHP_CodeSniffer_File;
/**
* Data structure length code sniffer.
*
* This sniff is the base for class, interface, trait, function and method
* length checks as part of "Keep your classes small" object calisthenics
* rule.
*/
abstract class AbstractDataStructureLengthSniff
{
/**
* Maximum data structure length.
*
* @var int
*/
protected $maxLength = 200;
/**
* @param PHP_CodeSniffer_File $phpcsFile
* @param int $stackPtr
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$length = StructureMetrics::getStructureLengthInLines($phpcsFile, $stackPtr);
if ($length > $this->maxLength) {
$tokenType = strtolower(substr($token['type'], 2));
$error = sprintf(
'Keep your %s small (currently using %d lines, must be less or equals than %d lines)',
$tokenType,
$length,
$this->maxLength
);
$phpcsFile->addError($error, $stackPtr, sprintf('%sTooBig', ucfirst($tokenType)));
}
}
}