Can I add return types to a custom collection for autocompletion? #39679
-
Hey everyone, I've created a custom collection and I'm using that as a DTO. The custom collection is quite simple, it only contains a static This other DTO is just used for accessing a few properties and methods. One of the big advantages of DTOs over just arrays is that DTO classes provide autocompletion for properties and methods. Unfortunately my IDE (PhpStorm) isn't able to figure out the return type of things like I already tried a few things, like adding a Personally I believe that this will be handy for many use cases, because most collections only contain one item type. Many thanks <?php
namespace App\DTOs;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
class MyDTO extends Collection
{
public static function make($items = []): static
{
return (new static($items))->map(function($input) {
return new OtherDTO($input);
});
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
$MyDTOCollection->map(function($input) {
/** @var OtherDTO $input */
$input->auto_completion
}); Also laravel-ide-helper could help |
Beta Was this translation helpful? Give feedback.
-
<?php
namespace App\DTOs;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
/**
* @method OtherDTO first(callable $callback = null, $default = null)
*/
class MyDTO extends Collection
{
public static function make($items = []): static
{
return (new static($items))->map(function($input) {
return new OtherDTO($input);
});
}
} |
Beta Was this translation helpful? Give feedback.
Also laravel-ide-helper could help