phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

feat: isEnum class-string narrowing

Open SamMousa opened this issue 2 months ago • 9 comments

Implements https://github.com/phpstan/phpstan/issues/13712

SamMousa avatar Oct 24 '25 08:10 SamMousa

It seems many of the failures are not related to my changes....

SamMousa avatar Oct 24 '25 08:10 SamMousa

yes, atm a few spurious errors are in CI.

these here are related though: https://github.com/phpstan/phpstan-src/actions/runs/18773626957/job/53563151392?pr=4474

staabm avatar Oct 24 '25 09:10 staabm

I'll have another look after the weekend, for now any annotation I add seems to have no effect on the asserted type.

SamMousa avatar Oct 24 '25 10:10 SamMousa

It feels like the annotations in the ReflectionClass.stub file are ignored.

    /**
     * @return false
     */
    public function isEnum(): bool
    {
    }
Failed assertions in /projects/phpstan-src/tests/PHPStan/Analyser/nsrt/reflectionclass-isEnum.php:

Line 13:
Expected: false
Actual:   bool

Is there some kind of build step for the stubs that i'm missing?

SamMousa avatar Oct 27 '25 11:10 SamMousa

There's no build step for stubs. I don't know why it does not work for you but I'd argue that:

    /**
     * @return false
     */
    public function isEnum(): bool
    {
    }

is wrong: https://3v4l.org/AZfkh

ondrejmirtes avatar Oct 27 '25 11:10 ondrejmirtes

It is wrong 100%, but my objective here was to test if the annotation is working at all. The simplest annotation I could think of was @return false....

And if this already doesn't work I need to figure that part out first before I test my actual more complex annotations...

SamMousa avatar Oct 27 '25 11:10 SamMousa

Do not forget to also update ReflectionClassWithLazyObjects.stub.

ondrejmirtes avatar Oct 27 '25 11:10 ondrejmirtes

Do not forget to also update ReflectionClassWithLazyObjects.stub.

That was the cause, I did not realize there are 2 stubs for this class. Thanks, now I can get to work on the actual implementation

SamMousa avatar Oct 27 '25 11:10 SamMousa

Current implementation works! I'd like to actually narrow the class (template type T) of the reflection object as well, but that doesn't seem to work yet.

SamMousa avatar Oct 27 '25 13:10 SamMousa