Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Generic DuplicateClassNameSniff is no longer a multi-file sniff, so i…

…t won't max out your memory
  • Loading branch information...
commit 371df28a42934351bc9860c236e895de0e86c828 1 parent f25f73d
@gsherwood gsherwood authored
View
115 CodeSniffer/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php
@@ -23,71 +23,86 @@
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
-class Generic_Sniffs_Classes_DuplicateClassNameSniff implements PHP_CodeSniffer_MultiFileSniff
+class Generic_Sniffs_Classes_DuplicateClassNameSniff implements PHP_CodeSniffer_Sniff
{
+ /**
+ * List of classes that have been found during checking.
+ *
+ * @var array
+ */
+ public $foundClasses = array();
+
+
+ /**
+ * Registers the tokens that this sniff wants to listen for.
+ *
+ * @return array(integer)
+ */
+ public function register()
+ {
+ return array(T_OPEN_TAG);
+
+ }//end register()
+
/**
- * Called once per script run to allow for processing of this sniff.
+ * Processes this test, when one of its tokens is encountered.
*
- * @param array(PHP_CodeSniffer_File) $files The PHP_CodeSniffer files processed
- * during the script run.
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token
+ * in the stack passed in $tokens.
*
* @return void
*/
- public function process(array $files)
+ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
- $foundClasses = array();
+ $tokens = $phpcsFile->getTokens();
- foreach ($files as $phpcsFile) {
- $tokens = $phpcsFile->getTokens();
+ $namespace = '';
+ $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), 0);
+ while ($stackPtr !== false) {
+ // Keep track of what namespace we are in.
+ if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
+ $nsEnd = $phpcsFile->findNext(
+ array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE),
+ ($stackPtr + 1),
+ null,
+ true
+ );
- $namespace = '';
- $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), 0);
- while ($stackPtr !== false) {
- // Keep track of what namespace we are in.
- if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
- $nsEnd = $phpcsFile->findNext(
- array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE),
- ($stackPtr + 1),
- null,
- true
- );
+ $namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
+ $stackPtr = $nsEnd;
+ } else {
+ $nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
+ $name = $tokens[$nameToken]['content'];
+ if ($namespace !== '') {
+ $name = $namespace.'\\'.$name;
+ }
- $namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
- $stackPtr = $nsEnd;
+ $compareName = strtolower($name);
+ if (isset($this->foundClasses[$compareName]) === true) {
+ $type = strtolower($tokens[$stackPtr]['content']);
+ $file = $this->foundClasses[$compareName]['file'];
+ $line = $this->foundClasses[$compareName]['line'];
+ $error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
+ $data = array(
+ $type,
+ $name,
+ $file,
+ $line,
+ );
+ $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
} else {
- $nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
- $name = $tokens[$nameToken]['content'];
- if ($namespace !== '') {
- $name = $namespace.'\\'.$name;
- }
-
- $compareName = strtolower($name);
- if (isset($foundClasses[$compareName]) === true) {
- $type = strtolower($tokens[$stackPtr]['content']);
- $file = $foundClasses[$compareName]['file'];
- $line = $foundClasses[$compareName]['line'];
- $error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
- $data = array(
- $type,
- $name,
- $file,
- $line,
- );
- $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
- } else {
- $foundClasses[$compareName] = array(
- 'file' => $phpcsFile->getFilename(),
- 'line' => $tokens[$stackPtr]['line'],
- );
- }
+ $this->foundClasses[$compareName] = array(
+ 'file' => $phpcsFile->getFilename(),
+ 'line' => $tokens[$stackPtr]['line'],
+ );
}
+ }
- $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), ($stackPtr + 1));
- }//end while
-
- }//end foreach
+ $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), ($stackPtr + 1));
+ }//end while
}//end process()
View
2  package.xml
@@ -40,6 +40,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Sniffs inside PHP 5.3 namespaces are now supported, along with the existing underscore-style emulated namespaces
-- For example: namespace MyStandard\Sniffs\Arrays; class ArrayDeclarationSniff implements \PHP_CodeSniffer_Sniff { ...
-- Thanks to Till Klampaeckel for the patch
+ - Generic DuplicateClassNameSniff is no longer a multi-file sniff, so it won't max out your memory
+ -- Multi-file sniff support should be considered deprecated as standard sniffs can now do the same thing
- Added Generic DisallowSpaceIndent to check that files are indented using tabs
- Added Generic OneClassPerFileSniff to check that only one class is defined in each file
-- Thanks to Andy Grunwald for the contribution
View
44 tests/Standards/AbstractSniffUnitTest.php
@@ -116,31 +116,20 @@ protected function shouldSkipTest()
}
}
- // Get them in order. This is particularly important for multi-file sniffs.
+ // Get them in order.
sort($testFiles);
+ self::$phpcs->process(array(), $standardName, array($sniffClass));
+ self::$phpcs->setIgnorePatterns(array());
+
$failureMessages = array();
- $multiFileSniff = false;
foreach ($testFiles as $testFile) {
try {
- self::$phpcs->process(array(), $standardName, array($sniffClass));
- self::$phpcs->setIgnorePatterns(array());
self::$phpcs->processFile($testFile);
- self::$phpcs->processMulti();
} catch (Exception $e) {
$this->fail('An unexpected exception has been caught: '.$e->getMessage());
}
- // After processing a file, check if the sniff was actually
- // a multi-file sniff (i.e., had no individual file sniffs).
- // If it is, we can skip checking of the other files and
- // do a single multi-file check.
- $sniffs = self::$phpcs->getTokenSniffs();
- if (empty($sniffs['file']) === true) {
- $multiFileSniff = true;
- break;
- }
-
$files = self::$phpcs->getFiles();
if (empty($files) === true) {
// File was skipped for some reason.
@@ -154,31 +143,6 @@ protected function shouldSkipTest()
$failureMessages = array_merge($failureMessages, $failures);
}//end foreach
- if ($multiFileSniff === true) {
- try {
- self::$phpcs->process(array(), $standardName, array($sniffClass));
- self::$phpcs->setIgnorePatterns(array());
- foreach ($testFiles as $testFile) {
- self::$phpcs->processFile($testFile);
- }
-
- self::$phpcs->processMulti();
- } catch (Exception $e) {
- $this->fail('An unexpected exception has been caught: '.$e->getMessage());
- }
-
- $files = self::$phpcs->getFiles();
- if (empty($files) === true) {
- // File was skipped for some reason.
- $this->markTestSkipped();
- } else {
- foreach ($files as $file) {
- $failures = $this->generateFailureMessages($file);
- $failureMessages = array_merge($failureMessages, $failures);
- }
- }
- }//end if
-
if (empty($failureMessages) === false) {
$this->fail(implode(PHP_EOL, $failureMessages));
}
Please sign in to comment.
Something went wrong with that request. Please try again.