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;
|
2020-12-20 22:05:48 +01:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2020-02-09 12:31:31 +01:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2020-09-17 23:18:18 +02:00
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
2020-02-21 15:54:51 +01:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2020-09-28 20:29:37 +02:00
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
2020-09-18 10:45:22 +02:00
|
|
|
use PhpParser\Node\Identifier;
|
2020-02-29 21:50:29 +01:00
|
|
|
use PhpParser\Node\Name;
|
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-12-25 18:53:26 +07:00
|
|
|
use Rector\Core\Util\StaticInstanceOf;
|
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) {
|
2020-12-24 23:28:56 +07:00
|
|
|
if ($this->isCallOrIdentifier($node->name)) {
|
2020-02-21 15:54:51 +01:00
|
|
|
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 (! 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-08-11 12:59:04 +02:00
|
|
|
* @return string[]
|
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-09-17 23:18:18 +02:00
|
|
|
public function isLocalPropertyFetchNamed(Node $node, string $name): bool
|
|
|
|
{
|
|
|
|
if (! $node instanceof PropertyFetch) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isName($node->var, 'this')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isName($node->name, $name);
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:29:37 +02:00
|
|
|
public function isLocalStaticPropertyFetchNamed(Node $node, string $name): bool
|
|
|
|
{
|
|
|
|
if (! $node instanceof StaticPropertyFetch) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isName($node->name, $name);
|
|
|
|
}
|
|
|
|
|
2020-12-20 22:05:48 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $names
|
|
|
|
*/
|
|
|
|
public function isFuncCallNames(Node $node, array $names): bool
|
|
|
|
{
|
|
|
|
if (! $node instanceof FuncCall) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isNames($node, $names);
|
|
|
|
}
|
|
|
|
|
2020-12-24 23:28:56 +07:00
|
|
|
private function isCallOrIdentifier(Node $node): bool
|
|
|
|
{
|
2020-12-25 18:53:26 +07:00
|
|
|
return StaticInstanceOf::isOneOf($node, [MethodCall::class, StaticCall::class, Identifier::class]);
|
2020-12-24 23:28:56 +07:00
|
|
|
}
|
|
|
|
|
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;
|
2020-12-20 13:50:55 +01:00
|
|
|
$message .= sprintf('Look at "%s"', $fileAndLine);
|
2020-05-01 11:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new ShouldNotHappenException($message);
|
|
|
|
}
|
2020-05-14 12:26:57 +02:00
|
|
|
|
2020-10-11 16:17:43 +02:00
|
|
|
/**
|
|
|
|
* @param mixed[] $backtrace
|
|
|
|
* @return mixed[]|null
|
|
|
|
*/
|
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
|
|
|
}
|