From b9d5f27194e50c381f6c5fc4f8948a99d5814b59 Mon Sep 17 00:00:00 2001 From: Simon Welsh Date: Mon, 13 May 2013 21:23:37 +1200 Subject: [PATCH] Add code-sniffs other than line length to scrutinizer --- .scrutinizer.yml | 5 +++++ .travis.yml | 9 ++------ tests/phpcs/ruleset.xml | 8 ------- tests/phpcs/tabs.xml | 8 +++++++ tests/phpcs_runner.php | 48 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 .scrutinizer.yml create mode 100644 tests/phpcs_runner.php diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 000000000..67c425e2a --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,5 @@ +tools: + custom_commands: + - + scope: file + command: php tests/phpcs_runner.php %pathname% diff --git a/.travis.yml b/.travis.yml index 46631b841..9f6c99cb4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ env: - DB=MYSQL CORE_RELEASE=3.0 - DB=PGSQL CORE_RELEASE=3.0 - DB=SQLITE3 CORE_RELEASE=3.0 - - PHPCS=1 CORE_RELEASE=3.0 matrix: exclude: @@ -16,22 +15,18 @@ matrix: env: DB=PGSQL CORE_RELEASE=3.0 - php: 5.4 env: DB=SQLITE3 CORE_RELEASE=3.0 - - php: 5.4 - env: PHPCS=1 CORE_RELEASE=3.0 allow_failures: - env: DB=PGSQL CORE_RELEASE=3.0 - env: DB=SQLITE3 CORE_RELEASE=3.0 - - env: PHPCS=1 CORE_RELEASE=3.0 before_script: - - pear install pear/PHP_CodeSniffer - phpenv rehash - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - cd ~/builds/ss script: - - sh -c "if [ '$PHPCS' != '1' ]; then phpunit framework/tests; else phpcs --encoding=utf-8 --tab-width=4 --standard=framework/tests/phpcs/ruleset.xml -np framework && phpcs --encoding=utf-8 --standard=framework/tests/phpcs/tabs.xml -np framework; fi" + - phpunit framework/tests branches: except: @@ -43,4 +38,4 @@ branches: notifications: irc: channels: - - "irc.freenode.org#silverstripe" \ No newline at end of file + - "irc.freenode.org#silverstripe" diff --git a/tests/phpcs/ruleset.xml b/tests/phpcs/ruleset.xml index 7d70c93cd..dc332ade7 100644 --- a/tests/phpcs/ruleset.xml +++ b/tests/phpcs/ruleset.xml @@ -14,14 +14,6 @@ */SSTemplateParser.php$ - - 8 - - - - - - 7 diff --git a/tests/phpcs/tabs.xml b/tests/phpcs/tabs.xml index 93dc08cb6..c6a996e86 100644 --- a/tests/phpcs/tabs.xml +++ b/tests/phpcs/tabs.xml @@ -14,5 +14,13 @@ */SSTemplateParser.php$ + + 8 + + + + + + diff --git a/tests/phpcs_runner.php b/tests/phpcs_runner.php new file mode 100644 index 000000000..43a53dc00 --- /dev/null +++ b/tests/phpcs_runner.php @@ -0,0 +1,48 @@ +\n"); +} + +$result = array('comments' => array()); + +// Run each sniff + +// phpcs --encoding=utf-8 --standard=framework/tests/phpcs/tabs.xml +run_sniff('tabs.xml', $path, $result); + +// phpcs --encoding=utf-8 --tab-width=4 --standard=framework/tests/phpcs/ruleset.xml +run_sniff('ruleset.xml', $path, $result, '--tab-width=4'); + +echo json_encode($result); + +function run_sniff($standard, $path, array &$result, $extraFlags = '') { + $sniffPath = escapeshellarg(__DIR__ . '/phpcs/' . $standard); + $checkPath = escapeshellarg($path); + + exec("phpcs --encoding=utf-8 $extraFlags --standard=$sniffPath --report=xml $checkPath", $output); + + // We can't check the return code as it's non-zero if the sniff finds an error + if($output) { + $xml = implode("\n", $output); + $xml = simplexml_load_string($xml); + $errors = $xml->xpath('/phpcs/file/error'); + if($errors) { + $sanePath = str_replace('/', '_', $path); + foreach($errors as $error) { + $attributes = $error->attributes(); + $result['comments'][] = array( + 'line' => (int)strval($attributes->line), + 'id' => $standard . '-' . $sanePath . '-' . $attributes->line . '-' . $attributes->column, + 'message' => strval($error) + ); + } + } + } +}