Skip to content

Commit

Permalink
PHP_CodeSniffer 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
VasekPurchart committed Jun 16, 2017
1 parent d85a143 commit 5b991d5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 44 deletions.
20 changes: 10 additions & 10 deletions Consistence/Sniffs/NamingConventions/ValidVariableNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Consistence\Sniffs\NamingConventions;

use PHP_CodeSniffer;
use PHP_CodeSniffer_File;
use PHP_CodeSniffer\Files\File as PhpCsFile;
use PHP_CodeSniffer\Util\Common as PhpCsUtil;

class ValidVariableNameSniff extends \PHP_CodeSniffer_Standards_AbstractVariableSniff
class ValidVariableNameSniff extends \PHP_CodeSniffer\Sniffs\AbstractVariableSniff
{

const CODE_CAMEL_CAPS = 'NotCamelCaps';
Expand All @@ -26,10 +26,10 @@ class ValidVariableNameSniff extends \PHP_CodeSniffer_Standards_AbstractVariable
];

/**
* @param \PHP_CodeSniffer_File $file
* @param \PHP_CodeSniffer\Files\File $file
* @param int $stackPointer position of the double quoted string
*/
protected function processVariable(PHP_CodeSniffer_File $file, $stackPointer)
protected function processVariable(PhpCsFile $file, $stackPointer)
{
$tokens = $file->getTokens();
$varName = ltrim($tokens[$stackPointer]['content'], '$');
Expand All @@ -43,7 +43,7 @@ protected function processVariable(PHP_CodeSniffer_File $file, $stackPointer)
return; // skip MyClass::$variable, there might be no control over the declaration
}

if (!PHP_CodeSniffer::isCamelCaps($varName, false, true, false)) {
if (!PhpCsUtil::isCamelCaps($varName, false, true, false)) {
$error = 'Variable "%s" is not in valid camel caps format';
$data = [$varName];
$file->addError($error, $stackPointer, self::CODE_CAMEL_CAPS, $data);
Expand All @@ -53,21 +53,21 @@ protected function processVariable(PHP_CodeSniffer_File $file, $stackPointer)
/**
* @codeCoverageIgnore
*
* @param \PHP_CodeSniffer_File $file
* @param \PHP_CodeSniffer\Files\File $file
* @param int $stackPointer position of the double quoted string
*/
protected function processMemberVar(PHP_CodeSniffer_File $file, $stackPointer)
protected function processMemberVar(PhpCsFile $file, $stackPointer)
{
// handled by PSR2.Classes.PropertyDeclaration
}

/**
* @codeCoverageIgnore
*
* @param \PHP_CodeSniffer_File $file
* @param \PHP_CodeSniffer\Files\File $file
* @param int $stackPointer position of the double quoted string
*/
protected function processVariableInString(PHP_CodeSniffer_File $file, $stackPointer)
protected function processVariableInString(PhpCsFile $file, $stackPointer)
{
// Consistence standard does not allow variables in strings, handled by Squiz.Strings.DoubleQuoteUsage
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
],
"require": {
"php": "~7.0",
"squizlabs/php_codesniffer": "~2.9.1",
"slevomat/coding-standard": "~2.4"
"squizlabs/php_codesniffer": "~3.0.1",
"slevomat/coding-standard": "~3.0"
},
"require-dev": {
"jakub-onderka/php-console-highlighter": "0.3.2",
Expand Down
4 changes: 2 additions & 2 deletions tests/Sniffs/NamingConventions/ValidVariableNameSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Consistence\Sniffs\NamingConventions;

use PHP_CodeSniffer_File;
use PHP_CodeSniffer\Files\File as PhpCsFile;

class ValidVariableNameSniffTest extends \Consistence\Sniffs\TestCase
{

private function getFileReport(): PHP_CodeSniffer_File
private function getFileReport(): PhpCsFile
{
return $this->checkFile(__DIR__ . '/data/FooClass.php');
}
Expand Down
48 changes: 18 additions & 30 deletions tests/Sniffs/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Consistence\Sniffs;

use PHP_CodeSniffer;
use PHP_CodeSniffer_File;
use PHP_CodeSniffer\Config as PhpCsConfig;
use PHP_CodeSniffer\Files\File as PhpCsFile;
use PHP_CodeSniffer\Files\LocalFile as PhpCsLocalFile;
use PHP_CodeSniffer\Runner as PhpCsRunner;

abstract class TestCase extends \PHPUnit\Framework\TestCase
{
Expand All @@ -20,17 +22,22 @@ public function __construct(string $name = null, array $data = [], string $dataN
parent::__construct($name, $data, $dataName);
}

protected function checkFile(string $filePath): PHP_CodeSniffer_File
protected function checkFile(string $filePath): PhpCsFile
{
$codeSniffer = new PHP_CodeSniffer();
$codeSniffer->cli->setCommandLineValues([
$codeSniffer = new PhpCsRunner();
$codeSniffer->config = new PhpCsConfig([
'-s', // showSources must be on, so that errors are recorded
]);

$codeSniffer->registerSniffs([$this->getSniffPath()], [], []);
$codeSniffer->populateTokenListeners();
$codeSniffer->init();

return $codeSniffer->processFile($filePath);
$codeSniffer->ruleset->sniffs = [$this->getSniffClassName() => $this->getSniffClassName()];
$codeSniffer->ruleset->populateTokenListeners();

$file = new PhpCsLocalFile($filePath, $codeSniffer->ruleset, $codeSniffer->config);
$file->process();

return $file;
}

protected function getSniffName(): string
Expand All @@ -50,37 +57,18 @@ protected function getSniffName(): string
);
}

private function getSniffPath(): string
{
$path = preg_replace(
[
'~\\\~',
'~Consistence~',
'~$~',
],
[
'/',
__DIR__ . '/../../Consistence',
'.php',
],
$this->getSniffClassName()
);

return realpath($path);
}

protected function getSniffClassName(): string
{
return substr(get_class($this), 0, -strlen('Test'));
}

/**
* @param \PHP_CodeSniffer_File $resultFile
* @param \PHP_CodeSniffer\Files\File $resultFile
* @param int $line
* @param string $code code used inside sniff to indicate error type
* @param string|null $message match part of text in error message
*/
protected function assertSniffError(PHP_CodeSniffer_File $resultFile, int $line, string $code, string $message = null)
protected function assertSniffError(PhpCsFile $resultFile, int $line, string $code, string $message = null)
{
$errors = $resultFile->getErrors();
$this->assertTrue(
Expand Down Expand Up @@ -125,7 +113,7 @@ private function hasError(array $errorsForLine, string $code, string $message =
return false;
}

protected function assertNoSniffError(PHP_CodeSniffer_File $resultFile, int $line)
protected function assertNoSniffError(PhpCsFile $resultFile, int $line)
{
$errors = $resultFile->getErrors();
$this->assertFalse(
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
error_reporting(E_ALL);

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/squizlabs/php_codesniffer/autoload.php';

0 comments on commit 5b991d5

Please sign in to comment.