Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Carbon] Fix refactor time to carbon to use ->getTimestamp() over ->timestamp #6733

Merged
merged 1 commit into from
Feb 11, 2025
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 @@ -20,7 +20,7 @@ class FirstClassCallable
{
public function run()
{
$time = (static fn() => \Carbon\Carbon::now()->timestamp);
$time = (static fn() => \Carbon\Carbon::now()->getTimestamp());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SomeClass
{
public function run()
{
$time = \Carbon\Carbon::now()->timestamp;
$time = \Carbon\Carbon::now()->getTimestamp();
}
}

Expand Down
14 changes: 7 additions & 7 deletions rules/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector\DateFuncCallToCarbonRectorTest
* @see \Rector\Tests\Carbon\Rector\FuncCall\TimeFuncCallToCarbonRector\TimeFuncCallToCarbonRectorTest
*/
final class TimeFuncCallToCarbonRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Convert time() function call to Carbon::now()->timestamp', [
return new RuleDefinition('Convert time() function call to Carbon::now()->getTimestamp()', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
Expand All @@ -39,7 +39,7 @@ class SomeClass
{
public function run()
{
$time = \Carbon\Carbon::now()->timestamp;
$time = \Carbon\Carbon::now()->getTimestamp();
}
}
CODE_SAMPLE
Expand Down Expand Up @@ -72,15 +72,15 @@ public function refactor(Node $node): ?Node

// create now and format()
$nowStaticCall = new StaticCall(new FullyQualified('Carbon\Carbon'), 'now');
$propertyFetch = new PropertyFetch($nowStaticCall, 'timestamp');
$methodCall = new MethodCall($nowStaticCall, 'getTimestamp');

if ($firstClassCallable) {
return new ArrowFunction([
'static' => true,
'expr' => $propertyFetch,
'expr' => $methodCall,
]);
}

return $propertyFetch;
return $methodCall;
}
}