-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathFakeModelsCommand.php
137 lines (113 loc) · 4.53 KB
/
FakeModelsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaColumn;
use function config;
use function get_class;
use function is_a;
use function in_array;
use function is_string;
use function implode;
/** @psalm-suppress PropertyNotSetInConstructor */
class FakeModelsCommand extends ModelsCommand
{
/** @var list<class-string<\Illuminate\Database\Eloquent\Model>> */
private $model_classes = [];
/** @var SchemaAggregator */
private $schema;
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/** @return list<class-string<\Illuminate\Database\Eloquent\Model>> */
public function getModels(): array
{
if ($this->dirs === []) {
throw new \LogicException('Directories to scan models are not set.');
}
$models = [];
// Bypass an issue https://github.com/barryvdh/laravel-ide-helper/issues/1414
/** @var list<class-string> $classlike_fq_names */
$classlike_fq_names = $this->loadModels();
foreach ($classlike_fq_names as $probably_model_fqcn) {
if (is_a($probably_model_fqcn, Model::class, true)) {
$models[] = $probably_model_fqcn;
}
}
return [...$this->model_classes, ...$models];
}
/**
* Load Model's properties.
* Overrides {@see \Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getPropertiesFromTable}
* in order to avoid using DB connection and use SchemaAggregator instead.
*
* @param Model $model
*/
public function getPropertiesFromTable($model): void
{
$table_name = $model->getTable();
if (!isset($this->schema->tables[$table_name])) {
return;
}
$this->model_classes[] = get_class($model);
$columns = $this->schema->tables[$table_name]->columns;
foreach ($columns as $column) {
$column_name = $column->name;
if (in_array($column_name, $model->getDates(), true)) {
$get_type = $set_type = '\Illuminate\Support\Carbon';
} else {
switch ($column->type) {
case SchemaColumn::TYPE_STRING:
case SchemaColumn::TYPE_INT:
case SchemaColumn::TYPE_FLOAT:
$get_type = $set_type = $column->type;
break;
case SchemaColumn::TYPE_BOOL:
switch (config('database.default')) {
case 'sqlite':
case 'mysql':
$set_type = '0|1|bool';
$get_type = '0|1';
break;
default:
$get_type = $set_type = 'bool';
break;
}
break;
case SchemaColumn::TYPE_ENUM:
if (!$column->options) {
$get_type = $set_type = 'string';
} else {
$get_type = $set_type = '\'' . implode('\'|\'', $column->options) . '\'';
}
break;
default:
$get_type = $set_type = SchemaColumn::TYPE_MIXED;
break;
}
}
if ($column->nullable) {
/** @psalm-suppress MixedArrayAssignment */
$this->nullableColumns[$column_name] = true;
}
if ($get_type === $set_type) {
$this->setProperty($column_name, $get_type, true, true, '', $column->nullable);
} else {
$this->setProperty($column_name, $get_type, true, null, '', $column->nullable);
$this->setProperty($column_name, $set_type, null, true, '', $column->nullable);
}
if ($this->write_model_magic_where) {
$this->setMethod(
Str::camel("where_" . $column_name),
'\Illuminate\Database\Eloquent\Builder|\\' . get_class($model),
array('$value')
);
}
}
}
}