mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-24 17:36:02 +02:00
add non-static call too (#4592)
This commit is contained in:
parent
aba4be85bb
commit
9dbf490a10
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user