phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 241 forks source link

Support for Disjunctive Normal Form Types #558

Open stof opened 2 years ago

stof commented 2 years ago

See https://wiki.php.net/rfc/dnf_types

We need to make sure they can be doubled properly, with tests ensuring they work fine.

kschatzle commented 2 years ago

Noting that getTypes() for the following code may now (PHP8.2) return 'ReflectionIntersectionType':

https://github.com/phpspec/prophecy/blob/a4477cd17b481a6095494c9583316a3e3aa88916/src/Prophecy/Prophecy/MethodProphecy.php#L90-L92

https://github.com/phpspec/prophecy/blob/a4477cd17b481a6095494c9583316a3e3aa88916/src/Prophecy/Doubler/Generator/ClassMirror.php#L224-L226

I'm unsure what the underlying decision to not support ReflectionIntersectionType was. My assumption is that it was difficult (maybe impossible) to create the double with the necessary values.

Possible solution would be to loop through types looking for ReflectionIntersectionType and throwing a nice Exception.

It does throw an err. The class effectively turns this:

    public function doSomething () : A|(B&D)|null
    {

    }

Into

        public  function doSomething(): A|B&D|null { //At least one err here. Should be A|(B&D)|null I believe.
            return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());
        }

All this said, I'm confused by something, so going to ping the php-src group. Either found a bug or something I don't understand.

ciaranmcnulty commented 2 years ago

I'm unsure what the underlying decision to not support ReflectionIntersectionType was

Simply that PHP 8.1 was coming and we wanted a clear exception to be thrown here rather than a hard error

Nobody's made it work since (or TBH has raised an issue)

ciaranmcnulty commented 2 years ago

if someone wants to make intersection types work that's fine, to be clear

stof commented 2 years ago

The issue tracking proper support for intersection types is #535, but nobody worked on it yet.