Completed
Push — master ( 0dfd17...a38508 )
by Sherif
02:04
created

MakeModuleCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 236
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 13 1
A generate() 0 25 2
C generateModule() 0 53 11
A replacePlaceholders() 0 43 1
A optimizeModules() 0 4 1
A generateMigration() 0 5 1
1
<?php
2
3
namespace App\Modules\Core\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Caffeinated\Modules\RepositoryManager;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
11
class MakeModuleCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'generate:module
19
        {slug : The slug of the module}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new module';
27
28
    /**
29
     * The modules instance.
30
     *
31
     * @var RepositoryManager
32
     */
33
    protected $module;
34
35
    /**
36
     * The filesystem instance.
37
     *
38
     * @var Filesystem
39
     */
40
    protected $files;
41
42
    /**
43
     * Array to store the configuration details.
44
     *
45
     * @var array
46
     */
47
    protected $container;
48
49
    /**
50
     * Array of folder mappings.
51
     *
52
     * @var Array
53
     */
54
    protected $mapping = [
55
        'Database/Factories'  => 'Database/Factories',
56
        'Database/Migrations' => 'Database/Migrations',
57
        'Database/Seeds'      => 'Database/Seeds',
58
        'Http/Controllers'    => 'Http/Controllers',
59
        'Http/Requests'       => 'Http/Requests',
60
        'Http/Resources'      => 'Http/Resources',
61
        'ModelObservers'      => 'ModelObservers',
62
        'Providers'           => 'Providers',
63
        'Repositories'        => 'Repositories',
64
        'Routes'              => 'Routes'
65
    ];
66
67
    /**
68
     * Create a new command instance.
69
     *
70
     * @param Filesystem $files
71
     * @param RepositoryManager $module
72
     */
73
    public function __construct(Filesystem $files, RepositoryManager $module)
74
    {
75
        parent::__construct();
76
77
        $this->files = $files;
78
        $this->module = $module;
79
    }
80
81
    /**
82
     * Execute the console command.
83
     *
84
     * @return mixed
85
     */
86
    public function handle()
