Skip to content

Commit

Permalink
Merge pull request #18 from bmitch/Issue#15
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
bmitch authored Dec 4, 2016
2 parents a8a2d5e + c1b504e commit a3abb9e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ Does not allow for any `else` or `elseif` statements.
Functions/methods must be 20 lines of code or fewer.

### Codor.Files.FunctionParameter ###
Functions/methods must have no more than 3 parameters.
Functions/methods must have no more than 3 parameters.

### Codor.Files.ReturnNull ###
Functions/methods must not returl `null`.
50 changes: 50 additions & 0 deletions src/Codor/Sniffs/Files/ReturnNullSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Codor\Sniffs\Files;

use PHP_CodeSniffer_Sniff;
use PHP_CodeSniffer_File;

class ReturnNullSniff implements PHP_CodeSniffer_Sniff
{

/**
* Returns the token types that this sniff is interested in.
* @return array
*/
public function register()
{
return [T_RETURN];
}

/**
* 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();
$returnTokenIndex = $stackPtr;

$returnValueToken = '';
for ($index = $returnTokenIndex; $index < count($tokens); $index++) {
if ($tokens[$index]['type'] === 'T_SEMICOLON') {
$returnValueToken = $tokens[$index - 1];
break;
}
}

if ($returnValueToken == '') {
return;
}

if ($returnValueToken['type'] === 'T_NULL') {
$error = "Return null value found.";
$phpcsFile->addError($error, $stackPtr);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

function foobar()
{
return null;
}

function barbaz()
{
return true;
}

class someClass
{
public function bazbar()
{
return null;
}
}
29 changes: 29 additions & 0 deletions tests/Sniffs/Files/ReturnNullSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Codor\Tests\Sniffs\ControlStructures;

use Codor\Tests\BaseTestCase;

/** @group Files */
class ReturnNullSniffTest extends BaseTestCase
{

/**
* Sets up the test class.
* @return void
*/
public function setup()
{
parent::setup();

$this->runner->setSniff('Codor.Files.ReturnNull')->setFolder(__DIR__.'/Assets/ReturnNullSniff/');
}

/** @test */
public function it_detects_functions_that_return_null()
{
$results = $this->runner->sniff('FunctionThatReturnsNull.inc');
$this->assertEquals(2, $results->getErrorCount());
$this->assertEquals(0, $results->getWarningCount());
}
}

0 comments on commit a3abb9e

Please sign in to comment.