2020-02-09 12:31:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-09 23:47:00 +01:00
|
|
|
namespace Rector\NodeNameResolver;
|
2020-02-09 12:31:31 +01:00
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2020-02-21 15:54:51 +01:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2020-02-29 21:50:29 +01:00
|
|
|
use PhpParser\Node\Name;
|
2020-02-09 12:31:31 +01:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
|
|
|
use PhpParser\Node\Stmt\Trait_;
|
2020-05-14 12:26:57 +02:00
|
|
|
use Rector\Core\Contract\Rector\RectorInterface;
|
2020-02-21 15:54:51 +01:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2020-05-01 11:00:21 +02:00
|
|
|
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
2020-02-09 23:47:00 +01:00
|
|
|
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
|
2020-02-13 11:09:51 +01:00
|
|
|
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
|
2020-05-01 11:00:21 +02:00
|
|
|
use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider;
|
|
|
|
use Symplify\SmartFileSystem\SmartFileInfo;
|
2020-02-09 12:31:31 +01:00
|
|
|
|
|
|
|
final class NodeNameResolver
|
|
|
|
{
|
2020-06-08 17:06:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-08 19:14:09 +02:00
|
|
|
private const FILE = 'file';
|
2020-06-08 17:06:58 +02:00
|
|
|
|
2020-02-13 11:09:51 +01:00
|
|
|
/**
|
2020-04-26 02:57:47 +02:00
|
|
|
* @var NodeNameResolverInterface[]
|
2020-02-13 11:09:51 +01:00
|
|
|
*/
|
2020-04-26 02:57:47 +02:00
|
|
|
private $nodeNameResolvers = [];
|
2020-02-13 11:09:51 +01:00
|
|
|
|
2020-02-09 12:31:31 +01:00
|
|
|
/**
|
2020-04-26 02:57:47 +02:00
|
|
|
* @var RegexPatternDetector
|
2020-02-09 12:31:31 +01:00
|
|
|
*/
|
2020-04-26 02:57:47 +02:00
|
|
|
private $regexPatternDetector;
|
2020-02-09 12:31:31 +01:00
|
|
|
|
2020-05-01 11:00:21 +02:00
|
|
|
/**
|
|
|
|
* @var CurrentFileInfoProvider
|
|
|
|
*/
|
|
|
|
private $currentFileInfoProvider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BetterStandardPrinter
|
|
|
|
*/
|
|
|
|
private $betterStandardPrinter;
|
|
|
|
|
2020-02-09 12:31:31 +01:00
|
|
|
/**
|
|
|
|
* @param NodeNameResolverInterface[] $nodeNameResolvers
|
|
|
|
*/
|
2020-05-01 11:00:21 +02:00
|
|
|
public function __construct(
|
|
|
|
RegexPatternDetector $regexPatternDetector,
|
|
|
|
BetterStandardPrinter $betterStandardPrinter,
|
|
|
|
CurrentFileInfoProvider $currentFileInfoProvider,
|
|
|
|
array $nodeNameResolvers = []
|
|
|
|
) {
|
2020-02-13 11:09:51 +01:00
|
|
|
$this->regexPatternDetector = $regexPatternDetector;
|
2020-02-09 12:31:31 +01:00
|
|
|
$this->nodeNameResolvers = $nodeNameResolvers;
|
2020-05-01 11:00:21 +02:00
|
|
|
$this->currentFileInfoProvider = $currentFileInfoProvider;
|
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $names
|
|
|
|
*/
|
|
|
|
public function isNames(Node $node, array $names): bool
|
|
|
|
{
|
|
|
|
foreach ($names as $name) {
|
|
|
|
if ($this->isName($node, $name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isName(Node $node, string $name): bool
|
|
|
|
{
|
|
|
|
if ($node instanceof MethodCall) {
|
|
|
|
// method call cannot have a name, only the variable or method name
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolvedName = $this->getName($node);
|
|
|
|
if ($resolvedName === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is probably regex pattern
|
2020-02-13 11:09:51 +01:00
|
|
|
if ($this->regexPatternDetector->isRegexPattern($name)) {
|
2020-02-09 12:31:31 +01:00
|
|
|
return (bool) Strings::match($resolvedName, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// is probably fnmatch
|
|
|
|
if (Strings::contains($name, '*')) {
|
|
|
|
return fnmatch($name, $resolvedName, FNM_NOESCAPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// special case
|
|
|
|
if ($name === 'Object') {
|
|
|
|
return $name === $resolvedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtolower($resolvedName) === strtolower($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(Node $node): ?string
|
|
|
|
{
|
2020-02-21 15:54:51 +01:00
|
|
|
if ($node instanceof MethodCall || $node instanceof StaticCall) {
|
|
|
|
if ($node->name instanceof MethodCall || $node->name instanceof StaticCall) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:00:21 +02:00
|
|
|
$this->reportInvalidNodeForName($node);
|
2020-02-21 15:54:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 12:31:31 +01:00
|
|
|
foreach ($this->nodeNameResolvers as $nodeNameResolver) {
|
|
|
|
if (! is_a($node, $nodeNameResolver->getNode(), true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nodeNameResolver->resolve($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// more complex
|
|
|
|
if ($node instanceof Interface_ || $node instanceof Trait_) {
|
|
|
|
return $this->resolveNamespacedNameAwareNode($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! property_exists($node, 'name')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unable to resolve
|
|
|
|
if ($node->name instanceof Expr) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) $node->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function areNamesEqual(Node $firstNode, Node $secondNode): bool
|
|
|
|
{
|
|
|
|
return $this->getName($firstNode) === $this->getName($secondNode);
|
|
|
|
}
|
|
|
|
|
2020-02-29 21:50:29 +01:00
|
|
|
/**
|
2020-03-01 00:45:06 +01:00
|
|
|
* @param Name[]|Node[] $nodes
|
2020-02-29 21:50:29 +01:00
|
|
|
*/
|
|
|
|
public function getNames(array $nodes): array
|
|
|
|
{
|
|
|
|
$names = [];
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$name = $this->getName($node);
|
|
|
|
if (! is_string($name)) {
|
|
|
|
throw new ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$names[] = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
|
2020-03-01 00:45:06 +01:00
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
|
|
|
public function haveName(array $nodes, string $name): bool
|
|
|
|
{
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
if (! $this->isName($node, $name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:00:21 +02:00
|
|
|
/**
|
|
|
|
* @param MethodCall|StaticCall $node
|
|
|
|
*/
|
|
|
|
private function reportInvalidNodeForName(Node $node): void
|
|
|
|
{
|
|
|
|
$message = sprintf('Pick more specific node than "%s", e.g. "$node->name"', get_class($node));
|
|
|
|
|
|
|
|
$fileInfo = $this->currentFileInfoProvider->getSmartFileInfo();
|
|
|
|
if ($fileInfo instanceof SmartFileInfo) {
|
|
|
|
$message .= PHP_EOL . PHP_EOL;
|
|
|
|
$message .= sprintf(
|
|
|
|
'Caused in "%s" file on line %d on code "%s"',
|
|
|
|
$fileInfo->getRelativeFilePathFromCwd(),
|
|
|
|
$node->getStartLine(),
|
|
|
|
$this->betterStandardPrinter->print($node)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$backtrace = debug_backtrace();
|
2020-05-14 12:26:57 +02:00
|
|
|
$rectorBacktrace = $this->matchRectorBacktraceCall($backtrace);
|
|
|
|
|
|
|
|
if ($rectorBacktrace) {
|
2020-06-08 17:01:11 +02:00
|
|
|
// issues to find the file in prefixed
|
2020-06-08 17:06:58 +02:00
|
|
|
if (file_exists($rectorBacktrace[self::FILE])) {
|
|
|
|
$fileInfo = new SmartFileInfo($rectorBacktrace[self::FILE]);
|
2020-06-08 17:01:11 +02:00
|
|
|
$fileAndLine = $fileInfo->getRelativeFilePathFromCwd() . ':' . $rectorBacktrace['line'];
|
|
|
|
} else {
|
2020-06-08 17:06:58 +02:00
|
|
|
$fileAndLine = $rectorBacktrace[self::FILE] . ':' . $rectorBacktrace['line'];
|
2020-06-08 17:01:11 +02:00
|
|
|
}
|
2020-05-01 11:00:21 +02:00
|
|
|
|
|
|
|
$message .= PHP_EOL . PHP_EOL;
|
|
|
|
$message .= sprintf('Look at %s', $fileAndLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ShouldNotHappenException($message);
|
|
|
|
}
|
2020-05-14 12:26:57 +02:00
|
|
|
|
2020-08-05 22:45:36 +02:00
|
|
|
/**
|
|
|
|
* @param Interface_|Trait_ $classLike
|
|
|
|
*/
|
|
|
|
private function resolveNamespacedNameAwareNode(ClassLike $classLike): ?string
|
|
|
|
{
|
|
|
|
if (property_exists($classLike, 'namespacedName')) {
|
|
|
|
return $classLike->namespacedName->toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($classLike->name === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getName($classLike->name);
|
|
|
|
}
|
|
|
|
|
2020-05-14 12:26:57 +02:00
|
|
|
private function matchRectorBacktraceCall(array $backtrace): ?array
|
|
|
|
{
|
|
|
|
foreach ($backtrace as $singleTrace) {
|
|
|
|
if (! isset($singleTrace['object'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// match a Rector class
|
|
|
|
if (! is_a($singleTrace['object'], RectorInterface::class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $singleTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $backtrace[1] ?? null;
|
|
|
|
}
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|