Skip to content

Commit

Permalink
Merge pull request #34 from bmitch/Issue#33
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
bmitch authored Dec 9, 2016
2 parents 9ba91dc + 3a89c9d commit ed1504a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Functions/methods must not return `null`.
### Codor.Classes.ClassLength ###
Classes must be 200 lines of code or fewer.

### Codor.Files.FunctionNameContainsAndOr ###
Functions/methods cannot contain "And" or "Or". This could be a sign of a function that does more than one thing.

## Contributing ##
Please see [CONTRIBUTING.md](CONTRIBUTING.md)

Expand Down
83 changes: 83 additions & 0 deletions src/Codor/Sniffs/Files/FunctionNameContainsAndOrSniff.php
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.";
}
}
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()
{

}
28 changes: 28 additions & 0 deletions tests/Sniffs/Files/FunctionNameContainsAndOrSniffTest.php
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());
}
}

0 comments on commit ed1504a

Please sign in to comment.