Skip to content
Merged
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
63 changes: 45 additions & 18 deletions src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RectorLaravel\Rector\ArrayDimFetch;

use Override;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand All @@ -13,7 +14,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\NodeVisitor;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,6 +23,8 @@
*/
class SessionVariableToSessionFacadeRector extends AbstractRector
{
private const string IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR = 'is_inside_array_dim_fetch_with_dim_not_expr';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -54,6 +56,37 @@ public function getRuleDefinition(): RuleDefinition
);
}

#[Override]
public function beforeTraverse(array $nodes): array
{
parent::beforeTraverse($nodes);

$this->traverseNodesWithCallable($nodes, function (Node $node) {
if (! $node instanceof ArrayDimFetch) {
return null;
}

if (! $node->dim instanceof Expr) {
$node->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);
$this->traverseNodesWithCallable($node, function (Node $subNode) {
if (! $subNode instanceof Variable) {
return null;
}

$subNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);

return $subNode;
});

return $node;
}

return null;
});

return $nodes;
}

public function getNodeTypes(): array
{
return [
Expand All @@ -68,9 +101,8 @@ public function getNodeTypes(): array

/**
* @param ArrayDimFetch|Assign|FuncCall|Isset_|Unset_|Variable $node
* @return StaticCall|Expression|1|null
*/
public function refactor(Node $node): StaticCall|Expression|int|null
public function refactor(Node $node): StaticCall|Expression|null
{
if ($node instanceof ArrayDimFetch) {
return $this->processDimFetch($node);
Expand Down Expand Up @@ -100,17 +132,14 @@ public function refactor(Node $node): StaticCall|Expression|int|null
return $this->processAssign($node);
}

/**
* @return StaticCall|1|null
*/
public function processDimFetch(ArrayDimFetch $arrayDimFetch): StaticCall|int|null
public function processDimFetch(ArrayDimFetch $arrayDimFetch): ?StaticCall
{
if (! $this->isName($arrayDimFetch->var, '_SESSION')) {
return null;
}

if (! $arrayDimFetch->dim instanceof Expr) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
return null;
}

return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'get', [
Expand Down Expand Up @@ -163,10 +192,7 @@ private function processFunction(FuncCall $funcCall): ?StaticCall
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', $replacementMethod);
}

/**
* @return StaticCall|1|null
*/
private function processIsset(Isset_ $isset): StaticCall|int|null
private function processIsset(Isset_ $isset): ?StaticCall
{
if (count($isset->vars) < 1) {
return null;
Expand All @@ -183,18 +209,15 @@ private function processIsset(Isset_ $isset): StaticCall|int|null
}

if (! $var->dim instanceof Expr) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
return null;
}

return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'has', [
new Arg($var->dim),
]);
}

/**
* @return StaticCall|1|null
*/
private function processUnset(Unset_ $unset): StaticCall|int|null
private function processUnset(Unset_ $unset): ?StaticCall
{
if (count($unset->vars) < 1) {
return null;
Expand All @@ -211,7 +234,7 @@ private function processUnset(Unset_ $unset): StaticCall|int|null
}

if (! $var->dim instanceof Expr) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
return null;
}

return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'forget', [
Expand All @@ -221,6 +244,10 @@ private function processUnset(Unset_ $unset): StaticCall|int|null

private function processVariable(Variable $variable): ?StaticCall
{
if ($variable->getAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR) === true) {
return null;
}

if (! $this->isName($variable, '_SESSION')) {
return null;
}
Expand Down