Completed
Pull Request — master (#340)
by
unknown
06:20
created

SeederMakeCommand::fire()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 30
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 59
rs 8.7117

How to fix   Long Method   

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:

1
<?php namespace Anomaly\Streams\Platform\Database\Seeder\Console;
2
3
use \Illuminate\Support\Composer;
4
use \Illuminate\Filesystem\Filesystem;
5
use Illuminate\Foundation\Bus\DispatchesJobs;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Anomaly\Streams\Platform\Addon\Command\GetAddon;
9
use Anomaly\Streams\Platform\Stream\StreamCollection;
10
use Anomaly\Streams\Platform\Stream\Command\GetStreams;
11
use Anomaly\Streams\Platform\Addon\Console\Command\WriteAddonSeeder;
12
use Anomaly\Streams\Platform\Stream\Console\Command\WriteEntitySeeder;
13
14
/**
15
 * @TODO alarm about replacing
16
 */
17
class SeederMakeCommand extends \Illuminate\Console\Command
18
{
19
    use DispatchesJobs;
20
21
    /**
22
     * The console command name.
23
     *
24
     * @var string
25
     */
26
    protected $name = 'make:seeder';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Create a new seeder class for addon';
34
35
    /**
36
     * All streams string value
37
     *
38
     * @var string
39
     */
40
    protected $allChoice = 'All streams';
41
42
    /**
43
     * The Composer instance.
44
     *
45
     * @var \Illuminate\Support\Composer
46
     */
47
    protected $composer;
48
49
    /**
50
     * Create a new command instance.
51
     *
52
     * @param  \Illuminate\Filesystem\Filesystem $files
53
     * @param  \Illuminate\Support\Composer      $composer
54
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
55
     */
56
    public function __construct(Filesystem $files, Composer $composer)
57
    {
58
        parent::__construct($files);
0 ignored issues
show
Unused Code introduced by
The call to Command::__construct() has too many arguments starting with $files.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
60
        $this->composer = $composer;
61
    }
62
63
    /**
64
     * Execute the console command.
65
     *
66
     * @return void
67
     */
68
    public function fire()
69
    {
70
        /* @var Addon $addon */
71
        if (!$addon = $this->dispatch(new GetAddon($this->getAddonNamespace())))
0 ignored issues
show
Bug introduced by
It seems like $this->getAddonNamespace() targeting Anomaly\Streams\Platform...nd::getAddonNamespace() can also be of type array; however, Anomaly\Streams\Platform...GetAddon::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72
        {
73
            throw new \Exception('Addon could not be found.');
74
        }
75
76
        $path   = $addon->getPath();
77
        $type   = $addon->getType();
78
        $slug   = $addon->getSlug();
79
        $vendor = $addon->getVendor();
80
81
        if ($type != 'module' && $type != 'extension')
82
        {
83
            throw new \Exception('Only {module} and {extension} addon types are allowed!!!');
84
        }
85
86
        /* @var StreamCollection $streams */
87
        $streams = $this->getStreams($slug);
88
89
        $answers = $this->makeQuestion($streams);
90
91
        if (array_search($this->getAllChoice(), $answers) === false)
92
        {
93
            $streams = $streams->filter(
94
                function ($stream) use ($answers)
95
                {
96
                    return array_search(ucfirst($stream->getSlug()), $answers) !== false;
97
                }
98
            );
99
        }
100
101
        $streams->each(
102
            function ($stream) use ($addon)
103
            {
104
                $slug = $stream->getSlug();
105
106
                $this->dispatch(new WriteEntitySeeder(
107
                    $addon,
108
                    $slug,
109
                    $stream->getNamespace()
110
                ));
111
112
                $singular = ucfirst(str_singular($slug));
113
                $path = "{$addon->getPath()}/src/{$singular}/{$singular}Seeder.php";
114
115
                $this->comment("Seeder for {$slug} created successfully.");
116
                $this->line("Path: {$path}");
117
                $this->line('');
118
            }
119
        );
120
121
        $this->dispatch(new WriteAddonSeeder($path, $type, $slug, $vendor, $streams));
122
123
        $this->composer->dumpAutoloads();
124
125
        $this->info('Seeders created successfully.');
126
    }
127
128
    /**
129
     * Gets the addon's stream namespace.
130
     *
131
     * @throws \Exception
132
     * @return string       The stream namespace.
133
     */
134
    public function getAddonNamespace()
135
    {
136
        $namespace = $this->argument('namespace');
137
138
        if (!str_is('*.*.*', $namespace))
0 ignored issues
show
Bug introduced by
It seems like $namespace defined by $this->argument('namespace') on line 136 can also be of type array; however, str_is() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
139
        {
140
            throw new \Exception('The namespace should be snake case and formatted like: {vendor}.{type}.{slug}');
141
        }
142
143
        return $namespace;
144
    }
145
146
    /**
147
     * Gets the root streams of addon.
148
     *
149
     * @param  string             $slug The addon slug
150
     * @return StreamCollection
151
     */
152
    public function getStreams($slug)
153
    {
154
        return $this->dispatch(new GetStreams($slug))->filter(
155
            function ($stream)
156
            {
157
                return !str_contains($stream->getSlug(), '_');
158
            }
159
        );
160
    }
161
162
    /**
163
     * Get `all` value.
164
     *
165
     * @return string All value.
166
     */
167
    public function getAllChoice()
168
    {
169
        return $this->allChoice;
170
    }
171
172
    /**
173
     * Makes a question about streams to make seeders for.
174
     *
175
     * @param  StreamCollection $streams  The streams
176
     * @return array            Answers
177
     */
178
    public function makeQuestion(StreamCollection $streams)
179
    {
180
        $choices = $streams->map(
181
            function ($stream)
182
            {
183
                return ucfirst($stream->getSlug());
184
            }
185
        )->prepend($this->getAllChoice())->toArray();
186
187
        return $this->choice(
188
            'Please choose the addon\'s streams to create seeders for (common separated if multiple)',
189
            $choices, 0, null, true
190
        );
191
    }
192
193
    /**
194
     * Get the options.
195
     *
196
     * @return array
197
     */
198 View Code Duplication
    protected function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    {
200
        return array_merge(
201
            parent::getOptions(),
202
            [
203
                ['stream', null, InputOption::VALUE_OPTIONAL, 'The stream slug.'],
204
                ['shared', null, InputOption::VALUE_NONE, 'Indicates if the addon should be created in shared addons.'],
205
            ]
206
        );
207
    }
208
209
    /**
210
     * Get the console command arguments.
211
     *
212
     * @return array
213
     */
214
    protected function getArguments()
215
    {
216
        return [
217
            ['namespace', InputArgument::REQUIRED, 'The namespace of the addon'],
218
        ];
219
    }
220
}
221