Skip to content

Commit 2e98c74

Browse files
committed
Generic/DuplicateClassName: use helper method
Use the `File::getDeclarationName()` method instead of finding the name of an OO structure in the sniff itself. This also prevents a potential situation where if a class name could not be found, the `findNext()` method would return `false`, which would then be used in the `$tokens[$nameToken]['content']` leading to `false` being juggled to `0` and a class name of `<?php` being stored in the class name cache. Includes tests.
1 parent 71cf8dc commit 2e98c74

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,32 @@ public function process(File $phpcsFile, $stackPtr)
8484
$stackPtr = $i;
8585
}
8686
} else {
87-
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
88-
$name = $tokens[$nameToken]['content'];
89-
if ($namespace !== '') {
90-
$name = $namespace.'\\'.$name;
91-
}
87+
$name = $phpcsFile->getDeclarationName($stackPtr);
88+
if (empty($name) === false) {
89+
if ($namespace !== '') {
90+
$name = $namespace.'\\'.$name;
91+
}
9292

93-
$compareName = strtolower($name);
94-
if (isset($this->foundClasses[$compareName]) === true) {
95-
$type = strtolower($tokens[$stackPtr]['content']);
96-
$file = $this->foundClasses[$compareName]['file'];
97-
$line = $this->foundClasses[$compareName]['line'];
98-
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
99-
$data = [
100-
$type,
101-
$name,
102-
$file,
103-
$line,
104-
];
105-
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
106-
} else {
107-
$this->foundClasses[$compareName] = [
108-
'file' => $phpcsFile->getFilename(),
109-
'line' => $tokens[$stackPtr]['line'],
110-
];
111-
}
93+
$compareName = strtolower($name);
94+
if (isset($this->foundClasses[$compareName]) === true) {
95+
$type = strtolower($tokens[$stackPtr]['content']);
96+
$file = $this->foundClasses[$compareName]['file'];
97+
$line = $this->foundClasses[$compareName]['line'];
98+
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
99+
$data = [
100+
$type,
101+
$name,
102+
$file,
103+
$line,
104+
];
105+
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
106+
} else {
107+
$this->foundClasses[$compareName] = [
108+
'file' => $phpcsFile->getFilename(),
109+
'line' => $tokens[$stackPtr]['line'],
110+
];
111+
}
112+
}//end if
112113
}//end if
113114

114115
$stackPtr = $phpcsFile->findNext($findTokens, ($stackPtr + 1));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error/live coding.
4+
// This should be the only test in this file.
5+
// This is a two-file test: test case file 97 and 98 belong together.
6+
// Testing against false positives during live coding and invalid class names being stored in the cache.
7+
8+
class
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error/live coding.
4+
// This should be the only test in this file.
5+
// This is a two-file test: test case file 97 and 98 belong together.
6+
// Testing against false positives during live coding and invalid class names being stored in the cache.
7+
8+
class

0 commit comments

Comments
 (0)