2021-01-17 16:43:47 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-01-17 16:43:47 +01:00
|
|
|
namespace Rector\PhpSpecToPHPUnit\NodeFactory;
|
|
|
|
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\PostRector\Collector\NodesToAddCollector;
|
|
|
|
final class DuringMethodCallFactory
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
2021-01-17 16:43:47 +01:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\PostRector\Collector\NodesToAddCollector
|
2021-01-17 16:43:47 +01:00
|
|
|
*/
|
|
|
|
private $nodesToAddCollector;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\Value\ValueResolver $valueResolver, \Rector\PostRector\Collector\NodesToAddCollector $nodesToAddCollector)
|
2021-01-17 16:43:47 +01:00
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
$this->nodesToAddCollector = $nodesToAddCollector;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function create(\PhpParser\Node\Expr\MethodCall $methodCall, \PhpParser\Node\Expr\PropertyFetch $propertyFetch) : \PhpParser\Node\Expr\MethodCall
|
2021-01-17 16:43:47 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!isset($methodCall->args[0])) {
|
2021-05-10 22:23:08 +00:00
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
2021-01-17 16:43:47 +01:00
|
|
|
}
|
|
|
|
$name = $this->valueResolver->getValue($methodCall->args[0]->value);
|
2021-05-10 22:23:08 +00:00
|
|
|
$thisObjectPropertyMethodCall = new \PhpParser\Node\Expr\MethodCall($propertyFetch, $name);
|
|
|
|
if (isset($methodCall->args[1]) && $methodCall->args[1]->value instanceof \PhpParser\Node\Expr\Array_) {
|
2021-01-17 16:43:47 +01:00
|
|
|
/** @var Array_ $array */
|
|
|
|
$array = $methodCall->args[1]->value;
|
|
|
|
if (isset($array->items[0])) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$thisObjectPropertyMethodCall->args[] = new \PhpParser\Node\Arg($array->items[0]->value);
|
2021-01-17 16:43:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/** @var MethodCall $parentMethodCall */
|
|
|
|
$parentMethodCall = $methodCall->var;
|
2021-05-10 22:23:08 +00:00
|
|
|
$parentMethodCall->name = new \PhpParser\Node\Identifier('expectException');
|
2021-01-17 16:43:47 +01:00
|
|
|
// add $this->object->someCall($withArgs)
|
|
|
|
$this->nodesToAddCollector->addNodeAfterNode($thisObjectPropertyMethodCall, $methodCall);
|
|
|
|
return $parentMethodCall;
|
|
|
|
}
|
|
|
|
}
|