forked from object-calisthenics/phpcs-calisthenics-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStructureLengthSniff.php
More file actions
99 lines (84 loc) · 3.11 KB
/
DataStructureLengthSniff.php
File metadata and controls
99 lines (84 loc) · 3.11 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* 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.
*
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
*/
abstract class ObjectCalisthenics_DataStructureLengthSniff implements PHP_CodeSniffer_Sniff
{
/**
* Supported list of tokenizers supported by this sniff.
*
* @var array
*/
public $supportedTokenizers = array('PHP');
/**
* Maximum data structure length for warning,
*
* @var integer
*/
public $maxLength = 200;
/**
* Absolute maximum data structure length for error.
*
* @var integer
*/
public $absoluteMaxLength = 200;
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return integer[]
*/
abstract public function register();
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token
* in the stack passed in $tokens.
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$tokenType = strtolower(substr($token['type'], 2));
$length = $this->getStructureLength($phpcsFile, $stackPtr);
switch (true) {
case ($length > $this->absoluteMaxLength):
$message = 'Keep your %s small (currently using %d lines, must be less or equals than %d lines)';
$error = sprintf($message, $tokenType, $length, $this->absoluteMaxLength);
$phpcsFile->addError($error, $stackPtr, sprintf('%sTooBig', ucfirst($tokenType)));
break;
case ($length > $this->maxLength):
$message = 'Your %s is too big, consider refactoring (currently using %d lines, should be less or equals than %d lines)';
$warning = sprintf($message, $tokenType, $length, $this->maxLength);
$phpcsFile->addWarning($warning, $stackPtr, sprintf('%sTooBig', ucfirst($tokenType)));
break;
}
}
/**
* Retrieve the data structure LOC.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return integer
*/
private function getStructureLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip function without body.
if (isset($token['scope_opener']) === false) {
return 0;
}
$firstToken = $tokens[$token['scope_opener']];
$lastToken = $tokens[$token['scope_closer']];
return $lastToken['line'] - $firstToken['line'];
}
}