mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
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.
This commit is contained in:
committed by
Barry vd. Heuvel
parent
027e58fd56
commit
0d1dd2182b
@@ -63,6 +63,14 @@ class ModelsCommand extends Command
|
|||||||
*/
|
*/
|
||||||
protected $nullableColumns = [];
|
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
|
* @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);
|
$content = $this->generateDocs($model, $ignore);
|
||||||
|
|
||||||
if (!$this->write) {
|
if (!$this->write) {
|
||||||
@@ -284,7 +296,7 @@ class ModelsCommand extends Command
|
|||||||
break;
|
break;
|
||||||
case 'date':
|
case 'date':
|
||||||
case 'datetime':
|
case 'datetime':
|
||||||
$realType = '\Illuminate\Support\Carbon';
|
$realType = $this->dateClass;
|
||||||
break;
|
break;
|
||||||
case 'collection':
|
case 'collection':
|
||||||
$realType = '\Illuminate\Support\Collection';
|
$realType = '\Illuminate\Support\Collection';
|
||||||
@@ -348,7 +360,7 @@ class ModelsCommand extends Command
|
|||||||
foreach ($columns as $column) {
|
foreach ($columns as $column) {
|
||||||
$name = $column->getName();
|
$name = $column->getName();
|
||||||
if (in_array($name, $model->getDates())) {
|
if (in_array($name, $model->getDates())) {
|
||||||
$type = '\Illuminate\Support\Carbon';
|
$type = $this->dateClass;
|
||||||
} else {
|
} else {
|
||||||
$type = $column->getType()->getName();
|
$type = $column->getType()->getName();
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user