tebru / gson-php

Gson implemented in PHP
Other
151 stars 18 forks source link

Add support for PHP 7.4 typed properties while type guessing #51

Open mattjanssen opened 4 years ago

mattjanssen commented 4 years ago

The TypeTokenFactory could be fixed and enhanced to support PHP typed properties.

Fix:

The following currently throws an exception if it encounters a non-initialized typed property:

$property->getDeclaringClass()->getDefaultProperties()[$property->getName()];

See https://3v4l.org/gVdKO to illustrate why this happens. An easy fix would be to add ?? null to the end.

Enhancement:

Somewhere in this chain of guessing the property's type could be a check for the property type:

$propertyAsTyped = $property->getType()->getName();

Obviously it would need a PHP version check around execution, and a null check in case it's not typed.