Skip to content

Commit

Permalink
WIP #114
Browse files Browse the repository at this point in the history
  • Loading branch information
bmitch committed Sep 22, 2017
1 parent db04cf4 commit c20b8fc
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"larapack/dd": "^1.1"
},
"scripts": {
"test": [
"code-standards": [
"vendor/bin/parallel-lint src tests",
"vendor/bin/phpcs --standard=psr2 src -sp",
"vendor/bin/phpcs --standard=phpcs.xml src tests --ignore=tests/Sniffs -sp",
Expand Down
48 changes: 48 additions & 0 deletions src/Codor/Sniffs/Files/ForbiddenMethodsSniff.php
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']);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types = 1);


$foo->raw();
$foo->statement();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php declare(strict_types = 1);

DB::raw();
DB::statement();
46 changes: 46 additions & 0 deletions tests/Sniffs/Files/ForbiddenMethodsSniffTest.php
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]);
}
}

0 comments on commit c20b8fc

Please sign in to comment.