From 0d1dd2182b44221eeb21e8a75ab5a01f884c066c Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Sat, 28 Dec 2019 14:33:08 +0100 Subject: [PATCH] Add support for custom date class via Date::use() (#859) Laravel 5.8 introduced a feature to support a custom date class via `Date::use()`, see https://github.com/laravel/framework/pull/25320 When e.g. using `Date::use(CarbonImmutable)` in a project, it means all date casts are not returning `\Illuminate\Support\Carbon` anymore but `\Carbon\CarbonImmutable`, which means all the generated type hints for dates are now wrong. This change tries to be still backwards compatible with Laravel < 5.8 which do not have the Date facade. --- src/Console/ModelsCommand.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 65e9007..b52aeb5 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -63,6 +63,14 @@ class ModelsCommand extends Command */ protected $nullableColumns = []; + /** + * During initializtion we use Laravels Date Facade to + * determine the actual date class and store it here. + * + * @var string + */ + protected $dateClass; + /** * @param Filesystem $files */ @@ -103,6 +111,10 @@ class ModelsCommand extends Command } } + $this->dateClass = class_exists(\Illuminate\Support\Facades\Date::class) + ? '\\' . get_class(\Illuminate\Support\Facades\Date::now()) + : '\Illuminate\Support\Carbon'; + $content = $this->generateDocs($model, $ignore); if (!$this->write) { @@ -284,7 +296,7 @@ class ModelsCommand extends Command break; case 'date': case 'datetime': - $realType = '\Illuminate\Support\Carbon'; + $realType = $this->dateClass; break; case 'collection': $realType = '\Illuminate\Support\Collection'; @@ -348,7 +360,7 @@ class ModelsCommand extends Command foreach ($columns as $column) { $name = $column->getName(); if (in_array($name, $model->getDates())) { - $type = '\Illuminate\Support\Carbon'; + $type = $this->dateClass; } else { $type = $column->getType()->getName(); switch ($type) {