-
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.
Merge pull request #64 from bmitch/bmitchell-#28
Fixes #28
- Loading branch information
Showing
4 changed files
with
155 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
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,45 @@ | ||
<?php | ||
|
||
namespace Codor\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer_Sniff; | ||
use PHP_CodeSniffer_File; | ||
|
||
class MethodFlagParameterSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
protected $booleans = ['T_FALSE', 'T_TRUE']; | ||
|
||
/** | ||
* Returns the token types that this sniff is interested in. | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_FUNCTION]; | ||
} | ||
|
||
/** | ||
* 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]; | ||
$openParenIndex = $token['parenthesis_opener']; | ||
$closedParenIndex = $token['parenthesis_closer']; | ||
|
||
for ($index=$openParenIndex+1; $index <= $closedParenIndex; $index++) { | ||
if (in_array($tokens[$index]['type'], $this->booleans)) { | ||
$phpcsFile->addError("Function/method contains a flag parameter.", $stackPtr); | ||
continue; | ||
} | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
tests/Sniffs/Files/Assets/MethodFlagParamater/MethodFlagParamater.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,74 @@ | ||
<?php | ||
function alpha($one) | ||
{ | ||
//pass | ||
} | ||
|
||
function beta($one = true) | ||
{ | ||
//fail | ||
} | ||
|
||
function charlie($one = false) | ||
{ | ||
//fail | ||
} | ||
|
||
function delta($one = false, $two = false) | ||
{ | ||
//fail X 2 | ||
} | ||
|
||
function echo($one=true) | ||
{ | ||
//fail | ||
} | ||
|
||
function foxtrot($one=false) | ||
{ | ||
//fail | ||
} | ||
|
||
function golf($one, $two, $tree) | ||
{ | ||
//pass | ||
} | ||
|
||
class Foobar | ||
{ | ||
function alpha($one) | ||
{ | ||
//pass | ||
} | ||
|
||
function beta($one = true) | ||
{ | ||
//fail | ||
} | ||
|
||
function charlie($one = false) | ||
{ | ||
//fail | ||
} | ||
|
||
function delta($one = false, $two = false) | ||
{ | ||
//fail X 2 | ||
} | ||
|
||
function echo($one=true) | ||
{ | ||
//fail | ||
} | ||
|
||
function foxtrot($one=false) | ||
{ | ||
//fail | ||
} | ||
|
||
function golf($one, $two, $tree) | ||
{ | ||
//pass | ||
} | ||
} | ||
|
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 | ||
|
||
namespace Codor\Tests\Sniffs\ControlStructures; | ||
|
||
use Codor\Tests\BaseTestCase; | ||
|
||
/** @group Files */ | ||
class MethodFlagParamaterTest extends BaseTestCase | ||
{ | ||
|
||
/** | ||
* Sets up the test class. | ||
* @return void | ||
*/ | ||
public function setup() | ||
{ | ||
parent::setup(); | ||
|
||
$this->runner->setSniff('Codor.Files.MethodFlagParameter')->setFolder(__DIR__.'/Assets/MethodFlagParamater/'); | ||
} | ||
|
||
/** @test */ | ||
public function it_detects_functions_that_are_over_the_max_allowed() | ||
{ | ||
$results = $this->runner->sniff('MethodFlagParamater.inc'); | ||
$this->assertSame(12, $results->getErrorCount()); | ||
$this->assertSame(0, $results->getWarningCount()); | ||
|
||
$errorMessages = $results->getAllErrorMessages(); | ||
$this->assertCount(10, $errorMessages); | ||
$this->assertAllEqual('Function/method contains a flag parameter.', $errorMessages); | ||
} | ||
} |