add non-static call too (#4592)

This commit is contained in:
Tomas Votruba 2020-11-12 22:08:18 +00:00 committed by GitHub
parent aba4be85bb
commit 9dbf490a10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 19 deletions

View File

@ -86,24 +86,21 @@ CODE_SAMPLE
}
// is just made with static call?
if ($argValue instanceof StaticCall) {
/** @var StaticCall $argValue */
if ($this->isStaticCallNamed($argValue, 'Carbon\Carbon', 'now')) {
// now!
// 1. extract assign
$dateTimeVariable = new Variable('dateTime');
$assign = new Assign($dateTimeVariable, $argValue);
$this->addNodeBeforeNode($assign, $node);
if ($argValue instanceof StaticCall || $argValue instanceof MethodCall) {
// now!
// 1. extract assign
$dateTimeVariable = new Variable('dateTime');
$assign = new Assign($dateTimeVariable, $argValue);
$this->addNodeBeforeNode($assign, $node);
$node->args[2]->value = $dateTimeVariable;
$node->args[2]->value = $dateTimeVariable;
// update assign ">" → ">="
$this->changeCompareSignExpr($node->args[1]);
// update assign ">" → ">="
$this->changeCompareSignExpr($node->args[1]);
// 2. add "whereTime()" time call
$whereTimeMethodCall = $this->createWhereTimeMethodCall($node, $dateTimeVariable);
$this->addNodeAfterNode($whereTimeMethodCall, $node);
}
// 2. add "whereTime()" time call
$whereTimeMethodCall = $this->createWhereTimeMethodCall($node, $dateTimeVariable);
$this->addNodeAfterNode($whereTimeMethodCall, $node);
return $node;
}
@ -165,10 +162,8 @@ CODE_SAMPLE
}
}
private function createWhereTimeMethodCall(
MethodCall $methodCall,
Variable $dateTimeVariable
): MethodCall {
private function createWhereTimeMethodCall(MethodCall $methodCall, Variable $dateTimeVariable): MethodCall
{
$whereTimeArgs = [$methodCall->args[0], $methodCall->args[1], new Arg($dateTimeVariable)];
return new MethodCall($methodCall->var, 'whereTime', $whereTimeArgs);

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\Laravel\Tests\Rector\MethodCall\ChangeQueryWhereDateValueWithCarbonRector\Fixture;
use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
final class CarbonNowWithCall
{
public function run(Builder $query)
{
$query->whereDate('created_at', '>', Carbon::now()->subDays(10000));
}
}
?>
-----
<?php
namespace Rector\Laravel\Tests\Rector\MethodCall\ChangeQueryWhereDateValueWithCarbonRector\Fixture;
use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
final class CarbonNowWithCall
{
public function run(Builder $query)
{
$dateTime = Carbon::now()->subDays(10000);
$query->whereDate('created_at', '>=', $dateTime);
$query->whereTime('created_at', '>=', $dateTime);
}
}
?>

View File

@ -14,9 +14,16 @@ class Carbon extends \DateTime
{
public static function now(): self
{
return new self();
}
public static function today(): self
{
return new self();
}
public function subDays(int $days): self
{
return $this;
}
}