55namespace Swis \Laravel \Elasticsearch \Commands ;
66
77use Illuminate \Console \Command ;
8- use Illuminate \Support \ Collection ;
8+ use Illuminate \Database \ Eloquent \ Model ;
99use Swis \Laravel \Elasticsearch \Contracts \IndexableInterface ;
1010
11+ /**
12+ * @phpstan-type IndexableModel \Illuminate\Database\Eloquent\Model&\Swis\Laravel\Elasticsearch\Contracts\IndexableInterface
13+ */
1114class ElasticsearchRefreshIndex extends Command
1215{
1316 protected $ signature = 'elasticsearch:refresh-index ' ;
@@ -19,12 +22,16 @@ public function handle(): int
1922 $ this ->call (ElasticsearchDeleteIndex::class);
2023 $ this ->call (ElasticsearchCreateIndex::class);
2124
22- /** @var class-string<\Illuminate\Database\Eloquent\Model >[] $models */
25+ /** @var class-string<IndexableModel >[] $models */
2326 $ models = config ('elasticsearch.models ' );
2427
2528 $ models = collect ($ models )
26- ->filter (fn (string $ model ) => is_a ($ model , IndexableInterface::class, true ))
27- ->flatMap (fn (string $ model ): Collection => $ model ::all ())
29+ ->filter (fn (string $ model ): bool => is_a ($ model , Model::class, true ) && is_a ($ model , IndexableInterface::class, true ))
30+ /**
31+ * @param class-string<IndexableModel> $model
32+ * @return array<int, IndexableModel>
33+ */
34+ ->flatMap (fn (string $ model ): array => $ model ::all ()->all ())
2835 ->each (fn (IndexableInterface $ model ) => $ model ->index ());
2936
3037 $ this ->info (sprintf ('Dispatched %d index jobs ' , $ models ->count ()));
0 commit comments