phpstan: [PHP^8.2] Access to an undefined property when type hinting interface with `@property`

Bug report

Similar to #8537 there is still an issue when a variable is type-hinted with an interface that has a @property, or @property-read declaration. From PHP8.2 onwards PHPStan is unable to recognise these declarations on interfaces.

/** 
 * @property int $x
 * @property-read int $y
 * @property-write int $z
 */
interface SampleInterface
{
}

function test(SampleInterface $test): void
{
    echo $test->x;
    echo $test->y;
    $test->z = 5;
}

Code snippet that reproduces the problem

https://phpstan.org/r/a199e998-8445-4590-9942-fe4b04a704bd

Expected output

No errors!

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

No response

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

This is now possible to solve with @phpstan-require-extends and @phpstan-require-implements.

Here’s an example: https://phpstan.org/r/2dec4d59-482d-4894-ba05-0c2453e28f2b

@mattvb91 If you have any questions please first try it out and then report if it doesn’t work as expected.

In the future this will be possible to solve with @phpstan-require-extends above an interface or a trait.

Yes, I’m thinking I’d allow this for interfaces with @mixin but I’d need to look into if it really solves all use cases.

Having the exact same issue with larastan. Have you managed to make it work yet?

Only with this 😦

// @phpstan-ignore-next-line see: https://github.com/phpstan/phpstan/issues/10302