laravel-ide-helper icon indicating copy to clipboard operation
laravel-ide-helper copied to clipboard

Thread docs bloks as primary source of type

Open matysekmichal opened this issue 1 month ago • 0 comments

I'm interested in the custom Laravel casts where I propose to thread a docs block as a primary source of type and then use a PHP type. I think that this approach is right, because in case below the docs block have more precise return type than the PHP type.

/**
 * @inheritDoc
 * @return CastedProperty[]
 */
public function get($model, string $key, $value, array $attributes): array
{
    return [];
}

Consequently a docs block should looks like below.

/**
 * 
 *
 * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty[] $casted_property_with_return_primitive_and_return_primitive_docblock
 *
 * instead
 * @property array $casted_property_with_return_primitive_and_return_primitive_docblock
 */

I know a workaround for this problem where you just need to remove the PHP type but that behavior was unexpected for me when I saw it for the first time.

Another example can by with the collection.

/**
 * @inheritDoc
 * @return Collection<int, CastedProperty>
 */
public function get($model, string $key, $value, array $attributes): Collection
{
    return [];
}

The change of the approach to threat docs blocks as a primary source of type is a little braking change but at the end of a day we have a better types.

matysekmichal avatar May 18 '24 08:05 matysekmichal