psalm-plugin-laravel/src/FakeModelsCommand.php

133 lines
4.0 KiB
PHP
Raw Normal View History

2020-01-05 21:51:49 +01:00
<?php
namespace Psalm\LaravelPlugin;
use Composer\Autoload\ClassMapGenerator;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use ReflectionClass;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Tag;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
2020-04-12 21:01:20 +02:00
use function get_class;
use function in_array;
use function config;
use function implode;
2020-01-05 21:51:49 +01:00
class FakeModelsCommand extends \Barryvdh\LaravelIdeHelper\Console\ModelsCommand
{
2020-01-06 00:51:16 +01:00
/** @var SchemaAggregator */
private $schema;
/** @var array<class-string> */
private $model_classes = [];
2020-01-05 21:51:49 +01:00
/**
* @param Filesystem $files
*/
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
2020-01-06 00:51:16 +01:00
/** @return array<class-string> */
public function getModels()
{
return $this->model_classes + $this->loadModels();
2020-01-06 00:51:16 +01:00
}
2020-01-05 21:51:49 +01:00
/**
* Load the properties from the database table.
*
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function getPropertiesFromTable($model) : void
{
$table_name = $model->getTable();
if (!isset($this->schema->tables[$table_name])) {
2020-01-06 00:51:16 +01:00
return;
2020-01-05 21:51:49 +01:00
}
2020-01-06 00:51:16 +01:00
$this->model_classes[] = get_class($model);
2020-01-05 21:51:49 +01:00
$columns = $this->schema->tables[$table_name]->columns;
foreach ($columns as $column) {
$name = $column->name;
2020-01-06 04:40:57 +01:00
2020-01-05 21:51:49 +01:00
if (in_array($name, $model->getDates())) {
2020-01-06 04:40:57 +01:00
$get_type = $set_type = '\Illuminate\Support\Carbon';
2020-01-05 21:51:49 +01:00
} else {
switch ($column->type) {
case 'string':
case 'int':
case 'float':
2020-01-06 04:40:57 +01:00
$get_type = $set_type = $column->type;
2020-01-05 21:51:49 +01:00
break;
case 'bool':
switch (config('database.default')) {
case 'sqlite':
case 'mysql':
2020-01-06 04:40:57 +01:00
$set_type = '0|1|bool';
$get_type = '0|1';
2020-01-05 21:51:49 +01:00
break;
default:
2020-01-06 04:40:57 +01:00
$get_type = $set_type = 'bool';
2020-01-05 21:51:49 +01:00
break;
}
break;
case 'enum':
2020-01-06 00:51:16 +01:00
if (!$column->options) {
2020-01-06 04:40:57 +01:00
$get_type = $set_type = 'string';
2020-01-06 00:51:16 +01:00
} else {
2020-01-06 04:40:57 +01:00
$get_type = $set_type = '\'' . implode('\'|\'', $column->options) . '\'';
2020-01-06 00:51:16 +01:00
}
2020-01-05 21:51:49 +01:00
break;
default:
2020-01-06 04:40:57 +01:00
$get_type = $set_type = 'mixed';
2020-01-05 21:51:49 +01:00
break;
}
}
if ($column->nullable) {
$this->nullableColumns[$name] = true;
}
2020-01-06 04:40:57 +01:00
if ($get_type === $set_type) {
$this->setProperty($name, $get_type, true, true, '', $column->nullable);
} else {
2020-01-06 04:52:27 +01:00
$this->setProperty($name, $get_type, true, null, '', $column->nullable);
$this->setProperty($name, $set_type, null, true, '', $column->nullable);
2020-01-06 04:40:57 +01:00
}
2020-01-05 21:51:49 +01:00
if ($this->write_model_magic_where) {
2020-01-06 00:51:16 +01:00
$this->setMethod(
2020-01-05 21:51:49 +01:00
Str::camel("where_" . $name),
'\Illuminate\Database\Eloquent\Builder|\\' . get_class($model),
array('$value')
);
}
}
}
/**
* @param \Illuminate\Database\Eloquent\Model $model
*/
2020-01-06 00:46:59 +01:00
protected function getPropertiesFromMethods($model) : void
2020-01-05 21:51:49 +01:00
{
2020-01-06 00:51:16 +01:00
// do nothing
2020-01-05 21:51:49 +01:00
}
}