hnw / php-timecop

A PHP extension providing "time travel" capabilities inspired by ruby timecop gem
MIT License
385 stars 40 forks source link

Ignoring TimecopDateTime during reflection? #5

Open klaussilveira opened 10 years ago

klaussilveira commented 10 years ago

Take a look at the following snippet:

<?php

class Test
{
    public function setCreatedAt(DateTime $createdAt)
    {
        $this->createdAt = $createdAt;
    }
}

$test = new Test();

$reflection = new ReflectionClass($test);
$params = $reflection->getMethod('setCreatedAt')->getParameters();

foreach ($params as $param) {
    echo $param->getClass()->getName();
}

When using Reflection's API to check the Type Hint of the params of methods, the TimecopDateTime class is found, instead of DateTime itself. How can we avoid this behavior? I'm unable to use the timecop extension since it's conflicting with Doctrine's ProxyGenerator.

hlegius commented 10 years ago

:+1: on this.

shouze commented 10 years ago

@klaussilveira indeed we will face the same issue on our side. We released https://github.com/rezzza/TimeTraveler if it can help you but it's for testing purpose only, not to use in production. AOP extension is leaking and have some bugs too BTW.

shouze commented 10 years ago

@klaussilveira can you plz point in doctrine ProxyGenerator lines where you get such type hinting exception? https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Proxy/ProxyGenerator.php

Can you execute this piece of code on your own?

 if (\TimecopDateTime instanceof \DateTime) {
   echo "TimecopDateTime is an instanceof DateTime\n";
 }

@hlegius any clue?

four43 commented 9 years ago

+1, we ran into the same issue. Seems to be around https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Proxy/ProxyGenerator.php#L941 it's returning the \TimecopDateTime instead of our type hinted \DateTime.

CristianGiordano commented 9 years ago

I have run into this problem, for me a simple solution was to ask for a DateTimeInterface instead of a particular type.

veloper commented 9 years ago

+1 on the doctrine proxy generation issue.

The proxy classes are created with method parameter type hints of TimecopDateTime instead of the expected DateTime

veloper commented 9 years ago

Bone headed work around after doctrine proxy generation:

sed -i -- 's/TimecopDateTime/DateTime/g' /current/release/proxies/*
IonBazan commented 7 years ago

@veloper @klaussilveira Can you confirm the bug is still valid? I can't reproduce it on newest timecop builds.

veloper commented 7 years ago

@IonBazan - Which pecl version would you like me to test against?

IonBazan commented 7 years ago

Try to compile from master.

rquadling commented 6 years ago

Is this extension still maintained? It would be nice to get some feedback on some of the issues?