REDROOM
PHP 8.2.31
Path:
Logout
Edit File
Size: 3.00 KB
Close
//opt/cpanel/ea-wappspector/vendor/rector/rector/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php
Text
Base64
<?php declare (strict_types=1); namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\MixedType; use Rector\CodeQuality\CompactConverter; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\CodeQuality\Rector\FuncCall\CompactToVariablesRector\CompactToVariablesRectorTest */ final class CompactToVariablesRector extends AbstractRector { /** * @readonly * @var \Rector\CodeQuality\CompactConverter */ private $compactConverter; public function __construct(CompactConverter $compactConverter) { $this->compactConverter = $compactConverter; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Change compact() call to own array', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { $checkout = 'one'; $form = 'two'; return compact('checkout', 'form'); } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $checkout = 'one'; $form = 'two'; return ['checkout' => $checkout, 'form' => $form]; } } 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, 'compact')) { return null; } if ($this->compactConverter->hasAllArgumentsNamed($node)) { return $this->compactConverter->convertToArray($node); } if ($node->isFirstClassCallable()) { return null; } $firstArg = $node->getArgs()[0]; $firstValue = $firstArg->value; $firstValueStaticType = $this->getType($firstValue); if (!$firstValueStaticType instanceof ConstantArrayType) { return null; } if ($firstValueStaticType->getItemType() instanceof MixedType) { return null; } return $this->refactorAssignArray($firstValueStaticType); } private function refactorAssignArray(ConstantArrayType $constantArrayType) : ?Array_ { $arrayItems = []; foreach ($constantArrayType->getValueTypes() as $valueType) { if (!$valueType instanceof ConstantStringType) { return null; } $variableName = $valueType->getValue(); $variable = new Variable($variableName); $arrayItems[] = new ArrayItem($variable, new String_($variableName)); } return new Array_($arrayItems); } }
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 14
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
ArrayMergeOfNonArraysToSimpleArrayRector.php
2.27 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
CallUserFuncWithArrowFunctionToInlineRector.php
2.43 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
ChangeArrayPushToArrayAssignRector.php
2.45 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
CompactToVariablesRector.php
3.00 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
InlineIsAInstanceOfRector.php
2.97 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
IsAWithStringWithThirdArgumentRector.php
1.77 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
RemoveSoleValueSprintfRector.php
1.93 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SetTypeToCastRector.php
4.81 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyFuncGetArgsCountRector.php
1.47 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyInArrayValuesRector.php
1.72 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyRegexPatternRector.php
2.49 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyStrposLowerRector.php
2.09 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SingleInArrayToCompareRector.php
3.71 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
UnwrapSprintfOneArgumentRector.php
1.35 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).