[NodeValueResolver] decouple ConcatValueResolver, ClassConstantFetchValueResolver

This commit is contained in:
TomasVotruba 2017-09-14 13:07:44 +02:00
parent ed4a309376
commit 43a74471b9
4 changed files with 91 additions and 42 deletions

View File

@ -3,7 +3,6 @@
namespace Rector\NodeValueResolver; namespace Rector\NodeValueResolver;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use Rector\Exception\NotImplementedException; use Rector\Exception\NotImplementedException;
use Rector\NodeValueResolver\Contract\PerNodeValueResolver\PerNodeValueResolverInterface; use Rector\NodeValueResolver\Contract\PerNodeValueResolver\PerNodeValueResolverInterface;
@ -35,10 +34,6 @@ final class NodeValueResolver
return $perNodeValueResolver->resolve($node); return $perNodeValueResolver->resolve($node);
} }
if ($node instanceof Concat) {
return $this->resolve($node->left) . $this->resolve($node->right);
}
throw new NotImplementedException(sprintf( throw new NotImplementedException(sprintf(
'%s() was unable to resolve "%s" Node. Add new value resolver via addValueResolver() method.', '%s() was unable to resolve "%s" Node. Add new value resolver via addValueResolver() method.',
__METHOD__, __METHOD__,

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -42,22 +42,17 @@ final class FuncCallValueResolver implements PerNodeValueResolverInterface, Node
*/ */
public function resolve(Node $funcCallArrayNode) public function resolve(Node $funcCallArrayNode)
{ {
$message = ''; if ((string) $funcCallArrayNode->name !== 'sprintf') {
if ((string) $funcCallArrayNode->name === 'sprintf') {
$message = $this->processSprintfNode($funcCallArrayNode);
$message = $this->classPrepender->completeClassToLocalMethods(
$message,
(string) $funcCallArrayNode->getAttribute(Attribute::CLASS_NAME)
);
}
if ($message === '') {
return null; return null;
} }
dump($funcCallArrayNode); $message = $this->processSprintfNode($funcCallArrayNode);
die; $message = $this->classPrepender->completeClassToLocalMethods(
$message,
(string) $funcCallArrayNode->getAttribute(Attribute::CLASS_NAME)
);
return $message ?: null;
} }
public function setNodeValueResolver(NodeValueResolver $nodeValueResolver): void public function setNodeValueResolver(NodeValueResolver $nodeValueResolver): void
@ -65,15 +60,15 @@ final class FuncCallValueResolver implements PerNodeValueResolverInterface, Node
$this->nodeValueResolver = $nodeValueResolver; $this->nodeValueResolver = $nodeValueResolver;
} }
private function processSprintfNode(FuncCall $funcCallNode): string private function processSprintfNode(FuncCall $funcCallNode): ?string
{ {
if ((string) $funcCallNode->name !== 'sprintf') { if ((string) $funcCallNode->name !== 'sprintf') {
// or Exception? // or Exception?
return ''; return null;
} }
if ($this->isDynamicSprintf($funcCallNode)) { if ($this->isDynamicSprintf($funcCallNode)) {
return ''; return null;
} }
$arguments = $funcCallNode->args; $arguments = $funcCallNode->args;
@ -82,33 +77,14 @@ final class FuncCallValueResolver implements PerNodeValueResolverInterface, Node
$firstArgument = $arguments[0]->value; $firstArgument = $arguments[0]->value;
if ($firstArgument instanceof String_) { if ($firstArgument instanceof String_) {
$sprintfMessage = $firstArgument->value; $sprintfMessage = $firstArgument->value;
} else {
return null;
} }
$sprintfArguments = []; $sprintfArguments = [];
for ($i = 1; $i < $argumentCount; ++$i) { for ($i = 1; $i < $argumentCount; ++$i) {
$argument = $arguments[$i]; $argument = $arguments[$i];
$sprintfArguments[] = $this->nodeValueResolver->resolve($argument->value); $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); return sprintf($sprintfMessage, ...$sprintfArguments);