Skip to content

Custom rule analyzing isset() has different types in PHP 7 vs 8 #13983

@jlherren

Description

@jlherren

Bug report

I have a custom rule that restricts usages of isset(), but it broke with PHPStan 2.1.34 (2.1.33 worked fine), but only on PHP 8. Likely this is related to the new FiberNodeScopeResolver. I tried to xdebug it, but my xdebug skills reguarding fibers are a bit lacking. I narrowed it down to $scope->getType($var) returning different results on PHP 7 and 8.

Here's a dummy rule that demonstrates the problem. To reproduce it, just make it analyze itself:

declare(strict_types=1);
namespace Test;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;

/**
 * @implements Rule<Node\Expr\Isset_>
 */
final class IssetDumpRule implements Rule {
    public function getNodeType(): string {
        return Node\Expr\Isset_::class;
    }

    /**
     * @param Node\Expr\Isset_ $node
     */
    public function processNode(Node $node, Scope $scope): array {
        $type = $scope->getType($node->vars[0]);
        $error = RuleErrorBuilder::message('Dumped: ' . $type->describe(VerbosityLevel::precise()))
                                 ->identifier('dump.isset')
                                 ->build();
        return [$error];
    }

    public function analyzeThis(): void {
        $a = mt_rand(0, 1) === 0 ? 1 : null;
        if (isset($a)) {
        }
    }
}

On PHP 7.4 I get:

 ------ -------------------------------------
  Line   src/PHPStan/Rules/IssetDumpRule.php
 ------ -------------------------------------
  34     Dumped: 1|null
         🪪  dump.isset
 ------ -------------------------------------

On PHP 8.2 I get:

 ------ -------------------------------------
  Line   src/PHPStan/Rules/IssetDumpRule.php
 ------ -------------------------------------
  34     Dumped: 1
         🪪  dump.isset
 ------ -------------------------------------

Code snippet that reproduces the problem

Edit: See above. I can also create a full git repo that reproduces the problem, if that's easier.

Expected output

Same type 1|null in both PHP versions.

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions