GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 588bb9...372783 )
by Shea
06:17
created

MakeModuleCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Caffeinated\Modules\Console\Generators;
3
4
use Caffeinated\Modules\Modules;
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
10
class MakeModuleCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'make:module
18
        {slug : The slug of the module}
19
        {--Q|quick : Skip the make:module wizard and use default values}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new Caffeinated module and bootstrap it';
27
28
    /**
29
     * Module folders to be created.
30
     *
31
	 * @var array
32
	 */
33
	protected $moduleFolders = [
34
		'Console/',
35
		'Database/',
36
		'Database/Migrations/',
37
		'Database/Seeds/',
38
		'Http/',
39
		'Http/Controllers/',
40
		'Http/Middleware/',
41
		'Http/Requests/',
42
		'Providers/',
43
		'Resources/',
44
		'Resources/Lang/',
45
		'Resources/Views/',
46
	];
47
48
	/**
49
     * Module files to be created.
50
     *
51
	 * @var array
52
	 */
53
	protected $moduleFiles = [
54
		'Database/Seeds/{{namespace}}DatabaseSeeder.php',
55
		'Http/routes.php',
56
		'Providers/{{namespace}}ServiceProvider.php',
57
		'Providers/RouteServiceProvider.php',
58
		'module.json'
59
	];
60
61
	/**
62
     * Module stubs used to populate defined files.
63
     *
64
	 * @var array
65
	 */
66
	protected $moduleStubs = [
67
		'seeder.stub',
68
		'routes.stub',
69
		'moduleserviceprovider.stub',
70
		'routeserviceprovider.stub',
71
		'manifest.stub'
72
	];
73
74
	/**
75
     * The modules instance.
76
     *
77
	 * @var Modules
78
	 */
79
	protected $module;
80
81
	/**
82
     * The filesystem instance.
83
     *
84
	 * @var Filesystem
85
	 */
86
	protected $files;
87
88
    /**
89
     * Array to store the configuration details.
90
     *
91
     * @var array
92
     */
93
    protected $container;
94
95
    /**
96
     * Create a new command instance.
97
     *
98
     * @param Filesystem  $files
99
     * @param Modules  $module
100
     */
101
    public function __construct(Filesystem $files, Modules $module)
102
    {
103
        parent::__construct();
104
105
        $this->files  = $files;
106
        $this->module = $module;
107
    }
108
109
    /**
110
     * Execute the console command.
111
     *
112
     * @return mixed
113
     */
114
    public function fire()
115
    {
116
        $this->container['slug']        = strtolower($this->argument('slug'));
117
        $this->container['name']        = Str::studly($this->container['slug']);
118
        $this->container['namespace']   = Str::studly($this->container['slug']);
119
        $this->container['version']     = '1.0';
120
        $this->container['description'] = 'This is the description for the '.$this->container['name'].' module.';
121
        $this->container['license']     = 'MIT';
122
        $this->container['author']      = ' ';
123
124
        if ($this->option('quick')) {
125
            return $this->generate();
126
        }
127
128
        $this->displayHeader('make_module_introduction');
129
130
        $this->stepOne();
131
    }
132
133
    /**
134
     * Step 1: Configure module manifest.
135
     *
136
     * @return mixed
137
     */
138
    private function stepOne()
139
    {
140
        $this->displayHeader('make_module_step_1');
141
142
        $this->container['name']        = $this->ask('Please enter the name of the module:', $this->container['name']);
143
        $this->container['slug']        = $this->ask('Please enter the slug for the module:', $this->container['slug']);
144
        $this->container['namespace']   = $this->ask('Please enter the namespace for the module:', $this->container['namespace']);
145
        $this->container['version']     = $this->ask('Please enter the module version:', $this->container['version']);
146
        $this->container['description'] = $this->ask('Please enter the description of the module:', $this->container['description']);
147
        $this->container['author']      = $this->ask('Please enter the author of the module:', $this->container['author']);
148
        $this->container['license']     = $this->ask('Please enter the module license:', $this->container['license']);
149
150
        $this->comment('You have provided the following manifest information:');
151
        $this->comment('Name:        '.$this->container['name']);
152
        $this->comment('Slug:        '.$this->container['slug']);
153
        $this->comment('Namespace:   '.$this->container['namespace']);
154
        $this->comment('Version:     '.$this->container['version']);
155
        $this->comment('Description: '.$this->container['description']);
156
        $this->comment('Author:      '.$this->container['author']);
157
        $this->comment('License:     '.$this->container['license']);
158
159
        if ($this->confirm('Do you wish to continue?')) {
160
            $this->comment('Thanks! That\'s all we need.');
161
			$this->comment('Now relax while your module is generated for you.');
162
163
            $this->generate();
164
        } else {
165
            return $this->stepOne();
166
        }
167
168
        return true;
169
    }
