Skip to content

File: add new File::findExtendedInterfaceNames() utility method #2128

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="" name="FindEndOfStatementTest.php" role="test" />
<file baseinstalldir="" name="FindExtendedClassNameTest.inc" role="test" />
<file baseinstalldir="" name="FindExtendedClassNameTest.php" role="test" />
<file baseinstalldir="" name="FindExtendedInterfaceNamesTest.inc" role="test" />
<file baseinstalldir="" name="FindExtendedInterfaceNamesTest.php" role="test" />
<file baseinstalldir="" name="FindImplementedInterfaceNamesTest.inc" role="test" />
<file baseinstalldir="" name="FindImplementedInterfaceNamesTest.php" role="test" />
<file baseinstalldir="" name="GetMemberPropertiesTest.inc" role="test" />
Expand Down
150 changes: 97 additions & 53 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2355,109 +2355,153 @@ public function getCondition($stackPtr, $type)

/**
* Returns the name of the class that the specified class extends.
* (works for classes, anonymous classes and interfaces)
*
* Works for classes, anonymous classes and interfaces, though it is
* strongly recommended to use the findExtendedInterfaceNames() method
* to examine interfaces as they can extend multiple parent interfaces.
*
* Returns FALSE on error or if there is no extended class name.
*
* @param int $stackPtr The stack position of the class.
* @param int $stackPtr The stack position of the class keyword.
*
* @return string|false
*/
public function findExtendedClassName($stackPtr)
{
// Check for the existence of the token.
if (isset($this->tokens[$stackPtr]) === false) {
return false;
}
$validStructures = [
T_CLASS => true,
T_ANON_CLASS => true,
T_INTERFACE => true,
];

if ($this->tokens[$stackPtr]['code'] !== T_CLASS
&& $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS
&& $this->tokens[$stackPtr]['code'] !== T_INTERFACE
) {
return false;
}
$classes = $this->examineObjectDeclarationSignature($stackPtr, $validStructures, T_EXTENDS);

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

$classOpenerIndex = $this->tokens[$stackPtr]['scope_opener'];
$extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex);
if (false === $extendsIndex) {
return false;
}
// Classes can only extend one parent class.
return $classes[0];

$find = [
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
];
}//end findExtendedClassName()

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

if ($name === '') {
/**
* Returns the names of the interfaces that the specified interface extends.
*
* Returns FALSE on error or if there is no extended interface name.
*
* @param int $stackPtr The stack position of the interface keyword.
*
* @return array|false
*/
public function findExtendedInterfaceNames($stackPtr)
{
$validStructures = [T_INTERFACE => true];

$interfaces = $this->examineObjectDeclarationSignature($stackPtr, $validStructures, T_EXTENDS);

if (empty($interfaces) === true) {
return false;
}

return $name;
return $interfaces;

}//end findExtendedClassName()
}//end findExtendedInterfaceNames()


/**
* Returns the names of the interfaces that the specified class implements.
*
* Returns FALSE on error or if there are no implemented interface names.
*
* @param int $stackPtr The stack position of the class.
* @param int $stackPtr The stack position of the class keyword.
*
* @return array|false
*/
public function findImplementedInterfaceNames($stackPtr)
{
$validStructures = [
T_CLASS => true,
T_ANON_CLASS => true,
];

$interfaces = $this->examineObjectDeclarationSignature($stackPtr, $validStructures, T_IMPLEMENTS);

if (empty($interfaces) === true) {
return false;
}

return $interfaces;

}//end findImplementedInterfaceNames()


