Skip to content

Commit

Permalink
add support for PHP_CodeSniffer v3
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed Mar 27, 2018
1 parent 590f4cb commit 2f9611c
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.vscode/
/build/
/coverage/
/doc/
Expand Down
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ php:
- 7
- 7.0
- 7.1
- 7.2
- hhvm

sudo: false

install:
- composer selfupdate
- composer install
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require codeclimate/php-test-reporter satooshi/php-coveralls; fi
- |
if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then
composer require 'codeclimate/php-test-reporter:*' 'satooshi/php-coveralls:*'
else
composer install
fi
script:
- composer travis
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"php": ">=5.6.0",
"schnittstabil/composer-extra": "^2.0",
"schnittstabil/finder-by-config": "^1.0",
"squizlabs/php_codesniffer": "^2.6"
"squizlabs/php_codesniffer": "^2.6 || 3.0"
},
"require-dev": {
"sugared-rim/phpunit": "^6.0"
"sugared-rim/phpunit": "^6.0 || ^7.0"
},
"extra": {
"sugared-rim/phpunit": {
Expand Down Expand Up @@ -53,10 +53,10 @@
"php-cs-fixer fix --dry-run --diff tests"
],
"test": [
"sugared-rim-phpunit",
"composer lint",
"./sugared-rim-phpcbf --help",
"./sugared-rim-phpcs --help"
"./sugared-rim-phpcs --help",
"sugared-rim-phpunit",
"composer lint"
],
"travis": "sugared-rim-phpunit"
}
Expand Down
85 changes: 4 additions & 81 deletions src/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,21 @@

namespace SugaredRim\PHP\CodeSniffer;

use Schnittstabil\ComposerExtra\ComposerExtra;
use Schnittstabil\FinderByConfig\FinderByConfig;

