From 8354e05e1d667872b763dc9177b87a86952ba098 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Tue, 27 Feb 2024 21:34:45 +0100 Subject: [PATCH] Delegate callable array handler to a utility class --- .../php/PHPMD/Rule/UnusedPrivateMethod.php | 31 +------- src/main/php/PHPMD/Utility/CallableArray.php | 79 +++++++++++++++++++ src/main/php/PHPMD/Utility/Seeker.php | 1 + 3 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 src/main/php/PHPMD/Utility/CallableArray.php diff --git a/src/main/php/PHPMD/Rule/UnusedPrivateMethod.php b/src/main/php/PHPMD/Rule/UnusedPrivateMethod.php index 49d0c08f2..8f2f7e61b 100644 --- a/src/main/php/PHPMD/Rule/UnusedPrivateMethod.php +++ b/src/main/php/PHPMD/Rule/UnusedPrivateMethod.php @@ -25,6 +25,7 @@ use PHPMD\Node\ASTNode; use PHPMD\Node\ClassNode; use PHPMD\Node\MethodNode; +use PHPMD\Utility\CallableArray; use PHPMD\Utility\LastVariableWriting; use PHPMD\Utility\Seeker; use SplObjectStorage; @@ -162,7 +163,8 @@ protected function removeCallableArrayRepresentations(ClassNode $class, array $m { foreach ($class->findChildrenOfTypeVariable() as $variable) { if ($this->isInstanceOfTheCurrentClass($class, $variable)) { - $method = $this->getMethodNameFromArraySecondElement($variable->getParent()); + $method = CallableArray::fromFirstArrayElement($variable->getParent()) + ->getMethodNameFromArraySecondElement(); if ($method) { unset($methods[strtolower($method)]); @@ -173,33 +175,6 @@ protected function removeCallableArrayRepresentations(ClassNode $class, array $m return $methods; } - /** - * Return represented method name if the given element is a 2-items array - * and that the second one is a literal static string. - * - * @param ASTNode|null $parent - * @return string|null - */ - protected function getMethodNameFromArraySecondElement($parent) - { - if ($parent instanceof ASTNode && $parent->isInstanceOf('ArrayElement')) { - $array = $parent->getParent(); - - if ($array instanceof ASTNode - && $array->isInstanceOf('Array') - && count($array->getChildren()) === 2 - ) { - $secondElement = $array->getChild(1)->getChild(0); - - if ($secondElement->isInstanceOf('Literal')) { - return substr($secondElement->getImage(), 1, -1); - } - } - } - - return null; - } - /** * This method checks that the given method postfix is accessed on an * instance or static reference to the given class. diff --git a/src/main/php/PHPMD/Utility/CallableArray.php b/src/main/php/PHPMD/Utility/CallableArray.php new file mode 100644 index 000000000..e951d741a --- /dev/null +++ b/src/main/php/PHPMD/Utility/CallableArray.php @@ -0,0 +1,79 @@ +. + * All rights reserved. + * + * Licensed under BSD License + * For full copyright and license information, please see the LICENSE file. + * Redistributions of files must retain the above copyright notice. + * + * @author Manuel Pichler + * @copyright Manuel Pichler. All rights reserved. + * @license https://opensource.org/licenses/bsd-license.php BSD License + * @link http://phpmd.org/ + */ + +namespace PHPMD\Utility; + +use PHPMD\Node\ASTNode; + +/** + * Utility class to check and read array possibly representing callable method. + */ +final class CallableArray +{ + /** @var ASTNode|null */ + private $array; + + // TODO: should be (?ASTNode $array) when dropping PHP < 7.1 + private function __construct(ASTNode $array = null) + { + $this->array = $array; + } + + /** @return self */ + public static function fromArray($array) + { + if ($array instanceof ASTNode + && $array->isInstanceOf('Array') + && count($array->getChildren()) === 2 + ) { + return new self($array); + } + + return new self(null); + } + + /** @return self */ + public static function fromFirstArrayElement($firstArrayElement) + { + if ($firstArrayElement instanceof ASTNode && $firstArrayElement->isInstanceOf('ArrayElement')) { + return self::fromArray($firstArrayElement->getParent()); + } + + return new self(null); + } + + /** + * Return represented method name if the given element is a 2-items array + * and that the second one is a literal static string. + * + * @return string|null + */ + public function getMethodNameFromArraySecondElement() + { + if ($this->array === null) { + return null; + } + + $secondElement = $this->array->getChild(1)->getChild(0); + + if ($secondElement->isInstanceOf('Literal')) { + return substr($secondElement->getImage(), 1, -1); + } + + return null; + } +} diff --git a/src/main/php/PHPMD/Utility/Seeker.php b/src/main/php/PHPMD/Utility/Seeker.php index b827ec782..3cec2049c 100644 --- a/src/main/php/PHPMD/Utility/Seeker.php +++ b/src/main/php/PHPMD/Utility/Seeker.php @@ -29,6 +29,7 @@ */ final class Seeker { + /** @var ASTNode */ private $node; private function __construct(ASTNode $node)