66use PhpParser \Node \Arg ;
77use PhpParser \Node \Expr ;
88use PhpParser \Node \Expr \ArrayDimFetch ;
9+ use PhpParser \Node \Expr \Assign ;
10+ use PhpParser \Node \Expr \Isset_ ;
911use PhpParser \Node \Expr \StaticCall ;
12+ use PhpParser \Node \Scalar \InterpolatedString ;
13+ use PhpParser \Node \Stmt \Unset_ ;
14+ use PHPStan \Analyser \Scope ;
15+ use Rector \NodeTypeResolver \Node \AttributeKey ;
1016use RectorLaravel \AbstractRector ;
11- use RectorLaravel \NodeVisitor \ArrayDimFetchContextNodeVisitor ;
1217use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1318use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
1419
1722 */
1823class ServerVariableToRequestFacadeRector extends AbstractRector
1924{
25+ private const string IS_IN_SERVER_VARIABLE = 'is_in_server_variable ' ;
26+
2027 public function getRuleDefinition (): RuleDefinition
2128 {
2229 return new RuleDefinition (
@@ -34,14 +41,37 @@ public function getRuleDefinition(): RuleDefinition
3441
3542 public function getNodeTypes (): array
3643 {
37- return [ArrayDimFetch::class];
44+ return [Node::class, ArrayDimFetch::class];
3845 }
3946
40- /**
41- * @param ArrayDimFetch $node
42- */
4347 public function refactor (Node $ node ): ?StaticCall
4448 {
49+ if (! $ node instanceof ArrayDimFetch) {
50+ $ scope = $ node ->getAttribute (AttributeKey::SCOPE );
51+ if ($ scope instanceof Scope && $ scope ->isInFirstLevelStatement ()) {
52+ $ this ->traverseNodesWithCallable ($ node , function (Node $ subNode ) {
53+ if (in_array ($ subNode ::class, [Assign::class, Isset_::class, Unset_::class, InterpolatedString::class], true )
54+ && (! $ subNode instanceof Assign || $ subNode ->var instanceof ArrayDimFetch && $ this ->isName ($ subNode ->var ->var , '_SERVER ' ))) {
55+ $ this ->traverseNodesWithCallable ($ subNode , function (Node $ subSubNode ) {
56+ if (! $ subSubNode instanceof ArrayDimFetch) {
57+ return null ;
58+ }
59+
60+ $ subSubNode ->setAttribute (self ::IS_IN_SERVER_VARIABLE , true );
61+
62+ return $ subSubNode ;
63+ });
64+
65+ return $ subNode ;
66+ }
67+
68+ return null ;
69+ });
70+ }
71+
72+ return null ;
73+ }
74+
4575 if (! $ this ->isName ($ node ->var , '_SERVER ' )) {
4676 return null ;
4777 }
@@ -50,7 +80,7 @@ public function refactor(Node $node): ?StaticCall
5080 return null ;
5181 }
5282
53- if ($ node ->getAttribute (ArrayDimFetchContextNodeVisitor ::IS_IN_SERVER_VARIABLE ) === true ) {
83+ if ($ node ->getAttribute (self ::IS_IN_SERVER_VARIABLE ) === true ) {
5484 return null ;
5585 }
5686
0 commit comments