?????????? ????????? - ??????????????? - /home/agenciai/public_html/cd38d8/Carbon.tar
???????
NodeFactory/CarbonCallFactory.php 0000644 00000012172 15130253507 0013026 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Carbon\NodeFactory; use RectorPrefix202411\Nette\Utils\Strings; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; final class CarbonCallFactory { /** * @var string * @see https://regex101.com/r/LLMrFw/1 */ private const PLUS_MINUS_COUNT_REGEX = '#(?<operator>\\+|-)(\\s+)?(?<count>\\d+)(\\s+)?(?<unit>seconds|second|sec|minutes|minute|min|hours|hour|days|day|weeks|week|months|month|years|year)#'; /** * @var string * @see https://regex101.com/r/IhxHTO/1 */ private const STATIC_DATE_REGEX = '#now|yesterday|today|tomorrow#'; /** * @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall */ public function createFromDateTimeString(FullyQualified $carbonFullyQualified, String_ $string) { $carbonCall = $this->createStaticCall($carbonFullyQualified, $string); $string->value = Strings::replace($string->value, self::STATIC_DATE_REGEX); // Handle add/sub multiple times while ($match = Strings::match($string->value, self::PLUS_MINUS_COUNT_REGEX)) { $methodCall = $this->createModifyMethodCall($carbonCall, new LNumber((int) $match['count']), $match['unit'], $match['operator']); if ($methodCall instanceof MethodCall) { $carbonCall = $methodCall; $string->value = Strings::replace($string->value, self::PLUS_MINUS_COUNT_REGEX, '', 1); } } // If we still have something in the string, we go back to the first method and replace this with a parse if (($rest = Strings::trim($string->value)) !== '') { $currentCall = $carbonCall; $callStack = []; while ($currentCall instanceof MethodCall) { $callStack[] = $currentCall; $currentCall = $currentCall->var; } if (!$currentCall instanceof StaticCall) { return $carbonCall; } // If we fallback to a parse we want to include tomorrow/today/yesterday etc if ($currentCall->name instanceof Identifier && $currentCall->name->name != 'now') { $rest .= ' ' . $currentCall->name->name; } $currentCall->name = new Identifier('parse'); $currentCall->args = [new Arg(new String_($rest))]; // Rebuild original call from callstack $carbonCall = $this->rebuildCallStack($currentCall, $callStack); } return $carbonCall; } private function createStaticCall(FullyQualified $carbonFullyQualified, String_ $string) : StaticCall { $startDate = Strings::match($string->value, self::STATIC_DATE_REGEX)[0] ?? 'now'; return new StaticCall($carbonFullyQualified, new Identifier($startDate)); } /** * @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $carbonCall */ private function createModifyMethodCall($carbonCall, LNumber $countLNumber, string $unit, string $operator) : ?MethodCall { switch ($unit) { case 'sec': case 'second': case 'seconds': $unit = 'seconds'; break; case 'min': case 'minute': case 'minutes': $unit = 'minutes'; break; case 'hour': case 'hours': $unit = 'hours'; break; case 'day': case 'days': $unit = 'days'; break; case 'week': case 'weeks': $unit = 'weeks'; break; case 'month': case 'months': $unit = 'months'; break; case 'year': case 'years': $unit = 'years'; break; default: $unit = null; break; } switch ($operator) { case '+': $operator = 'add'; break; case '-': $operator = 'sub'; break; default: $operator = null; break; } if ($unit === null || $operator === null) { return null; } $methodName = $operator . \ucfirst($unit); return new MethodCall($carbonCall, new Identifier($methodName), [new Arg($countLNumber)]); } /** * @param MethodCall[] $callStack * @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall */ private function rebuildCallStack(StaticCall $staticCall, array $callStack) { if ($callStack === []) { return $staticCall; } $currentCall = $staticCall; $callStack = \array_reverse($callStack); foreach ($callStack as $call) { $call->var = $currentCall; $currentCall = $call; } return $currentCall; } } Rector/MethodCall/DateTimeMethodCallToCarbonRector.php 0000644 00000005277 15130253507 0017003 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Carbon\Rector\MethodCall; use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use Rector\Carbon\NodeFactory\CarbonCallFactory; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector\DateTimeMethodCallToCarbonRectorTest */ final class DateTimeMethodCallToCarbonRector extends AbstractRector { /** * @readonly * @var \Rector\Carbon\NodeFactory\CarbonCallFactory */ private $carbonCallFactory; public function __construct(CarbonCallFactory $carbonCallFactory) { $this->carbonCallFactory = $carbonCallFactory; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Convert new DateTime() with a method call to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { $date = (new \DateTime('today +20 day'))->format('Y-m-d'); } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $date = \Carbon\Carbon::today()->addDays(20)->format('Y-m-d') } } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [MethodCall::class]; } /** * @param MethodCall $node */ public function refactor(Node $node) : ?Node { if (!$node->var instanceof New_) { return null; } $new = $node->var; if (!$new->class instanceof Name) { return null; } if (!$this->isName($new->class, 'DateTime') && !$this->isName($new->class, 'DateTimeImmutable')) { return null; } if ($new->isFirstClassCallable()) { return null; } if (\count($new->getArgs()) !== 1) { // @todo handle in separate static call return null; } $firstArg = $new->getArgs()[0]; if (!$firstArg->value instanceof String_) { return null; } if ($this->isName($new->class, 'DateTime')) { $carbonFullyQualified = new FullyQualified('Carbon\\Carbon'); } else { $carbonFullyQualified = new FullyQualified('Carbon\\CarbonImmutable'); } $carbonCall = $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value); $node->var = $carbonCall; return $node; } } Rector/New_/DateTimeInstanceToCarbonRector.php 0000644 00000005015 15130253507 0015375 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Carbon\Rector\New_; use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use Rector\Carbon\NodeFactory\CarbonCallFactory; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\DateTimeInstanceToCarbonRectorTest */ final class DateTimeInstanceToCarbonRector extends AbstractRector { /** * @readonly * @var \Rector\Carbon\NodeFactory\CarbonCallFactory */ private $carbonCallFactory; public function __construct(CarbonCallFactory $carbonCallFactory) { $this->carbonCallFactory = $carbonCallFactory; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Convert new DateTime() to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE' $date = new \DateTime('today'); CODE_SAMPLE , <<<'CODE_SAMPLE' $date = \Carbon\Carbon::today(); CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [New_::class]; } /** * @param New_ $node */ public function refactor(Node $node) : ?Node { if ($node->isFirstClassCallable()) { return null; } if ($this->isName($node->class, 'DateTime')) { return $this->refactorWithClass($node, 'Carbon\\Carbon'); } if ($this->isName($node->class, 'DateTimeImmutable')) { return $this->refactorWithClass($node, 'Carbon\\CarbonImmutable'); } return null; } /** * @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null */ private function refactorWithClass(New_ $new, string $className) { // no arg? ::now() $carbonFullyQualified = new FullyQualified($className); if ($new->args === []) { return new StaticCall($carbonFullyQualified, new Identifier('now')); } if (\count($new->getArgs()) === 1) { $firstArg = $new->getArgs()[0]; if ($firstArg->value instanceof String_) { return $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value); } } return null; } } Rector/FuncCall/TimeFuncCallToCarbonRector.php 0000644 00000003566 15130253507 0015332 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Carbon\Rector\FuncCall; use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\PropertyFetch; 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 */ final class TimeFuncCallToCarbonRector extends AbstractRector { public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Convert time() function call to Carbon::now()->timestamp', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { $time = time(); } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $time = \Carbon\Carbon::now()->timestamp; } } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [FuncCall::class]; } /** * @param FuncCall $node */ public function refactor(Node $node) : ?Node { if (!$this->isName($node->name, 'time')) { return null; } $firstClassCallable = $node->isFirstClassCallable(); if (!$firstClassCallable && \count($node->getArgs()) !== 0) { return null; } // create now and format() $nowStaticCall = new StaticCall(new FullyQualified('Carbon\\Carbon'), 'now'); $propertyFetch = new PropertyFetch($nowStaticCall, 'timestamp'); if ($firstClassCallable) { return new ArrowFunction(['static' => \true, 'expr' => $propertyFetch]); } return $propertyFetch; } } Rector/FuncCall/DateFuncCallToCarbonRector.php 0000644 00000003573 15130253507 0015307 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Carbon\Rector\FuncCall; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector\DateFuncCallToCarbonRectorTest */ final class DateFuncCallToCarbonRector extends AbstractRector { public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Convert date() function call to Carbon::now()->format(*)', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { $date = date('Y-m-d'); } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $date = \Carbon\Carbon::now()->format('Y-m-d'); } } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [FuncCall::class]; } /** * @param FuncCall $node */ public function refactor(Node $node) : ?Node { if (!$this->isName($node->name, 'date')) { return null; } if ($node->isFirstClassCallable()) { return null; } if (\count($node->getArgs()) !== 1) { return null; } $firstArg = $node->getArgs()[0]; if (!$firstArg->value instanceof String_) { return null; } // create now and format() $nowStaticCall = new StaticCall(new FullyQualified('Carbon\\Carbon'), 'now'); return new MethodCall($nowStaticCall, 'format', [new Arg($firstArg->value)]); } }
| ver. 1.6 |
Github
|
.
| PHP 8.2.30 | ??????????? ?????????: 0 |
proxy
|
phpinfo
|
???????????