spatie / schema-org

A fluent builder Schema.org types and ld+json generator

Home Page:https://freek.dev/2016/12/package-fluently-generate-schema-org-markup/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP 7.4 compatibility issue

Nuranto opened this issue · comments

I'm currently migrating to PHP7.4 (GA release will be on November, 28th) on my dev environment.
I get an error :
Function ReflectionType::__toString() is deprecated thrown on a $graph->organization() call

I think I've found a fixe, but not sure about this enough to make a PR :

$this->getOrCreate($type);

=>

$this->getOrCreate($type ? $type->getName() : null);

(in Spatie\SchemaOrg\Graph)

Thanks for pointing out. Right now PHP7.4 isn't supported but after reading the docs the __toString() method is deprecated since PHP5.5 😮
So will try to find a fix. The problem is that getName() is only available on ReflectionNamedType.

https://www.php.net/manual/de/reflectionnamedtype.getname.php

In english version of the doc, it says PHP7.1.0, not 5.5... Probably a bug in DE doc ^^

Anyway, thanks for the fix to come !

Similar issue on symfony repo : symfony/symfony#32397
They used the getName() solution, even if they were also dealing with ReflectionType class.. I don't get it either, but I tried the fix I proposed earlier, and it fixes the issue.

Hope it helps.
Note : I tested this only on a PHP7.4 env.

EDIT : If I dd(get_class($type)); it seems that get getReturnType returns a ReflectionNamedType, and not a ReflectionType as the doc says. (Probably the same for ReflectionParameter::getType() by the way)

EDIT 2 : Actually, there is something in the doc here https://www.php.net/manual/en/reflectionparameter.gettype.php (in examples section, the return type is still ReflectionType...)
Note the "may"....

EDIT 3 : More informations here : https://bugs.php.net/bug.php?id=73459

The documentation is correct -- we only guarantee that a ReflectionType is returned. ReflectionNamedType is a specific ReflectionType. In the future it might also return something like ReflectionUnionType (another specific ReflectionType).

Return types do not specify that *exactly* that class will be returned, only that it's that class or a subtype. Right now it will always be ReflectionNamedType, but this is not guaranteed, by design.

Sorry for the delay! Had to fight a cold the last days.
Will start with a fix and rely on the ReflectionNamedType but will add a instanceof check before to prevent method not exists exceptions.
PR will come. :)