87
    {
88
        $this->container['slug']        = Str::slug($this->argument('slug'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('slug') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, Illuminate\Support\Str::slug() 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...
89
        $this->container['name']        = Str::studly($this->container['slug']);
90
        $this->container['version']     = '1.0';
91
        $this->container['description'] = 'This is the description for the ' . $this->container['name'] . ' module.';
92
        $this->container['location']    = config('modules.default_location');
93
        $this->container['provider']    = config("modules.locations.{$this->container['location']}.provider");
94
        $this->container['basename']    = Str::studly($this->container['slug']);
95
        $this->container['namespace']   = config("modules.locations.{$this->container['location']}.namespace").$this->container['basename'];
96
97
        return $this->generate();
98
    }
99
100
    /**
101
     * Generate the module.
102
     */
103
    protected function generate()
104
    {
105
        $steps = [
106
            'Generating module...' => 'generateModule',
107
            'Optimizing module cache...' => 'optimizeModules',
108
            'Generating migrations...' => 'generateMigration',
109
        ];
110
111
        $progress = new ProgressBar($this->output, count($steps));
112
        $progress->start();
113
114
        foreach ($steps as $message => $function) {
115
            $progress->setMessage($message);
116
117
            $this->$function();
118
119
            $progress->advance();
120
        }
121
122
        $progress->finish();
123
124
        event($this->container['slug'] . '.module.made');
125
126
        $this->info("\nModule generated successfully.");
127
    }
128
129
    /**
130
     * Generate defined module folders.
131
     */
132
    protected function generateModule()
133
    {
134
        $location = $this->container['location'];
135
        $root     = module_path(null, '', $location);
136
        $manifest = config("modules.locations.$location.manifest") ?: 'module.json';
137
        $provider = config("modules.locations.$location.provider") ?: 'ModuleServiceProvider';
138
139
        if (!$this->files->isDirectory($root)) {
140
            $this->files->makeDirectory($root);
141
        }
142
143
        $directory = module_path(null, $this->container['basename'], $location);
144
        $source    = __DIR__ . '/Stubs/Module';
145
146
        $this->files->makeDirectory($directory);
147
148
        $sourceFiles = $this->files->allFiles($source, true);
149
150
        if (!empty($this->mapping)) {
151
            $search = array_keys($this->mapping);
152
            $replace = array_values($this->mapping);
153
        }
154
155
        foreach ($sourceFiles as $file) {
156
            $contents = $this->replacePlaceholders($file->getContents());
157
            $subPath = $file->getRelativePathname();
158
159
            if (!empty($this->mapping)) {
160
                $subPath = str_replace($search, $replace, $subPath);
0 ignored issues
show
Bug introduced by
The variable $search does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $replace does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
161
            }
162
163
            $filePath = $directory . '/' . $subPath;
164
            
165
            // if the file is module.json, replace it with the custom manifest file name
166
            if ($file->getFilename() === 'module.json' && $manifest) {
167
                $filePath = str_replace('module.json', $manifest, $filePath);
168
            }
169
            
170
            // if the file is ModuleServiceProvider.php, replace it with the custom provider file name
171
            if ($file->getFilename() === 'ModuleServiceProvider.php') {
172
                $filePath = str_replace('ModuleServiceProvider', $provider, $filePath);
173
            }
174
            $filePath = $this->replacePlaceholders($filePath);
175
            
176
            $dir = dirname($filePath);
177
            
178
            if (! $this->files->isDirectory($dir)) {
179
                $this->files->makeDirectory($dir, 0755, true);
180
            }
181
182
            $this->files->put($filePath, $contents);
183
        }
184
    }
185
186
    protected function replacePlaceholders($contents)
187
    {
188
        $modelName = \Str::camel($this->container['slug']);
189
        $modelNameSingular = \Str::singular($modelName);
190
191
        $find = [
192
            'DummyFactory',
193
            'DummyModelName',
194
            'DummyModuleSlug',
195
            'DummyModule',
196
            'DummyModel',
197
            'DummyDatabaseSeeder',
198
            'DummyTableSeeder',
199
            'DummyController',
200
            'DummyRepository',
201
            'InsertDummy',
202
            'UpdateDummy',
203
            'DummyResource',
204
            'DummyObserver',
205
            'DummyTableName',
206
            'DummyRoutePrefix',
207
        ];
208
209
        $replace = [
210
            ucfirst($modelNameSingular) . 'Factory',
211
            $modelNameSingular,
212
            $this->container['slug'],
213
            ucfirst($modelName),
214
            ucfirst($modelNameSingular),
215
            ucfirst($modelName) . 'DatabaseSeeder',
216
            ucfirst($modelName) . 'TableSeeder',
217
            ucfirst($modelNameSingular) . 'Controller',
218
            ucfirst($modelNameSingular) . 'Repository',
219
            'Insert' . ucfirst($modelNameSingular),
220
            'Update' . ucfirst($modelNameSingular),
221
            ucfirst($modelNameSingular),
222
            ucfirst($modelNameSingular) . 'Observer',
223
            $modelName,
224
            $modelName,
225
        ];
226
227
        return str_replace($find, $replace, $contents);
228
    }
229
230
    /**
231
     * Reset module cache of enabled and disabled modules.
232
     */
233
    protected function optimizeModules()
234
    {
235
        return $this->callSilent('module:optimize');
236
    }
237
238
    /**
239
     * Generate table migrations.
240
     */
241
    protected function generateMigration()
242
    {
243
        $modelName = $this->container['slug'];
244
        return $this->callSilent('make:module:migration', ['slug' => $modelName, 'name' => 'create_' . $modelName . '_table']);
245
    }
246
}
247