Skip to content

Commit 4eb922e

Browse files
committed
You can now specify the full path to a coding standard on the command line rather than having to use one of the installed standards
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@248400 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent 49113fe commit 4eb922e

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

CodeSniffer.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,13 @@ public function process($files, $standard, array $sniffs=array(), $local=false)
232232
throw new PHP_CodeSniffer_Exception('$standard must be a string');
233233
}
234234

235-
$this->standardDir = realpath(dirname(__FILE__).'/CodeSniffer/Standards/'.$standard);
235+
if (is_dir($standard) === true) {
236+
// This is a custom standard.
237+
$this->standardDir = $standard;
238+
$standard = basename($standard);
239+
} else {
240+
$this->standardDir = realpath(dirname(__FILE__).'/CodeSniffer/Standards/'.$standard);
241+
}
236242

237243
// Reset the members.
238244
$this->listeners = array();
@@ -373,10 +379,18 @@ public static function getSniffFiles($dir, $standard=null)
373379

374380
$included = $standardClass->getIncludedSniffs();
375381
foreach ($included as $sniff) {
376-
$sniffDir = realpath(dirname(__FILE__)."/CodeSniffer/Standards/$sniff");
377-
378-
if ($sniffDir === false) {
379-
throw new PHP_CodeSniffer_Exception("Included sniff $sniff does not exist");
382+
if (is_dir($sniff) === true) {
383+
// Trying to include from a custom standard.
384+
$sniffDir = $sniff;
385+
$sniff = basename($sniff);
386+
} else if (is_file($sniff) === true) {
387+
// Trying to include a custom sniff.
388+
$sniffDir = $sniff;
389+
} else {
390+
$sniffDir = realpath(dirname(__FILE__)."/CodeSniffer/Standards/$sniff");
391+
if ($sniffDir === false) {
392+
throw new PHP_CodeSniffer_Exception("Included sniff $sniff does not exist");
393+
}
380394
}
381395

382396
if (is_dir($sniffDir) === true) {
@@ -398,10 +412,18 @@ public static function getSniffFiles($dir, $standard=null)
398412

399413
$excluded = $standardClass->getExcludedSniffs();
400414
foreach ($excluded as $sniff) {
401-
$sniffDir = realpath(dirname(__FILE__)."/CodeSniffer/Standards/$sniff");
402-
403-
if ($sniffDir === false) {
404-
throw new PHP_CodeSniffer_Exception("Excluded sniff $sniff does not exist");
415+
if (is_dir($sniff) === true) {
416+
// Trying to exclude from a custom standard.
417+
$sniffDir = $sniff;
418+
$sniff = basename($sniff);
419+
} else if (is_file($sniff) === true) {
420+
// Trying to exclude a custom sniff.
421+
$sniffDir = $sniff;
422+
} else {
423+
$sniffDir = realpath(dirname(__FILE__)."/CodeSniffer/Standards/$sniff");
424+
if ($sniffDir === false) {
425+
throw new PHP_CodeSniffer_Exception("Excluded sniff $sniff does not exist");
426+
}
405427
}
406428

407429
if (is_dir($sniffDir) === true) {
@@ -1410,7 +1432,14 @@ public static function isInstalledStandard($standard)
14101432
{
14111433
$standardDir = dirname(__FILE__);
14121434
$standardDir .= '/CodeSniffer/Standards/'.$standard;
1413-
return (is_file("$standardDir/{$standard}CodingStandard.php") === true);
1435+
if (is_file("$standardDir/{$standard}CodingStandard.php") === true) {
1436+
return true;
1437+
} else {
1438+
// This could be a custom standard, installed outside our
1439+
// standards directory.
1440+
$standardFile = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.basename($standard).'CodingStandard.php';
1441+
return (is_file($standardFile) === true);
1442+
}
14141443

14151444
}//end isInstalledStandard()
14161445

package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
3232
</stability>
3333
<license uri="http://matrix.squiz.net/developer/tools/php_cs/licence">BSD License</license>
3434
<notes>
35+
- You can now specify the full path to a coding standard on the command line (feature request #11886)
36+
- This allows you to use standards that are stored outside of PHP_CodeSniffer's own Standard dir
37+
- You can also specify full paths in the CodingStandard.php include and exclude methods
38+
- Classes, dirs and files need to be names as if the standard was part of PHP_CodeSniffer
3539
- Modified the scope map to keep checking after 3 lines for some tokens (feature request #12561)
3640
-- Those tokens that must have an opener (like T_CLASS) now keep looking until EOF
3741
-- Other tokens (like T_FUNCTION) still stop after 3 lines for performance

scripts/phpcs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ function PHP_CodeSniffer_printInstalledStandards()
9090
}//end PHP_CodeSniffer_printInstalledStandards()
9191

9292

93-
// First, we need to ensure there is at least one coding standard
94-
// installed, or they will never be able to run this script.
95-
$installedStandards = PHP_CodeSniffer::getInstalledStandards();
96-
if (count($installedStandards) === 0) {
97-
echo 'ERROR: There are no coding standards installed.'.PHP_EOL;
98-
exit(2);
99-
}
100-
101-
10293
// The default values for config settings.
10394
$files = array();
10495
$standard = null;
@@ -253,17 +244,13 @@ foreach ($files as $file) {
253244
}
254245
}
255246

256-
if (count($installedStandards) > 1) {
247+
if ($standard === null) {
248+
// They did not supply a standard to use.
249+
// Try to get the default from the config system.
250+
$standard = PHP_CodeSniffer::getConfigData('default_standard');
257251
if ($standard === null) {
258-
// They did not supply a standard to use.
259-
// Try to get the default from the config system.
260-
$standard = PHP_CodeSniffer::getConfigData('default_standard');
261-
if ($standard === null) {
262-
$standard = 'PEAR';
263-
}
252+
$standard = 'PEAR';
264253
}
265-
} else {
266-
$standard = array_pop($installedStandards);
267254
}
268255

269256
// Check if the standard name is valid. If not, check that the case

0 commit comments

Comments
 (0)