Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #15 #18

Merged
merged 1 commit into from
Dec 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}