Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertIssetToSpecificMethodRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipSuperglobals extends TestCase
{
public function test(): void
{
self::assertFalse(isset($GLOBALS['userconfig']));
self::assertFalse(isset($_SERVER['userconfig']));
self::assertFalse(isset($_GET['userconfig']));
self::assertFalse(isset($_POST['userconfig']));
self::assertFalse(isset($_FILES['userconfig']));
self::assertFalse(isset($_COOKIE['userconfig']));
self::assertFalse(isset($_SESSION['userconfig']));
self::assertFalse(isset($_REQUEST['userconfig']));
self::assertFalse(isset($_ENV['userconfig']));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
*/
final class AssertIssetToSpecificMethodRector extends AbstractRector
{
/**
* @var string[]
*/
private const array SUPER_GLOBAL_VARIABLE_NAMES = [
'GLOBALS',
'_SERVER',
'_GET',
'_POST',
'_FILES',
'_COOKIE',
'_SESSION',
'_REQUEST',
'_ENV',
];

public function __construct(
private readonly IdentifierManipulator $identifierManipulator,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
Expand Down Expand Up @@ -78,6 +93,12 @@ public function refactor(Node $node): ?Node
return null;
}

// Keep the non-evaluating behavior of isset() for global state. A superglobal
// may be unavailable or unset, while passing it directly evaluates the variable.
if ($this->isNames($issetExpr->var, self::SUPER_GLOBAL_VARIABLE_NAMES)) {
return null;
}

// isset() on an ArrayAccess object is not equivalent to assertArrayHasKey():
// the key may be any type (assertArrayHasKey() requires int|string) and offsetExists()
// semantics can differ from array_key_exists()
Expand Down
Loading