-
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
102 additions
and
1 deletion.
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,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); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Sniffs/Files/Assets/ReturnNullSniff/FunctionThatReturnsNull.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,19 @@ | ||
<?php | ||
|
||
function foobar() | ||
{ | ||
return null; | ||
} | ||
|
||
function barbaz() | ||
{ | ||
return true; | ||
} | ||
|
||
class someClass | ||
{ | ||
public function bazbar() | ||
{ | ||
return null; | ||
} | ||
} |
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,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()); | ||
} | ||
} |