Skip to content

Analyzer gets confused when mixing asymmetric visibility and property hooks #837

@BenMorel

Description

@BenMorel

Description

This code works fine:

class Foo
{
    private(set) int $bar {
        get {
            return $this->bar ?? 0;
        }
    }
}

echo new Foo()->bar; // 0

But Mago analyzer fails with:

error[invalid-property-read]: Cannot read private property `$bar` from class `Foo`.
   ┌─ test.php:12:17
   │  
 5 │ ╭     private(set) int $bar {
 6 │ │         get {
 7 │ │             return $this->bar ?? 0;
 8 │ │         }
 9 │ │     }
   │ ╰─────' Member is defined as `private` here
   ·  
12 │   echo new Foo()->bar;
   │                 --^^^ This member is private and cannot be accessed here
   │                 │  
   │                 Invalid access occurs here, from the global scope
   │  
   = Help: Make the property `$bar` readable (e.g., `public`), or add a public getter method.

error[no-value]: The first value for `echo` cannot produce a value.
   ┌─ test.php:12:6
   │
12 │ echo new Foo()->bar;
   │ ---- ^^^^^^^^^^^^^^ This expression has type `never`
   │ │     
   │ `echo` called here
   │
   = An expression of type `never` cannot complete, often because it throws or exits.

error: found 2 issues: 2 error(s)

Playground Link

https://mago.carthage.software/playground#019b6027-8521-e913-495d-af72594d2a8e

Reproduction

No response

Environment

  • Mago: 1.0.3
  • OS: Linux
  • Installed via: Composer

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugAn issue causing unintended or problematic behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions