Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 15, 2022
1 parent b823a58 commit 262e65c
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions src/Illuminate/Foundation/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ReflectionMethod;
use SplFileObject;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'model:show')]
class ShowModelCommand extends Command
Expand Down Expand Up @@ -113,8 +114,9 @@ public function handle()
protected function getAttributes($model)
{
$schema = $model->getConnection()->getDoctrineSchemaManager();
$columns = $schema->listTableColumns($model->getConnection()->getTablePrefix().$model->getTable());
$indexes = $schema->listTableIndexes($model->getTable());
$table = $model->getConnection()->getTablePrefix().$model->getTable();
$columns = $schema->listTableColumns($table);
$indexes = $schema->listTableIndexes($table);

return collect($columns)
->values()
Expand Down Expand Up @@ -145,9 +147,9 @@ protected function getVirtualAttributes($model, $columns)
$class = new ReflectionClass($model);

return collect($class->getMethods())
->filter(fn (ReflectionMethod $method) => ! $method->isStatic()
&& ! $method->isAbstract()
&& $method->getDeclaringClass()->getName() === get_class($model)
->reject(fn (ReflectionMethod $method) => $method->isStatic()
|| $method->isAbstract()
|| $method->getDeclaringClass()->getName() !== get_class($model)
)
->mapWithKeys(function (ReflectionMethod $method) use ($model) {
if (preg_match('/^get(.*)Attribute$/', $method->getName(), $matches) === 1) {
Expand Down Expand Up @@ -184,9 +186,9 @@ protected function getRelations($model)
{
return collect(get_class_methods($model))
->map(fn ($method) => new ReflectionMethod($model, $method))
->filter(fn (ReflectionMethod $method) => ! $method->isStatic()
&& ! $method->isAbstract()
&& $method->getDeclaringClass()->getName() === get_class($model)
->reject(fn (ReflectionMethod $method) => $method->isStatic()
|| $method->isAbstract()
|| $method->getDeclaringClass()->getName() !== get_class($model)
)
->filter(function (ReflectionMethod $method) {
$file = new SplFileObject($method->getFileName());
Expand All @@ -200,13 +202,16 @@ protected function getRelations($model)
return collect($this->relationMethods)
->contains(fn ($relationMethod) => str_contains($code, '$this->'.$relationMethod.'('));
})
->mapWithKeys(fn (ReflectionMethod $method) => [$method->getName() => $method->invoke($model)])
->map(fn (Relation $relation, string $name) => [
'name' => $name,
'type' => Str::afterLast(get_class($relation), '\\'),
'related' => get_class($relation->getRelated()),
])
->values();
->values()
->map(function (ReflectionMethod $method) use ($model) {
$relation = $method->invoke($model);

return [
'name' => $method->getName(),
'type' => Str::afterLast(get_class($relation), '\\'),
'related' => get_class($relation->getRelated()),
];
});
}

/**
Expand Down Expand Up @@ -275,22 +280,27 @@ protected function displayCli($class, $database, $table, $attributes, $relations
);

foreach ($attributes as $attribute) {
$first = sprintf('%s %s', $attribute['name'], collect(['increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended'])
->filter(fn ($property) => $attribute[$property])
->map(fn ($property) => sprintf('<fg=gray>%s</>', $property))
->implode('<fg=gray>,</> '));
$first = trim(sprintf(
'%s %s',
$attribute['name'],
collect(['increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended'])
->filter(fn ($property) => $attribute[$property])
->map(fn ($property) => sprintf('<fg=gray>%s</>', $property))
->implode('<fg=gray>,</> ')
));

$second = collect([
$attribute['type'],
$attribute['cast'] ? '<fg=yellow;options=bold>'.$attribute['cast'].'</>' : null,
])->filter()->implode(' <fg=gray>/</> ');

$this->components->twoColumnDetail(
str($first)->trim(), $second,
);
$this->components->twoColumnDetail($first, $second);

if ($this->output->isVerbose() && $attribute['default'] !== null) {
$this->components->bulletList(["default: {$attribute['default']}"]);
if ($attribute['default'] !== null) {
$this->components->bulletList(
[sprintf('default: %s', $attribute['default'])],
OutputInterface::VERBOSITY_VERBOSE
);
}
}

Expand All @@ -300,7 +310,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations

foreach ($relations as $relation) {
$this->components->twoColumnDetail(
$relation['name'].' <fg=gray>'.$relation['type'].'</>',
sprintf('%s <fg=gray>%s</>', $relation['name'], $relation['type']),
$relation['related']
);
}
Expand Down

0 comments on commit 262e65c

Please sign in to comment.