Skip to content

Commit a7d8693

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 3dd91e2 commit a7d8693

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
@@ -83,31 +83,32 @@ public function process(File $phpcsFile, $stackPtr)
8383
$stackPtr = $i;
8484
}
8585
} else {
86-
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
87-
$name = $tokens[$nameToken]['content'];
88-
if ($namespace !== '') {
89-
$name = $namespace.'\\'.$name;
90-
}
86+
$name = $phpcsFile->getDeclarationName($stackPtr);
87+
if (empty($name) === false) {
88+
if ($namespace !== '') {
89+
$name = $namespace.'\\'.$name;
90+
}
9191

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

113114
$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)