-
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
4 changed files
with
160 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,83 @@ | ||
<?php | ||
|
||
namespace Codor\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer_Sniff; | ||
use PHP_CodeSniffer_File; | ||
|
||
class FunctionNameContainsAndOrSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
/** | ||
* The forbidden strings this sniff looks for. | ||
* @var array | ||
*/ | ||
protected $keywords = ['And', '_and', 'Or', '_or']; | ||
|
||
/** | ||
* 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(); | ||
$functionNameToken = $tokens[$stackPtr + 2]; | ||
$functionName = $functionNameToken['content']; | ||
|
||
if (! $this->containsKeywords($functionName)) { | ||
return; | ||
} | ||
|
||
$phpcsFile->addError($this->getErrorMessage(), $stackPtr); | ||
} | ||
|
||
/** | ||
* Determines if the provided $string contains any of the | ||
* strings in the $this->keywords property. | ||
* @param string $string The string to check. | ||
* @return boolean | ||
*/ | ||
protected function containsKeywords($string) | ||
{ | ||
foreach ($this->keywords as $keyword) { | ||
if ($this->contains($keyword, $string)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Checks if the $haystack contains the $needle. | ||
* @param string $needle Needle string. | ||
* @param string $haystack Haystack string. | ||
* @return boolean | ||
*/ | ||
protected function contains($needle, $haystack) | ||
{ | ||
return strpos($haystack, $needle) !== false; | ||
} | ||
|
||
/** | ||
* Gets the error message for this sniff. | ||
* @return string | ||
*/ | ||
protected function getErrorMessage() | ||
{ | ||
return "Your function contains 'and' or 'or' which indicates it might be doing more than one thing."; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
tests/Sniffs/Files/Assets/FunctionNameContainsAndOrSnuff/FunctionNameContainsAndOrSniff.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,46 @@ | ||
<?php | ||
|
||
// This looks like it has the word | ||
// "and" but it's part of another word | ||
function expand() | ||
{ | ||
|
||
} | ||
|
||
// This looks like it has the word | ||
// "or" but it's part of another word | ||
function orbit() | ||
{ | ||
|
||
} | ||
|
||
function thisDoesThisAndThat($foo) | ||
{ | ||
|
||
return "This and that"; | ||
} | ||
|
||
function thisDoesThisOrThat($foo) | ||
{ | ||
return "This or that"; | ||
} | ||
|
||
function this_and_that() | ||
{ | ||
|
||
} | ||
|
||
function that_And_This() | ||
{ | ||
|
||
} | ||
|
||
function this_or_that() | ||
{ | ||
|
||
} | ||
|
||
function that_Or_This() | ||
{ | ||
|
||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Codor\Tests\Sniffs\ControlStructures; | ||
|
||
use Codor\Tests\BaseTestCase; | ||
|
||
/** @group Files */ | ||
class FunctionNameContainsAndOrSniffTest extends BaseTestCase | ||
{ | ||
|
||
/** | ||
* Sets up the test class. | ||
* @return void | ||
*/ | ||
public function setup() | ||
{ | ||
parent::setup(); | ||
|
||
$this->runner->setSniff('Codor.Files.FunctionNameContainsAndOr')->setFolder(__DIR__.'/Assets/FunctionNameContainsAndOrSnuff/'); | ||
} | ||
|
||
/** @test */ | ||
public function it_detects_functions_that_are_over_the_max_allowed() | ||
{ | ||
$results = $this->runner->sniff('FunctionNameContainsAndOrSniff.inc'); | ||
$this->assertSame(6, $results->getErrorCount()); | ||
} | ||
} |