From 262e65c8995f89ab42cb97fe12b669dd8d420a95 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Fri, 15 Jul 2022 20:11:53 +1000 Subject: [PATCH] Refactoring --- .../Foundation/Console/ShowModelCommand.php | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 999138585cee..49ab4b611978 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -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 @@ -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() @@ -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) { @@ -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()); @@ -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()), + ]; + }); } /** @@ -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('%s', $property)) - ->implode(', ')); + $first = trim(sprintf( + '%s %s', + $attribute['name'], + collect(['increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended']) + ->filter(fn ($property) => $attribute[$property]) + ->map(fn ($property) => sprintf('%s', $property)) + ->implode(', ') + )); $second = collect([ $attribute['type'], $attribute['cast'] ? ''.$attribute['cast'].'' : null, ])->filter()->implode(' / '); - $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 + ); } } @@ -300,7 +310,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations foreach ($relations as $relation) { $this->components->twoColumnDetail( - $relation['name'].' '.$relation['type'].'', + sprintf('%s %s', $relation['name'], $relation['type']), $relation['related'] ); }