170
171
    /**
172
     * Generate the module.
173
     */
174
    protected function generate()
175
    {
176
        $steps = [
177
            'Generating folders...'      => 'generateFolders',
178
            'Generating .gitkeep...'     => 'generateGitkeep',
179
            'Generating files...'        => 'generateFiles',
180
            'Optimizing module cache...' => 'optimizeModules'
181
        ];
182
183
        $progress = new ProgressBar($this->output, count($steps));
184
        $progress->start();
185
186
        foreach ($steps as $message => $function) {
187
            $progress->setMessage($message);
188
189
            $this->$function();
190
191
            $progress->advance();
192
        }
193
194
        $progress->finish();
195
196
        $this->info("\nModule generated successfully.");
197
    }
198
199
    /**
200
	 * Generate defined module folders.
201
	 *
202
	 * @return void
203
	 */
204
	protected function generateFolders()
205
	{
206
		if (! $this->files->isDirectory($this->module->getPath())) {
207
			$this->files->makeDirectory($this->module->getPath());
208
		}
209
210
		$this->files->makeDirectory($this->getModulePath($this->container['slug'], true));
211
212
		foreach ($this->moduleFolders as $folder) {
213
			$this->files->makeDirectory($this->getModulePath($this->container['slug']).$folder);
214
		}
215
	}
216
217
    /**
218
	 * Generate defined module files.
219
	 *
220
	 * @return void
221
	 */
222
	protected function generateFiles()
223
	{
224
		foreach ($this->moduleFiles as $key => $file) {
225
			$file = $this->formatContent($file);
226
227
			$this->files->put($this->getDestinationFile($file), $this->getStubContent($key));
228
		}
229
	}
230
231
    /**
232
     * Generate .gitkeep files within generated folders.
233
     *
234
     * @return null
235
     */
236
    protected function generateGitkeep()
237
    {
238
        $modulePath = $this->getModulePath($this->container['slug']);
239
        foreach ($this->moduleFolders as $folder) {
240
            $gitkeep = $modulePath.$folder.'/.gitkeep';
241
            $this->files->put($gitkeep, '');
242
        }
243
    }
244
245
    /**
246
     * Reset module cache of enabled and disabled modules.
247
     *
248
     * @return void
249
     */
250
    protected function optimizeModules()
251
    {
252
        return $this->callSilent('module:optimize');
253
    }
254
255
    /**
256
	 * Get the path to the module.
257
	 *
258
	 * @param  string $slug
259
	 * @return string
260
	 */
261
	protected function getModulePath($slug = null, $allowNotExists = false)
262
	{
263
		if ($slug) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
264
            return $this->module->getModulePath($slug, $allowNotExists);
0 ignored issues
show
Unused Code introduced by
The call to Modules::getModulePath() has too many arguments starting with $allowNotExists.

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...
265
        }
266
267
		return $this->module->getPath();
268
	}
269
270
	/**
271
	 * Get destination file.
272
	 *
273
	 * @param  string $file
274
	 * @return string
275
	 */
276
	protected function getDestinationFile($file)
277
	{
278
		return $this->getModulePath($this->container['slug']).$this->formatContent($file);
279
	}
280
281
	/**
282
	 * Get stub content by key.
283
	 *
284
	 * @param int  $key
285
	 * @return string
286
	 */
287
	protected function getStubContent($key)
288
	{
289
		return $this->formatContent($this->files->get(__DIR__.'/../../../resources/stubs/'.$this->moduleStubs[$key]));
290
	}
291
292
	/**
293
	 * Replace placeholder text with correct values.
294
	 *
295
	 * @return string
296
	 */
297
	protected function formatContent($content)
298
	{
299
		return str_replace(
300
			['{{slug}}', '{{name}}', '{{namespace}}', '{{version}}', '{{description}}', '{{author}}', '{{path}}'],
301
			[$this->container['slug'], $this->container['name'], $this->container['namespace'], $this->container['version'], $this->container['description'], $this->container['author'], $this->module->getNamespace()],
302
			$content
303
		);
304
	}
305
306
	/**
307
	 * Pull the given stub file contents and display them on screen.
308
	 *
309
	 * @param string  $file
310
	 * @param string  $level
311
	 * @return mixed
312
	 */
313
	protected function displayHeader($file = '', $level = 'info')
314
	{
315
		$stub = $this->files->get(__DIR__.'/../../../resources/stubs/console/'.$file.'.stub');
316
		return $this->$level($stub);
317
	}
318
}
319