-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
667 additions
and
11 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
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
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
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,107 @@ | ||
<?php | ||
|
||
namespace Codor\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer_Sniff; | ||
use PHP_CodeSniffer_File; | ||
|
||
class IndentationLevelSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
/** | ||
* Indentations cannot be >= to this number. | ||
* @var integer | ||
*/ | ||
public $indentationLimit = 2; | ||
|
||
/** | ||
* The highest indentation level found. | ||
* @var integer | ||
*/ | ||
protected $maxIndentationFound; | ||
|
||
/** | ||
* Returns the token types that this sniff is interested in. | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_FUNCTION, T_CLOSURE, T_SWITCH]; | ||
} | ||
|
||
/** | ||
* Processes the tokens that this sniff is interested in. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. | ||
* @param integer $stackPtr The position in the stack where | ||
* the token was found. | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$token = $tokens[$stackPtr]; | ||
$this->maxIndentationFound = 0; | ||
|
||
// Ignore functions with no body | ||
if (isset($token['scope_opener']) === false) { | ||
return; | ||
} | ||
|
||
$this->inspectScope($token, $tokens); | ||
|
||
if ($this->maxIndentationFound <= $this->indentationLimit) { | ||
return; | ||
} | ||
|
||
$phpcsFile->addError($this->getErrorMessage(), $stackPtr); | ||
} | ||
|
||
/** | ||
* Inspect the tokens in the scope of the provided $token. | ||
* @codingStandardsIgnoreStart | ||
* @param array $token Token Data. | ||
* @param array $tokens Tokens. | ||
* @return void | ||
*/ | ||
protected function inspectScope(array $token, array $tokens) | ||
{ | ||
$start = $token['scope_opener']; | ||
$end = $token['scope_closer']; | ||
$this->relativeScopeLevel = $tokens[$start]['level']; | ||
for ($index=$start; $index <= $end; $index++) { | ||
$nestedToken = $tokens[$index]; | ||
if ($nestedToken['type'] === "T_SWITCH") { | ||
return; | ||
} | ||
$this->adjustMaxIndentationFound($nestedToken); | ||
} | ||
} | ||
// @codingStandardsIgnoreEnd | ||
|
||
/** | ||
* Adjust the maximum indentation level found value. | ||
* @param array $nestedToken Token data. | ||
* @return void | ||
*/ | ||
protected function adjustMaxIndentationFound(array $nestedToken) | ||
{ | ||
$tokenNestedLevel = $nestedToken['level']; | ||
$nestedLevel = $tokenNestedLevel - $this->relativeScopeLevel; | ||
$nestedLevel > $this->maxIndentationFound ? $this->maxIndentationFound = $nestedLevel : null; | ||
} | ||
|
||
/** | ||
* Produce the error message. | ||
* @return string | ||
*/ | ||
protected function getErrorMessage() | ||
{ | ||
// Hack to fix the output numbers for the | ||
// indentation levels found and the | ||
// indentation limit. | ||
$indentationFound = $this->maxIndentationFound - 1; | ||
$indentationLimit = $this->indentationLimit - 1; | ||
return "{$indentationFound} indenation levels found. " . | ||
"Maximum of {$indentationLimit} indenation levels allowed."; | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
tests/Sniffs/Files/Assets/IndentationLevelSniff/ClosureWithNoCode.inc
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,5 @@ | ||
<?php | ||
|
||
$foo = function () | ||
{ | ||
}; |
6 changes: 6 additions & 0 deletions
6
tests/Sniffs/Files/Assets/IndentationLevelSniff/ClosureWithNoIndentation.inc
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,6 @@ | ||
<?php | ||
|
||
$foo = function () | ||
{ | ||
$oneIndentation; | ||
}; |
9 changes: 9 additions & 0 deletions
9
tests/Sniffs/Files/Assets/IndentationLevelSniff/ClosureWithOneIndentation.inc
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,9 @@ | ||
<?php | ||
|
||
$foo = function () | ||
{ | ||
$oneIndentation; | ||
if (true) { | ||
$twoIndentation; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
tests/Sniffs/Files/Assets/IndentationLevelSniff/ClosureWithTwoIndentation.inc
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,12 @@ | ||
<?php | ||
|
||
$foo = function () | ||
{ | ||
$oneIndentation; | ||
if (true) { | ||
$twoIndentation; | ||
if (true) { | ||
$threeIndentation; | ||
} | ||
} | ||
}; |
6 changes: 6 additions & 0 deletions
6
tests/Sniffs/Files/Assets/IndentationLevelSniff/FunctionWithNoCode.inc
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,6 @@ | ||
<?php | ||
|
||
function foo() | ||
{ | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
tests/Sniffs/Files/Assets/IndentationLevelSniff/FunctionWithNoIndentation.inc
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,6 @@ | ||
<?php | ||
|
||
function foo() | ||
{ | ||
$oneIndentation; | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Sniffs/Files/Assets/IndentationLevelSniff/FunctionWithOneIndentation.inc
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,9 @@ | ||
<?php | ||
|
||
function foo() | ||
{ | ||
$oneIndentation; | ||
if (true) { | ||
$twoIndentation; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Sniffs/Files/Assets/IndentationLevelSniff/FunctionWithTwoIndentation.inc
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,12 @@ | ||
<?php | ||
|
||
function foo() | ||
{ | ||
$oneIndentation; | ||
if (true) { | ||
$twoIndentation; | ||
if (true) { | ||
$threeIndentation; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/Sniffs/Files/Assets/IndentationLevelSniff/InvalidClass.inc
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,40 @@ | ||
<?php | ||
|
||
class Foo | ||
{ | ||
|
||
protected $baz; | ||
|
||
public function foo() | ||
{ | ||
return true; | ||
} | ||
|
||
public function bar() | ||
{ | ||
$one = 1; | ||
$two = 2; | ||
$three = 3; | ||
return true; | ||
} | ||
|
||
public function baz() | ||
{ | ||
if (true) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function tooMuchIndentation() | ||
{ | ||
if (true) { | ||
if (true) { | ||
return "Uh oh"; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/Sniffs/Files/Assets/IndentationLevelSniff/MethodWithNoCode.inc
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,8 @@ | ||
<?php | ||
|
||
class foo | ||
{ | ||
public function bar() | ||
{ | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Sniffs/Files/Assets/IndentationLevelSniff/MethodWithNoIndentation.inc
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,9 @@ | ||
<?php | ||
|
||
class foo | ||
{ | ||
public function bar() | ||
{ | ||
$oneIndentation; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Sniffs/Files/Assets/IndentationLevelSniff/MethodWithOneIndentation.inc
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,12 @@ | ||
<?php | ||
|
||
class foo | ||
{ | ||
public function bar() | ||
{ | ||
$oneIndentation; | ||
if (true) { | ||
$twoIndentation; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Sniffs/Files/Assets/IndentationLevelSniff/MethodWithTwoIndentation.inc
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,15 @@ | ||
<?php | ||
|
||
class foo | ||
{ | ||
public function bar() | ||
{ | ||
$oneIndentation; | ||
if (true) { | ||
$twoIndentation; | ||
if (true) { | ||
$threeIndentation; | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/Sniffs/Files/Assets/IndentationLevelSniff/SwitchStatementInFunctionWithNoCode.inc
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,8 @@ | ||
<?php | ||
|
||
function foobar() | ||
{ | ||
switch ($condition) { | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
.../Sniffs/Files/Assets/IndentationLevelSniff/SwitchStatementInFunctionWithNoIndentation.inc
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,9 @@ | ||
<?php | ||
|
||
function foobar() | ||
{ | ||
switch ($condition) { | ||
case true: | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
...Sniffs/Files/Assets/IndentationLevelSniff/SwitchStatementInFunctionWithOneIndentation.inc
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,11 @@ | ||
<?php | ||
|
||
function foobar() | ||
{ | ||
switch ($condition) { | ||
case '1': | ||
echo 'hi'; | ||
break; | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
...Sniffs/Files/Assets/IndentationLevelSniff/SwitchStatementInFunctionWithTwoIndentation.inc
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,13 @@ | ||
<?php | ||
|
||
function foobar() | ||
{ | ||
switch ($condition) { | ||
case '1': | ||
if (true) { | ||
$threeIndentation; | ||
} | ||
break; | ||
} | ||
} | ||
|
Oops, something went wrong.