mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
[NodeValueResolver] decouple ConcatValueResolver, ClassConstantFetchValueResolver
This commit is contained in:
parent
ed4a309376
commit
43a74471b9
@ -3,7 +3,6 @@
|
||||
namespace Rector\NodeValueResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use Rector\Exception\NotImplementedException;
|
||||
use Rector\NodeValueResolver\Contract\PerNodeValueResolver\PerNodeValueResolverInterface;
|
||||
|
||||
@ -35,10 +34,6 @@ final class NodeValueResolver
|
||||
return $perNodeValueResolver->resolve($node);
|
||||
}
|
||||
|
||||
if ($node instanceof Concat) {
|
||||
return $this->resolve($node->left) . $this->resolve($node->right);
|
||||
}
|
||||
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s() was unable to resolve "%s" Node. Add new value resolver via addValueResolver() method.',
|
||||
__METHOD__,
|
||||
|
@ -0,0 +1,41 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeValueResolver\PerNodeValueResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\PrettyPrinter\Standard;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\NodeValueResolver\Contract\PerNodeValueResolver\PerNodeValueResolverInterface;
|
||||
|
||||
final class ClassConstantFetchValueResolver implements PerNodeValueResolverInterface
|
||||
{
|
||||
/**
|
||||
* @var Standard
|
||||
*/
|
||||
private $standardPrinter;
|
||||
|
||||
public function getNodeClass(): string
|
||||
{
|
||||
return ClassConstFetch::class;
|
||||
}
|
||||
|
||||
public function __construct(Standard $standardPrinter)
|
||||
{
|
||||
$this->standardPrinter = $standardPrinter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConstFetch $classConstFetchNode
|
||||
* @return mixed
|
||||
*/
|
||||
public function resolve(Node $classConstFetchNode)
|
||||
{
|
||||
$value = $this->standardPrinter->prettyPrint([$classConstFetchNode]);
|
||||
if ($value === 'static::class') {
|
||||
return $classConstFetchNode->getAttribute(Attribute::CLASS_NAME);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeValueResolver\PerNodeValueResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use Rector\NodeValueResolver\Contract\NodeValueResolverAwareInterface;
|
||||
use Rector\NodeValueResolver\Contract\PerNodeValueResolver\PerNodeValueResolverInterface;
|
||||
use Rector\NodeValueResolver\NodeValueResolver;
|
||||
|
||||
final class ConcatValueResolver implements PerNodeValueResolverInterface, NodeValueResolverAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var NodeValueResolver
|
||||
*/
|
||||
private $nodeValueResolver;
|
||||
|
||||
public function getNodeClass(): string
|
||||
{
|
||||
return Concat::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Concat $concatNode
|
||||
* @return mixed
|
||||
*/
|
||||
public function resolve(Node $concatNode)
|
||||
{
|
||||
return $this->nodeValueResolver->resolve($concatNode->left) .
|
||||
$this->nodeValueResolver->resolve($concatNode->right);
|
||||
}
|
||||
|
||||
public function setNodeValueResolver(NodeValueResolver $nodeValueResolver): void
|
||||
{
|
||||
$this->nodeValueResolver = $nodeValueResolver;
|
||||
}
|
||||
}
|
@ -42,22 +42,17 @@ final class FuncCallValueResolver implements PerNodeValueResolverInterface, Node
|
||||
*/
|
||||
public function resolve(Node $funcCallArrayNode)
|
||||
{
|
||||
$message = '';
|
||||
|
||||
if ((string) $funcCallArrayNode->name === 'sprintf') {
|
||||
$message = $this->processSprintfNode($funcCallArrayNode);
|
||||
$message = $this->classPrepender->completeClassToLocalMethods(
|
||||
$message,
|
||||
(string) $funcCallArrayNode->getAttribute(Attribute::CLASS_NAME)
|
||||
);
|
||||
}
|
||||
|
||||
if ($message === '') {
|
||||
if ((string) $funcCallArrayNode->name !== 'sprintf') {
|
||||
return null;
|
||||
}
|
||||
|
||||
dump($funcCallArrayNode);
|
||||
die;
|
||||
$message = $this->processSprintfNode($funcCallArrayNode);
|
||||
$message = $this->classPrepender->completeClassToLocalMethods(
|
||||
$message,
|
||||
(string) $funcCallArrayNode->getAttribute(Attribute::CLASS_NAME)
|
||||
);
|
||||
|
||||
return $message ?: null;
|
||||
}
|
||||
|
||||
public function setNodeValueResolver(NodeValueResolver $nodeValueResolver): void
|
||||
@ -65,15 +60,15 @@ final class FuncCallValueResolver implements PerNodeValueResolverInterface, Node
|
||||
$this->nodeValueResolver = $nodeValueResolver;
|
||||
}
|
||||
|
||||
private function processSprintfNode(FuncCall $funcCallNode): string
|
||||
private function processSprintfNode(FuncCall $funcCallNode): ?string
|
||||
{
|
||||
if ((string) $funcCallNode->name !== 'sprintf') {
|
||||
// or Exception?
|
||||
return '';
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isDynamicSprintf($funcCallNode)) {
|
||||
return '';
|
||||
return null;
|
||||
}
|
||||
|
||||
$arguments = $funcCallNode->args;
|
||||
@ -82,33 +77,14 @@ final class FuncCallValueResolver implements PerNodeValueResolverInterface, Node
|
||||
$firstArgument = $arguments[0]->value;
|
||||
if ($firstArgument instanceof String_) {
|
||||
$sprintfMessage = $firstArgument->value;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sprintfArguments = [];
|
||||
for ($i = 1; $i < $argumentCount; ++$i) {
|
||||
$argument = $arguments[$i];
|
||||
|
||||
$sprintfArguments[] = $this->nodeValueResolver->resolve($argument->value);
|
||||
|
||||
// if ($argument->value instanceof Method) {
|
||||
// /** @var Node\Stmt\ClassMethod $methodNode */
|
||||
// $methodNode = $funcCallNode->getAttribute(Attribute::SCOPE_NODE);
|
||||
// $sprintfArguments[] = (string) $methodNode->name;
|
||||
// } elseif ($argument->value instanceof ClassConstFetch) {
|
||||
// $value = $this->standardPrinter->prettyPrint([$argument->value]);
|
||||
// if ($value === 'static::class') {
|
||||
// $sprintfArguments[] = $argument->value->getAttribute(Attribute::CLASS_NAME);
|
||||
// }
|
||||
// } else {
|
||||
// dump($this->standardPrinter->prettyPrint([$argument]));
|
||||
// die;
|
||||
//
|
||||
// throw new NotImplementedException(sprintf(
|
||||
// 'Not implemented yet. Go to "%s()" and add check for "%s" argument node.',
|
||||
// __METHOD__,
|
||||
// get_class($argument->value)
|
||||
// ));
|
||||
// }
|
||||
}
|
||||
|
||||
return sprintf($sprintfMessage, ...$sprintfArguments);
|
||||
|
Loading…
x
Reference in New Issue
Block a user