|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Codor\Sniffs\Files; |
|
4
|
|
|
|
|
5
|
|
|
use PHP_CodeSniffer_Sniff; |
|
6
|
|
|
use PHP_CodeSniffer_File; |
|
7
|
|
|
|
|
8
|
|
|
class IndentationLevelSniff implements PHP_CodeSniffer_Sniff |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Indentations cannot be >= to this number. |
|
12
|
|
|
* @var integer |
|
13
|
|
|
*/ |
|
14
|
|
|
public $indentationLimit = 2; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The highest indentation level found. |
|
18
|
|
|
* @var integer |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $maxIndentationFound; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns the token types that this sniff is interested in. |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function register() |
|
27
|
|
|
{ |
|
28
|
|
|
return [T_FUNCTION, T_CLOSURE, T_SWITCH]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Processes the tokens that this sniff is interested in. |
|
33
|
|
|
* |
|
34
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
35
|
|
|
* @param integer $stackPtr The position in the stack where |
|
36
|
|
|
* the token was found. |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
40
|
|
|
{ |
|
41
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
42
|
|
|
$token = $tokens[$stackPtr]; |
|
43
|
|
|
$this->maxIndentationFound = 0; |
|
44
|
|
|
|
|
45
|
|
|
// Ignore functions with no body |
|
46
|
|
|
if (isset($token['scope_opener']) === false) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->inspectScope($token, $tokens); |
|
51
|
|
|
|
|
52
|
|
|
if ($this->maxIndentationFound <= $this->indentationLimit) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$phpcsFile->addError($this->getErrorMessage(), $stackPtr); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Inspect the tokens in the scope of the provided $token. |
|
61
|
|
|
* @codingStandardsIgnoreStart |
|
62
|
|
|
* @param array $token Token Data. |
|
63
|
|
|
* @param array $tokens Tokens. |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function inspectScope(array $token, array $tokens) |
|
67
|
|
|
{ |
|
68
|
|
|
$start = $token['scope_opener']; |
|
69
|
|
|
$end = $token['scope_closer']; |
|
70
|
|
|
$this->relativeScopeLevel = $tokens[$start]['level']; |
|
|
|
|
|
|
71
|
|
|
for ($index=$start; $index <= $end; $index++) { |
|
72
|
|
|
$nestedToken = $tokens[$index]; |
|
73
|
|
|
if ($nestedToken['type'] === "T_SWITCH") { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
$this->adjustMaxIndentationFound($nestedToken); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
// @codingStandardsIgnoreEnd |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Adjust the maximum indentation level found value. |
|
83
|
|
|
* @param array $nestedToken Token data. |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function adjustMaxIndentationFound(array $nestedToken) |
|
87
|
|
|
{ |
|
88
|
|
|
$tokenNestedLevel = $nestedToken['level']; |
|
89
|
|
|
$nestedLevel = $tokenNestedLevel - $this->relativeScopeLevel; |
|
90
|
|
|
$nestedLevel > $this->maxIndentationFound ? $this->maxIndentationFound = $nestedLevel : null; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Produce the error message. |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getErrorMessage() |
|
98
|
|
|
{ |
|
99
|
|
|
// Hack to fix the output numbers for the |
|
100
|
|
|
// indentation levels found and the |
|
101
|
|
|
// indentation limit. |
|
102
|
|
|
$indentationFound = $this->maxIndentationFound - 1; |
|
103
|
|
|
$indentationLimit = $this->indentationLimit - 1; |
|
104
|
|
|
return "{$indentationFound} indenation levels found. " . |
|
105
|
|
|
"Maximum of {$indentationLimit} indenation levels allowed."; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: