File tree Expand file tree Collapse file tree
rules-tests/CodeQuality/Rector/MethodCall/AssertIssetToSpecificMethodRector/Fixture
rules/CodeQuality/Rector/MethodCall Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \PHPUnit \Tests \CodeQuality \Rector \MethodCall \AssertIssetToSpecificMethodRector \Fixture ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+
7+ final class SkipSuperglobals extends TestCase
8+ {
9+ public function test (): void
10+ {
11+ self ::assertFalse (isset ($ GLOBALS ['userconfig ' ]));
12+ self ::assertFalse (isset ($ _SERVER ['userconfig ' ]));
13+ self ::assertFalse (isset ($ _GET ['userconfig ' ]));
14+ self ::assertFalse (isset ($ _POST ['userconfig ' ]));
15+ self ::assertFalse (isset ($ _FILES ['userconfig ' ]));
16+ self ::assertFalse (isset ($ _COOKIE ['userconfig ' ]));
17+ self ::assertFalse (isset ($ _SESSION ['userconfig ' ]));
18+ self ::assertFalse (isset ($ _REQUEST ['userconfig ' ]));
19+ self ::assertFalse (isset ($ _ENV ['userconfig ' ]));
20+ }
21+ }
Original file line number Diff line number Diff line change 2222 */
2323final class AssertIssetToSpecificMethodRector extends AbstractRector
2424{
25+ /**
26+ * @var string[]
27+ */
28+ private const array SUPER_GLOBAL_VARIABLE_NAMES = [
29+ 'GLOBALS ' ,
30+ '_SERVER ' ,
31+ '_GET ' ,
32+ '_POST ' ,
33+ '_FILES ' ,
34+ '_COOKIE ' ,
35+ '_SESSION ' ,
36+ '_REQUEST ' ,
37+ '_ENV ' ,
38+ ];
39+
2540 public function __construct (
2641 private readonly IdentifierManipulator $ identifierManipulator ,
2742 private readonly TestsNodeAnalyzer $ testsNodeAnalyzer ,
@@ -78,6 +93,12 @@ public function refactor(Node $node): ?Node
7893 return null ;
7994 }
8095
96+ // Keep the non-evaluating behavior of isset() for global state. A superglobal
97+ // may be unavailable or unset, while passing it directly evaluates the variable.
98+ if ($ this ->isNames ($ issetExpr ->var , self ::SUPER_GLOBAL_VARIABLE_NAMES )) {
99+ return null ;
100+ }
101+
81102 // isset() on an ArrayAccess object is not equivalent to assertArrayHasKey():
82103 // the key may be any type (assertArrayHasKey() requires int|string) and offsetExists()
83104 // semantics can differ from array_key_exists()
You can’t perform that action at this time.
0 commit comments