-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delegate callable array handler to a utility class
- Loading branch information
1 parent
609b314
commit 8354e05
Showing
3 changed files
with
83 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters