Skip to content

Commit

Permalink
Merge pull request #64 from bmitch/bmitchell-#28
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
bmitch committed Mar 28, 2017
2 parents bb17395 + ff08431 commit 6d5a547
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Functions/methods must have no more than 3 parameters.
### Codor.Files.ReturnNull ###
Functions/methods must not return `null`.

### Codor.Files.MethodFlagParameter ###
Functions/methods cannot have parameters that default to a boolean.

### Codor.Classes.ClassLength ###
Classes must be no more than 200 lines.

Expand Down
45 changes: 45 additions & 0 deletions src/Codor/Sniffs/Files/MethodFlagParameterSniff.php
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;
}
}
}
}
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
}
}

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

0 comments on commit 6d5a547

Please sign in to comment.