/**
* Returns the names of the extended classes or interfaces or the implemented
* interfaces that the specific class/interface declaration extends/implements.
*
* Returns FALSE on error or if the object does not extend/implement another
* object.
*
* @param int $stackPtr The stack position of the class/interface keyword.
* @param array $OOTypes Array of accepted token types.
* Array format <token constant> => true.
* @param int $keyword The keyword to examine. Either `T_EXTENDS` or `T_IMPLEMENTS`.
*
* @return array|false
*/
private function examineObjectDeclarationSignature($stackPtr, $OOTypes, $keyword)
{
// Check for the existence of the token.
if (isset($this->tokens[$stackPtr]) === false) {
return false;
}

if ($this->tokens[$stackPtr]['code'] !== T_CLASS
&& $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS
) {
if (isset($OOTypes[$this->tokens[$stackPtr]['code']]) === false) {
return false;
}

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

$classOpenerIndex = $this->tokens[$stackPtr]['scope_opener'];
$implementsIndex = $this->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex);
if ($implementsIndex === false) {
$openerIndex = $this->tokens[$stackPtr]['scope_opener'];
$keywordIndex = $this->findNext($keyword, ($stackPtr + 1), $openerIndex);
if ($keywordIndex === false) {
return false;
}

$find = [
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
T_COMMA,
];
$find = Util\Tokens::$emptyTokens;
$find[] = T_NS_SEPARATOR;
$find[] = T_STRING;
$find[] = T_COMMA;

$end = $this->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true);
$name = $this->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1));
$name = trim($name);
$end = $this->findNext($find, ($keywordIndex + 1), ($openerIndex + 1), true);
$names = [];
$name = '';
for ($i = ($keywordIndex + 1); $i < $end; $i++) {
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === true) {
continue;
}

if ($name === '') {
return false;
} else {
$names = explode(',', $name);
$names = array_map('trim', $names);
return $names;
if ($this->tokens[$i]['code'] === T_COMMA && $name !== '') {
$names[] = $name;
$name = '';
continue;
}

$name .= $this->tokens[$i]['content'];
}

}//end findImplementedInterfaceNames()
// Add the last name.
if ($name !== '') {
$names[] = $name;
}

return $names;

}//end examineObjectDeclarationSignature()


}//end class
2 changes: 2 additions & 0 deletions tests/Core/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_once 'ErrorSuppressionTest.php';
require_once 'File/FindEndOfStatementTest.php';
require_once 'File/FindExtendedClassNameTest.php';
require_once 'File/FindExtendedInterfaceNamesTest.php';
require_once 'File/FindImplementedInterfaceNamesTest.php';
require_once 'File/GetMemberPropertiesTest.php';
require_once 'File/GetMethodParametersTest.php';
Expand Down Expand Up @@ -50,6 +51,7 @@ public static function suite()
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\ErrorSuppressionTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindEndOfStatementTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindExtendedClassNameTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindExtendedInterfaceNamesTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindImplementedInterfaceNamesTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMemberPropertiesTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodParametersTest');
Expand Down
31 changes: 31 additions & 0 deletions tests/Core/File/FindExtendedInterfaceNamesTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/* @codingStandardsIgnoreFile */

namespace PHP_CodeSniffer\Tests\Core\File;

interface testFEINInterface2 {}

/* testInterface */
interface testFEINInterface {}

/* testExtendedInterface */
interface testFEINExtendedInterface extends testFEINInterface {}

/* testMultiExtendedInterface */
interface testFEINMultiExtendedInterface extends testFEINInterface, testFEINInterface2 {}

/* testNamespacedInterface */
interface testFEINNamespacedInterface extends \PHP_CodeSniffer\Tests\Core\File\testFEINInterface {}

/* testMultiNamespacedInterface */
interface testFEINMultiNamespacedInterface extends \PHP_CodeSniffer\Tests\Core\File\testFEINInterface, \PHP_CodeSniffer\Tests\Core\File\testFEINInterface2 {}

/* testMultiExtendedInterfaceWithComment */
interface testFEINMultiExtendedInterface
extends
/* a comment */
testFEINInterface,
\PHP_CodeSniffer\Tests /* comment */ \Core \ File \testFEINInterface2,
\testFEINInterface3 /* comment */
{
}
Loading