Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing properties if use docblocks can not work #59

Closed
JimChenWYU opened this issue Apr 4, 2021 · 1 comment
Closed

Typing properties if use docblocks can not work #59

JimChenWYU opened this issue Apr 4, 2021 · 1 comment

Comments

@JimChenWYU
Copy link
Contributor

In README.md an example

Or you can use docblocks:

class DateSettings extends Settings
{
    /** @var \DateTime  */
    public $birth_date;
    
    /** @var ?int  */
    public $a_nullable_int;
    
    public static function group(): string
    {
        return 'date';
    }
}

I always get an exception like:

Spatie\LaravelSettings\Exceptions\CouldNotResolveDocblockType
Could not resolve type in docblock: `int` of property `App\DateSettings::a_nullable_int`

when I try to look for the solution, I see the code and find this

$resolvedType = (new TypeResolver())->resolve($type);

if ($resolvedType instanceof Nullable) {
    return new Nullable(self::reflectDocblock(
        $reflectionProperty,
        (string) $resolvedType->getActualType()
    ));
}

when $type is ?int, or like other ?float, ?bool...
self::reflectDocblock() cannot be get actual type, alway call throw CouldNotResolveDocblockType::create($type, $reflectionProperty);

@rubenvanassche
Copy link
Member

Hi @JimChenWYU,

This was a small bug that has been fixed in 2.1.1!

We've also added a new syntax:

class DateSettings extends Settings
{
    /** @var \DateTime  */
    public $birth_date;
    
    /** @var ?null  */
    public $a_nullable_int;
    
    /** @var int|null  */
    public $another_nullable_int;
    
    /** @var int[]|null  */
    public $an_array_of_ints_or_null;
    
    public static function group(): string
    {
        return 'date';
    }
}

This one feels a bit more natural

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants