forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for multi-file sniffs, which get a list of all sniffed …
…files in a run for checking. Using these sniffs dramatically increases the memory requirements of the scirpt as all the token arrays are left around in memory for later use. There may be an alternative way of doing this, but I also don't want to slow the multi-file sniffs down too much by re-parsing everything; but I may have to. I've also added a sample multi-file sniff and its new unit tests (a specific strucutre is required for these) that checks if the same class name is used multiple times throughout your files. Could be useful if you check your whole project, but mostly just a sample. git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@263478 c90b9560-bf6c-de11-be94-00142212c4b1
- Loading branch information
Showing
9 changed files
with
474 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Represents a PHP_CodeSniffer sniff for sniffing coding standards. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version CVS: $Id$ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Represents a PHP_CodeSniffer multi-file sniff for sniffing coding standards. | ||
* | ||
* A multi-file sniff is called after all files have been checked using the | ||
* regular sniffs. The process() method is passed an array of PHP_CodeSniffer_File | ||
* objects, one for each file checked during the script run. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
interface PHP_CodeSniffer_MultiFileSniff | ||
{ | ||
|
||
|
||
/** | ||
* Called once per script run to allow for processing of this sniff. | ||
* | ||
* @param array(PHP_CodeSniffer_File) $files The PHP_CodeSniffer files processed | ||
* during the script run. | ||
* | ||
* @return void | ||
*/ | ||
public function process(array $files); | ||
|
||
|
||
}//end interface | ||
|
||
?> |
74 changes: 74 additions & 0 deletions
74
CodeSniffer/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* Reports errors if the same class or interface name is used in multiple files. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version CVS: $Id$ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Reports errors if the same class or interface name is used in multiple files. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Generic_Sniffs_Classes_DuplicateClassNameSniff implements PHP_CodeSniffer_MultiFileSniff | ||
{ | ||
|
||
|
||
/** | ||
* Called once per script run to allow for processing of this sniff. | ||
* | ||
* @param array(PHP_CodeSniffer_File) $files The PHP_CodeSniffer files processed | ||
* during the script run. | ||
* | ||
* @return void | ||
*/ | ||
public function process(array $files) | ||
{ | ||
$foundClasses = array(); | ||
|
||
foreach ($files as $phpcsFile) { | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), 0); | ||
while ($stackPtr !== false) { | ||
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr); | ||
$name = $tokens[$nameToken]['content']; | ||
$compareName = strtolower($name); | ||
if (isset($foundClasses[$compareName]) === true) { | ||
$type = strtolower($tokens[$stackPtr]['content']); | ||
$file = $foundClasses[$compareName]['file']; | ||
$line = $foundClasses[$compareName]['line']; | ||
$error = "Duplicate $type name \"$name\" found; first defined in $file on line $line"; | ||
$phpcsFile->addWarning($error, $stackPtr); | ||
} else { | ||
$foundClasses[$compareName] = array( | ||
'file' => $phpcsFile->getFilename(), | ||
'line' => $tokens[$stackPtr]['line'], | ||
); | ||
} | ||
|
||
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1)); | ||
}//end while | ||
|
||
}//end foreach | ||
|
||
}//end process() | ||
|
||
|
||
}//end class | ||
|
||
?> |
8 changes: 8 additions & 0 deletions
8
CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.1.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
class MyClass {} | ||
class YourClass {} | ||
interface MyInterface {} | ||
interface YourInterface {} | ||
class MyClass {} | ||
interface MyInterface {} | ||
?> |
4 changes: 4 additions & 0 deletions
4
CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.2.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
class MyClass {} | ||
interface MyInterface {} | ||
?> |
82 changes: 82 additions & 0 deletions
82
CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Unit test class for the DuplicateClassName multi-file sniff. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version CVS: $Id$ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Unit test class for the DuplicateClassName multi-file sniff. | ||
* | ||
* A multi-file sniff unit test checks a .1.inc and a .2.inc file for expected violations | ||
* of a single coding standard. Expected errors and warnings are stored in this class. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Generic_Tests_Classes_DuplicateClassNameUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array(int => int) | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return array(); | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array(int => int) | ||
*/ | ||
public function getWarningList($testFile='') | ||
{ | ||
switch ($testFile) { | ||
case 'DuplicateClassNameUnitTest.1.inc': | ||
return array( | ||
6 => 1, | ||
7 => 1, | ||
); | ||
break; | ||
case 'DuplicateClassNameUnitTest.2.inc': | ||
return array( | ||
2 => 1, | ||
3 => 1, | ||
); | ||
break; | ||
default: | ||
return array(); | ||
break; | ||
}//end switch | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class | ||
|
||
?> |
Oops, something went wrong.