Skip to content

Commit

Permalink
Add code-sniffs other than line length to scrutinizer
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwelsh committed May 22, 2013
1 parent 17ac2e3 commit b9d5f27
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,5 @@
tools:
custom_commands:
-
scope: file
command: php tests/phpcs_runner.php %pathname%
9 changes: 2 additions & 7 deletions .travis.yml
Expand Up @@ -8,30 +8,25 @@ 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:
- php: 5.4
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:
Expand All @@ -43,4 +38,4 @@ branches:
notifications:
irc:
channels:
- "irc.freenode.org#silverstripe"
- "irc.freenode.org#silverstripe"
8 changes: 0 additions & 8 deletions tests/phpcs/ruleset.xml
Expand Up @@ -14,14 +14,6 @@
<!-- PHP-PEG generated file not intended for human consumption -->
<exclude-pattern>*/SSTemplateParser.php$</exclude-pattern>

<rule ref="Generic.Files.LineEndings.InvalidEOLChar">
<severity>8</severity>
</rule>
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n" />
</properties>
</rule>
<rule ref="Generic.Files.LineLength.TooLong">
<severity>7</severity>
</rule>
Expand Down
8 changes: 8 additions & 0 deletions tests/phpcs/tabs.xml
Expand Up @@ -14,5 +14,13 @@
<!-- PHP-PEG generated file not intended for human consumption -->
<exclude-pattern>*/SSTemplateParser.php$</exclude-pattern>
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
<rule ref="Generic.Files.LineEndings.InvalidEOLChar">
<severity>8</severity>
</rule>
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n" />
</properties>
</rule>
</ruleset>

48 changes: 48 additions & 0 deletions tests/phpcs_runner.php
@@ -0,0 +1,48 @@
<?php

if(php_sapi_name() != 'cli') {
die("This script must be called from the command line\n");
}

if(!empty($_SERVER['argv'][1])) {
$path = $_SERVER['argv'][1];
} else {
die("Usage: php {$_SERVER['argv'][0]} <file>\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)
);
}
}
}
}

0 comments on commit b9d5f27

Please sign in to comment.