Skip to content

Commit a2a92f9

Browse files
committed
Merge branch 'feature/bugfix-find-extended-class' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents f2cea38 + a3232a6 commit a2a92f9

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/Files/File.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,12 +2377,12 @@ public function findExtendedClassName($stackPtr)
23772377
return false;
23782378
}
23792379

2380-
if (isset($this->tokens[$stackPtr]['scope_closer']) === false) {
2380+
if (isset($this->tokens[$stackPtr]['scope_opener']) === false) {
23812381
return false;
23822382
}
23832383

2384-
$classCloserIndex = $this->tokens[$stackPtr]['scope_closer'];
2385-
$extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classCloserIndex);
2384+
$classOpenerIndex = $this->tokens[$stackPtr]['scope_opener'];
2385+
$extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex);
23862386
if (false === $extendsIndex) {
23872387
return false;
23882388
}
@@ -2393,7 +2393,7 @@ public function findExtendedClassName($stackPtr)
23932393
T_WHITESPACE,
23942394
];
23952395

2396-
$end = $this->findNext($find, ($extendsIndex + 1), $classCloserIndex, true);
2396+
$end = $this->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true);
23972397
$name = $this->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1));
23982398
$name = trim($name);
23992399

tests/Core/File/FindExtendedClassNameTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ interface testInterfaceThatExtendsInterface extends testFECNInterface{}
2222

2323
/* testInterfaceThatExtendsFQCNInterface */
2424
interface testInterfaceThatExtendsFQCNInterface extends \PHP_CodeSniffer\Tests\Core\File\testFECNInterface{}
25+
26+
/* testNestedExtendedClass */
27+
class testFECNNestedExtendedClass {
28+
public function someMethod() {
29+
/* testNestedExtendedAnonClass */
30+
$anon = new class extends testFECNAnonClass {};
31+
}
32+
}

tests/Core/File/FindExtendedClassNameTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,49 @@ public function testExtendedNamespacedInterface()
191191
}//end testExtendedNamespacedInterface()
192192

193193

194+
/**
195+
* Test a non-extended class with a nested class which does extend another class.
196+
*
197+
* @return void
198+
*/
199+
public function testNestedExtendedClass()
200+
{
201+
$start = ($this->phpcsFile->numTokens - 1);
202+
$class = $this->phpcsFile->findPrevious(
203+
T_COMMENT,
204+
$start,
205+
null,
206+
false,
207+
'/* testNestedExtendedClass */'
208+
);
209+
210+
$found = $this->phpcsFile->findExtendedClassName(($class + 2));
211+
$this->assertFalse($found);
212+
213+
}//end testNestedExtendedClass()
214+
215+
216+
/**
217+
* Test a nested anonymous class that extends a named class.
218+
*
219+
* @return void
220+
*/
221+
public function testNestedExtendedAnonClass()
222+
{
223+
$start = ($this->phpcsFile->numTokens - 1);
224+
$class = $this->phpcsFile->findPrevious(
225+
T_COMMENT,
226+
$start,
227+
null,
228+
false,
229+
'/* testNestedExtendedAnonClass */'
230+
);
231+
$class = $this->phpcsFile->findNext(T_ANON_CLASS, ($class + 1));
232+
233+
$found = $this->phpcsFile->findExtendedClassName($class);
234+
$this->assertSame('testFECNAnonClass', $found);
235+
236+
}//end testNestedExtendedAnonClass()
237+
238+
194239
}//end class

0 commit comments

Comments
 (0)