Passed
Push — next ( 9991ed...bea07e )
by Bas
06:34 queued 03:05
created

ShowModelCommand::displayCli()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 6.0007

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 32
nc 8
nop 8
dl 0
loc 53
ccs 35
cts 36
cp 0.9722
crap 6.0007
rs 8.7857
c 1
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Console;
6
7
use Illuminate\Contracts\Container\BindingResolutionException;
8
use Illuminate\Database\Console\ShowModelCommand as IlluminateShowModelCommand;
9
use Illuminate\Database\Eloquent\ModelInspector;
10
use Illuminate\Support\Collection;
11
12
class ShowModelCommand extends IlluminateShowModelCommand
13
{
14
    /**
15
     * The console command signature.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'model:show {model : The model to show}
20
                {--database= : The database connection to use}
21
                {--json : Output the model as JSON}';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return int
27
     */
28 2
    public function handle(ModelInspector $modelInspector)
29
    {
30
        try {
31 2
            $info = $modelInspector->inspect(
32 2
                $this->argument('model'),
33 2
                $this->option('database'),
34 2
            );
35
        } catch (BindingResolutionException $e) {
36
            $this->components->error($e->getMessage());
37
38
            return 1;
39
        }
40
41 2
        $this->display(
42 2
            $info['class'],
43 2
            $info['database'],
44 2
            $info['table'],
45 2
            $info['policy'],
46 2
            $info['attributes'],
47 2
            $info['relations'],
48 2
            $info['events'],
49 2
            $info['observers'],
50 2
        );
51
52 2
        return 0;
53
    }
54
55
    /**
56
     * Render the model information.
57
     *
58
     * @param  class-string<\Illuminate\Database\Eloquent\Model>  $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Illuminate\Database\Eloquent\Model> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Illuminate\Database\Eloquent\Model>.
Loading history...
59
     * @param  string  $database
60
     * @param  string  $table
61
     * @param  class-string|null  $policy
62
     * @param  Collection  $attributes
63
     * @param  Collection  $relations
64
     * @param  Collection  $events
65
     * @param  Collection  $observers
66
     * @return void
67
     */
68 2
    protected function display($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
69
    {
70 2
        $this->option('json')
71
            ? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
72 2
            : $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers);
73
    }
74
75
    /**
76
     * Render the model information for the CLI.
77
     *
78
     * @param  class-string<\Illuminate\Database\Eloquent\Model>  $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Illuminate\Database\Eloquent\Model> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Illuminate\Database\Eloquent\Model>.
Loading history...
79
     * @param  string  $database
80
     * @param  string  $table
81
     * @param  class-string|null  $policy
82
     * @param  Collection  $attributes
83
     * @param  Collection  $relations
84
     * @param  Collection  $events
85
     * @param  Collection  $observers
86
     * @return void
87
     */
88 2
    protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
89
    {
90 2
        $this->newLine();
91
92 2
        $this->components->twoColumnDetail('<fg=green;options=bold>' . $class . '</>');
93 2
        $this->components->twoColumnDetail('Database', $database);
94 2
        $this->components->twoColumnDetail('Table', $table);
95
96 2
        if ($policy) {
97
            $this->components->twoColumnDetail('Policy', $policy);
98
        }
99
100 2
        $this->newLine();
101
102 2
        $this->components->twoColumnDetail(
103 2
            '<fg=green;options=bold>Attributes</>',
104 2
            'type <fg=gray>/</> <fg=yellow;options=bold>cast</>',
105 2
        );
106
107 2
        foreach ($attributes as $attribute) {
108 2
            $first = trim(sprintf(
109 2
                '%s %s',
110 2
                $attribute['name'],
111 2
                collect(['computed', 'increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended'])
0 ignored issues
show
Bug introduced by
array('computed', 'incre..., 'hidden', 'appended') of type array<integer,string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
                collect(/** @scrutinizer ignore-type */ ['computed', 'increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended'])
Loading history...
112 2
                    ->filter(fn($property) => $attribute[$property])
113 2
                    ->map(fn($property) => sprintf('<fg=gray>%s</>', $property))
114 2
                    ->implode('<fg=gray>,</> '),
115 2
            ));
116
117 2
            $second = collect([
0 ignored issues
show
Bug introduced by
array(is_array($attribut...'cast'] . '</>' : null) of type array<integer,mixed|null|string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
            $second = collect(/** @scrutinizer ignore-type */ [
Loading history...
118 2
                (is_array($attribute['type'])) ? implode(', ', $attribute['type']) : $attribute['type'],
119 2
                $attribute['cast'] ? '<fg=yellow;options=bold>' . $attribute['cast'] . '</>' : null,
120 2
            ])->filter()->implode(' <fg=gray>/</> ');
121
122 2
            $this->components->twoColumnDetail($first, $second);
123
        }
124
125 2
        $this->newLine();
126
127 2
        $this->components->twoColumnDetail('<fg=green;options=bold>Relations</>');
128
129 2
        foreach ($relations as $relation) {
130 2
            $this->components->twoColumnDetail(
131 2
                sprintf('%s <fg=gray>%s</>', $relation['name'], $relation['type']),
132 2
                $relation['related'],
133 2
            );
134
        }
135
136 2
        $this->newLine();
137
138 2
        $this->displayCliEvents($events, $observers);
139
140 2
        $this->newLine();
141
    }
142
143
    /**
144
     * @param Collection $events
145
     * @return void
146
     */
147 2
    public function displayCliEvents(Collection $events, Collection $observers): void
148
    {
149 2
        $this->components->twoColumnDetail('<fg=green;options=bold>Events</>');
150
151 2
        if ($events->count()) {
152
            foreach ($events as $event) {
153
                $this->components->twoColumnDetail(
154
                    sprintf('%s', $event['event']),
155
                    sprintf('%s', $event['class']),
156
                );
157
            }
158
        }
159
160 2
        $this->newLine();
161
162 2
        $this->components->twoColumnDetail('<fg=green;options=bold>Observers</>');
163
164 2
        if ($observers->count()) {
165
            foreach ($observers as $observer) {
166
                $this->components->twoColumnDetail(
167
                    sprintf('%s', $observer['event']),
168
                    implode(', ', $observer['observer']),
169
                );
170
            }
171
        }
172
173
    }
174
175
}
176