class CLI extends \PHP_CodeSniffer_CLI
{
protected $namespace = 'sugared-rim/php_codesniffer';
protected $finderByConfig;
protected $defaultConfig;
protected $config;
use SugaredRimConfigAwareTrait;

public function __construct(callable $finderByConfig = null)
{
if ($finderByConfig === null) {
$finderByConfig = new FinderByConfig();
}
$this->finderByConfig = $finderByConfig;

$this->defaultConfig = new \stdClass();
$this->defaultConfig->presets = [
'SugaredRim\\PHP\\CodeSniffer\\DefaultPreset::get',
];
}

protected function getConfig($path = null, $default = null)
{
if ($this->config === null) {
$this->config = new ComposerExtra(
$this->namespace,
$this->defaultConfig,
'presets'
);
}

if ($path === null) {
$path = array();
}

return $this->config->get($path, $default);
}

public function processLongArgument($arg, $pos)
{
if (substr($arg, 0, 10) === 'namespace=') {
$this->namespace = substr($arg, 10);

return;
}

parent::processLongArgument($arg, $pos);
}

public function setCommandLineValues($args)
{
parent::setCommandLineValues($args);

if (!empty($this->values['files'])) {
return;
}

$files = $this->getConfig('files', []);

foreach (call_user_func($this->finderByConfig, $files) as $file) {
$this->processUnknownArgument($file->getPathname(), -1);
}
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function processConfig()
{
foreach ($this->getConfig() as $key => $value) {
switch ($key) {
case 'presets':
case 'files':
continue 2;
case 'default_standard':
if (is_array($value)) {
$value = implode(',', $value);
}
}

\PHP_CodeSniffer::setConfigData($key, $value, true);
}
$this->sugaredRimSetFinderByConfig($finderByConfig);
}

/**
* @codeCoverageIgnore hard coded `exit()` in `parent::runphpcbf`
*/
public function runphpcbf()
{
$this->processConfig();
$this->sugaredRimProcessConfig();
parent::runphpcbf();
}

Expand All @@ -102,7 +25,7 @@ public function runphpcbf()
*/
public function runphpcs()
{
$this->processConfig();
$this->sugaredRimProcessConfig();
parent::runphpcs();
}
}
14 changes: 14 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SugaredRim\PHP\CodeSniffer;

class Config extends \PHP_CodeSniffer\Config
{
use SugaredRimConfigAwareTrait;

public function __construct(array $cliArgs = [], $dieOnUnknownArg = true, callable $finderByConfig = null)
{
$this->sugaredRimSetFinderByConfig($finderByConfig);
parent::__construct($cliArgs, $dieOnUnknownArg);
}
}
46 changes: 46 additions & 0 deletions src/Runner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SugaredRim\PHP\CodeSniffer;

class Runner extends \PHP_CodeSniffer\Runner
{
/**
* @var string
*/
protected $sugaredRimTrigger;

/**
* @inheritDoc
*/
public function init():void
{
$this->config = new Config();

// v3.* and still sucks :-(
if ($this->sugaredRimTrigger === 'runPHPCBF') {
if ($this->config->stdin === true) {
$this->config->verbosity = 0;
}
}

parent::init();
}

/**
* @inheritDoc
*/
public function runPHPCS()
{
$this->sugaredRimTrigger = 'runPHPCS';
return parent::runPHPCS();
}

/**
* @inheritDoc
*/
public function runPHPCBF()
{
$this->sugaredRimTrigger = 'runPHPCBF';
return parent::runPHPCBF();
}
}
105 changes: 105 additions & 0 deletions src/SugaredRimConfigAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace SugaredRim\PHP\CodeSniffer;

use Schnittstabil\ComposerExtra\ComposerExtra;
use Schnittstabil\FinderByConfig\FinderByConfig;

trait SugaredRimConfigAwareTrait
{
protected $sugaredRimNamespace = 'sugared-rim/php_codesniffer';
protected $sugaredRimfinderByConfig;
protected $sugaredRimDefaultConfig;
protected $sugaredRimConfig;

protected function sugaredRimSetFinderByConfig(callable $finderByConfig = null)
{
if ($finderByConfig === null) {
$finderByConfig = new FinderByConfig();
}
$this->sugaredRimfinderByConfig = $finderByConfig;

$this->sugaredRimDefaultConfig = new \stdClass();
$this->sugaredRimDefaultConfig->presets = [
'SugaredRim\\PHP\\CodeSniffer\\DefaultPreset::get',
];
}

protected function sugaredRimGetConfig($path = null, $default = null)
{
if ($this->sugaredRimConfig === null) {
$this->sugaredRimConfig = new ComposerExtra(
$this->sugaredRimNamespace,
$this->sugaredRimDefaultConfig,
'presets'
);
}

if ($path === null) {
$path = array();
}

return $this->sugaredRimConfig->get($path, $default);
}

public function processLongArgument($arg, $pos)
{
if (substr($arg, 0, 10) === 'namespace=') {
$this->sugaredRimNamespace = substr($arg, 10);

return;
}

parent::processLongArgument($arg, $pos);
}

public function setCommandLineValues($args)
{
parent::setCommandLineValues($args);

if (isset($this->values) && !empty($this->values['files'])) {
// v2.*
return;
} elseif (isset($this->files) && !empty($this->files)) {
// v3.*
return;
}

$files = $this->sugaredRimGetConfig('files', []);

foreach (call_user_func($this->sugaredRimfinderByConfig, $files) as $file) {
$this->processUnknownArgument($file->getPathname(), -1);
}
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function sugaredRimProcessConfig()
{
foreach ($this->sugaredRimGetConfig() as $key => $value) {
switch ($key) {
case 'presets':
case 'files':
continue 2;
case 'default_standard':
if (is_array($value)) {
$value = implode(',', $value);
}
}

$this->sugaredRimSetConfigData($key, $value);
}
}

abstract public function setConfigData($key, $value, $temp = false);

protected function sugaredRimSetConfigData($key, $value)
{
if (method_exists($this, 'setConfigData')) {
$this->setConfigData($key, $value, true);
} else {
\PHP_CodeSniffer::setConfigData($key, $value, true);
}
}
}
15 changes: 10 additions & 5 deletions sugared-rim-phpcbf
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env php
<?php
foreach ([
__DIR__.'/../../autoload.php',
__DIR__.'/../vendor/autoload.php',
__DIR__.'/vendor/autoload.php'
] as $file) {
__DIR__ . '/vendor/squizlabs/php_codesniffer/autoload.php',
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
] as $file) {

if (file_exists($file)) {
require $file;
Expand All @@ -14,4 +15,8 @@ foreach ([

unset($file);

(new SugaredRim\PHP\CodeSniffer\CLI())->runphpcbf();
if (class_exists(\PHP_CodeSniffer_CLI::class)) {
(new SugaredRim\PHP\CodeSniffer\CLI())->runphpcbf();
} else {
exit((new \SugaredRim\PHP\CodeSniffer\Runner())->runPHPCBF());
}
7 changes: 6 additions & 1 deletion sugared-rim-phpcs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
foreach ([
__DIR__. '/vendor/squizlabs/php_codesniffer/autoload.php',
__DIR__.'/../../autoload.php',
__DIR__.'/../vendor/autoload.php',
__DIR__.'/vendor/autoload.php'
Expand All @@ -14,4 +15,8 @@ foreach ([

unset($file);

(new SugaredRim\PHP\CodeSniffer\CLI())->runphpcs();
if (class_exists(\PHP_CodeSniffer_CLI::class)) {
(new \SugaredRim\PHP\CodeSniffer\CLI())->runphpcs();
} else {
exit((new \SugaredRim\PHP\CodeSniffer\Runner())->runPHPCS());
}
7 changes: 7 additions & 0 deletions tests/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class CLITest extends \PHPUnit\Framework\TestCase
{
protected function setUp()
{
if (!class_exists(\PHP_CodeSniffer_CLI::class)) {
$this->markTestSkipped('The \PHP_CodeSniffer_CLI class is not available.');
}
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
Expand Down
Loading

0 comments on commit 2f9611c

Please sign in to comment.