Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
Expand All @@ -25,11 +24,6 @@
*/
final class SimplifyWebTestCaseAssertionsRector extends AbstractRector
{
/**
* @var string
*/
private const THIS = 'this';

/**
* @var string
*/
Expand All @@ -40,12 +34,6 @@ final class SimplifyWebTestCaseAssertionsRector extends AbstractRector
*/
private $getStatusCodeMethodCall;

public function __construct()
{
$clientGetResponse = new MethodCall(new Variable('client'), 'getResponse');
$this->getStatusCodeMethodCall = new MethodCall($clientGetResponse, 'getStatusCode');
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Simplify use of assertions in WebTestCase', [
Expand Down Expand Up @@ -111,6 +99,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$clientGetResponse = $this->createMethodCall('client', 'getResponse');
$this->getStatusCodeMethodCall = $this->createMethodCall($clientGetResponse, 'getStatusCode');

if (! $this->isInWebTestCase($node)) {
return null;
}
Expand All @@ -119,9 +110,9 @@ public function refactor(Node $node): ?Node
$args = [];
$args[] = new Arg(new LNumber(200));
$args[] = new Arg($this->getStatusCodeMethodCall);
$match = new MethodCall(new Variable(self::THIS), self::ASSERT_SAME, $args);
$match = $this->createLocalMethodCall(self::ASSERT_SAME, $args);
if ($this->areNodesEqual($node, $match)) {
return new MethodCall(new Variable(self::THIS), 'assertResponseIsSuccessful');
return $this->createLocalMethodCall('assertResponseIsSuccessful');
}

// assertResponseStatusCodeSame
Expand All @@ -133,7 +124,7 @@ public function refactor(Node $node): ?Node
// assertSelectorTextContains
$args = $this->matchAssertContainsCrawlerArg($node);
if ($args !== null) {
return new MethodCall(new Variable(self::THIS), 'assertSelectorTextContains', $args);
return $this->createLocalMethodCall('assertSelectorTextContains', $args);
}

// 3. assertResponseRedirects
Expand Down Expand Up @@ -171,7 +162,7 @@ private function processAssertResponseStatusCodeSame(Node $node): ?MethodCall
return null;
}

return new MethodCall(new Variable(self::THIS), 'assertResponseStatusCodeSame', [$node->args[0]]);
return $this->createLocalMethodCall('assertResponseStatusCodeSame', [$node->args[0]]);
}

/**
Expand Down Expand Up @@ -224,13 +215,12 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node
$args[] = new Arg(new LNumber(301));
$args[] = new Arg($this->getStatusCodeMethodCall);

$match = new MethodCall(new Variable(self::THIS), self::ASSERT_SAME, $args);
$match = $this->createLocalMethodCall(self::ASSERT_SAME, $args);

if ($this->areNodesEqual($previousNode, $match)) {
$clientGetLocation = new MethodCall(new PropertyFetch(new MethodCall(
new Variable('client'),
'getResponse'
), 'headers'), 'get', [new Arg(new String_('Location'))]);
$getResponseMethodCall = $this->createMethodCall('client', 'getResponse');
$propertyFetch = new PropertyFetch($getResponseMethodCall, 'headers');
$clientGetLocation = $this->createMethodCall($propertyFetch, 'get', [new Arg(new String_('Location'))]);

if (! isset($methodCall->args[1])) {
return null;
Expand All @@ -243,7 +233,7 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node

$this->removeNode($previousNode);

return new MethodCall(new Variable(self::THIS), 'assertResponseRedirects', $args);
return $this->createLocalMethodCall('assertResponseRedirects', $args);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rector/AbstractRector/NodeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function createMethodCall($variable, string $method, array $arguments
protected function createLocalMethodCall(string $method, array $arguments = []): MethodCall
{
$thisVariable = new Variable('this');
return $this->createMethodCall($thisVariable, $method, $arguments);
return $this->nodeFactory->createMethodCall($thisVariable, $method, $arguments);
}

/**
Expand Down