Skip to content

match expression does not reconcile variable types based on condition  #260

@innocenzi

Description

@innocenzi

Mago doesn't seem to infer the type from the match case

$cache = new TestingCache(
    tag: match (true) {
        is_string($tag) => to_kebab_case($tag),
        $tag instanceof UnitEnum => to_kebab_case($tag->name), // this line
        default => 'default',
    },
    clock: $this->container->get(Clock::class)->toPsrClock(),
);
error[possibly-null-property-access]: Attempting to access a property on a possibly `null` value.
   ┌─ packages/cache/src/Testing/CacheTester.php:28:59
   │
28 │                 $tag instanceof UnitEnum => to_kebab_case($tag->name),
   │                                                           ^^^^ This expression can be `null` here
   │
   = If this expression is `null` at runtime, PHP will raise a warning and the property access will result in `null`.
   = Help: Use the nullsafe operator (`?->`) to safely access the property, or add a check to ensure the value is not `null` (e.g., `if ($obj !== null)`).

Source: https://github.com/tempestphp/tempest-framework/blob/main/packages/cache/src/Testing/CacheTester.php#L28

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority: HighAfter critical issues are fixed, handle these before any further issues.Status: In ProgressThis issue is being worked on and has someone assigned.Subject: AnalyzerAn issue or PR related to the static analyzer/type checker.Type: BugAn issue causing unintended or problematic behavior.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions