-
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
5 changed files
with
104 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,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Codor\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer_Sniff; | ||
use PHP_CodeSniffer_File; | ||
|
||
class ForbiddenMethodsSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
/** | ||
* The methods that are forbidden. | ||
* @var array | ||
*/ | ||
public $forbiddenMethods = ['raw', 'statement']; | ||
|
||
/** | ||
* Returns the token types that this sniff is interested in. | ||
* @return array | ||
*/ | ||
public function register(): array | ||
{ | ||
return [T_OBJECT_OPERATOR, T_DOUBLE_COLON]; | ||
} | ||
|
||
/** | ||
* 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(); | ||
$potentialMethodToken = $tokens[$stackPtr + 1]; | ||
$openParenToken = $tokens[$stackPtr + 2]; | ||
|
||
if ($openParenToken['type'] !== 'T_OPEN_PARENTHESIS') { | ||
return; | ||
} | ||
|
||
$methodName = $potentialMethodToken['content']; | ||
if (in_array($methodName, $this->forbiddenMethods, true)) { | ||
$phpcsFile->addError("Call to method {$methodName} is forbidden.", $potentialMethodToken['line']); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/Sniffs/Files/Assets/ForbiddenMethodsSniff/CallingForbiddenMethods.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,5 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
|
||
$foo->raw(); | ||
$foo->statement(); |
4 changes: 4 additions & 0 deletions
4
tests/Sniffs/Files/Assets/ForbiddenMethodsSniff/CallingForbiddenMethodsStatic.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,4 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
DB::raw(); | ||
DB::statement(); |
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,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Codor\Tests\Sniffs\Files; | ||
|
||
use Codor\Tests\BaseTestCase; | ||
|
||
/** @group Files */ | ||
class ForbiddenMethodsSniffTest extends BaseTestCase | ||
{ | ||
/** | ||
* Sets up the test class. | ||
* @return void | ||
*/ | ||
public function setup() | ||
{ | ||
parent::setup(); | ||
|
||
$this->runner->setSniff('Codor.Files.ForbiddenMethods')->setFolder(__DIR__.'/Assets/ForbiddenMethodsSniff/'); | ||
} | ||
|
||
/** @test */ | ||
public function it_produces_errors_for_each_forbidden_method_call_found() | ||
{ | ||
$results = $this->runner->sniff('CallingForbiddenMethods.inc'); | ||
$this->assertSame(2, $results->getErrorCount()); | ||
$this->assertSame(0, $results->getWarningCount()); | ||
|
||
$errorMessages = $results->getAllErrorMessages(); | ||
$this->assertCount(2, $errorMessages); | ||
$this->assertSame('Call to method raw is forbidden.', $errorMessages[0]); | ||
$this->assertSame('Call to method statement is forbidden.', $errorMessages[1]); | ||
} | ||
|
||
/** @test */ | ||
public function it_produces_errors_for_each_forbidden_method_static_call_found() | ||
{ | ||
$results = $this->runner->sniff('CallingForbiddenMethodsStatic.inc'); | ||
$this->assertSame(2, $results->getErrorCount()); | ||
$this->assertSame(0, $results->getWarningCount()); | ||
|
||
$errorMessages = $results->getAllErrorMessages(); | ||
$this->assertCount(2, $errorMessages); | ||
$this->assertSame('Call to method raw is forbidden.', $errorMessages[0]); | ||
$this->assertSame('Call to method statement is forbidden.', $errorMessages[1]); | ||
} | ||
} |