Skip to content

Commit

Permalink
Delegate callable array handler to a utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Feb 27, 2024
1 parent 609b314 commit 8354e05
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 28 deletions.
31 changes: 3 additions & 28 deletions src/main/php/PHPMD/Rule/UnusedPrivateMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]);
Expand All @@ -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.
Expand Down
79 changes: 79 additions & 0 deletions src/main/php/PHPMD/Utility/CallableArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* This file is part of PHP Mess Detector.
*
* Copyright (c) Manuel Pichler <mapi@phpmd.org>.
* 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 <mapi@phpmd.org>
* @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;
}
}
1 change: 1 addition & 0 deletions src/main/php/PHPMD/Utility/Seeker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
final class Seeker
{
/** @var ASTNode */
private $node;

private function __construct(ASTNode $node)
Expand Down

0 comments on commit 8354e05

Please sign in to comment.