Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File::findExtendedClassName() doesn't support nested classes #2127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2377,12 +2377,12 @@ public function findExtendedClassName($stackPtr)
return false;
}

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

$classCloserIndex = $this->tokens[$stackPtr]['scope_closer'];
$extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classCloserIndex);
$classOpenerIndex = $this->tokens[$stackPtr]['scope_opener'];
$extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex);
if (false === $extendsIndex) {
return false;
}
Expand All @@ -2393,7 +2393,7 @@ public function findExtendedClassName($stackPtr)
T_WHITESPACE,
];

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

Expand Down
8 changes: 8 additions & 0 deletions tests/Core/File/FindExtendedClassNameTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ interface testInterfaceThatExtendsInterface extends testFECNInterface{}

/* testInterfaceThatExtendsFQCNInterface */
interface testInterfaceThatExtendsFQCNInterface extends \PHP_CodeSniffer\Tests\Core\File\testFECNInterface{}

/* testNestedExtendedClass */
class testFECNNestedExtendedClass {
public function someMethod() {
/* testNestedExtendedAnonClass */
$anon = new class extends testFECNAnonClass {};
}
}
45 changes: 45 additions & 0 deletions tests/Core/File/FindExtendedClassNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,49 @@ public function testExtendedNamespacedInterface()
}//end testExtendedNamespacedInterface()


/**
* Test a non-extended class with a nested class which does extend another class.
*
* @return void
*/
public function testNestedExtendedClass()
{
$start = ($this->phpcsFile->numTokens - 1);
$class = $this->phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testNestedExtendedClass */'
);

$found = $this->phpcsFile->findExtendedClassName(($class + 2));
$this->assertFalse($found);

}//end testNestedExtendedClass()


/**
* Test a nested anonymous class that extends a named class.
*
* @return void
*/
public function testNestedExtendedAnonClass()
{
$start = ($this->phpcsFile->numTokens - 1);
$class = $this->phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testNestedExtendedAnonClass */'
);
$class = $this->phpcsFile->findNext(T_ANON_CLASS, ($class + 1));

$found = $this->phpcsFile->findExtendedClassName($class);
$this->assertSame('testFECNAnonClass', $found);

}//end testNestedExtendedAnonClass()


}//end class