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 ( 7270bd...00b768 )
by Shea
08:40 queued 04:07
created

MakeModuleCommand::getDestinationFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
use Symfony\Component\Console\Input\InputArgument;
10
11
class MakeModuleCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'make:module';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new Caffeinated module and bootstrap it';
26
27
    /**
28
     * Module folders to be created.
29
     *
30
	 * @var array
31
	 */
32
	protected $moduleFolders = [
33
		'Console/',
34
		'Database/',
35
		'Database/Migrations/',
36
		'Database/Seeds/',
37
		'Http/',
38
		'Http/Controllers/',
39
		'Http/Middleware/',
40
		'Http/Requests/',
41
		'Providers/',
42
		'Resources/',
43
		'Resources/Lang/',
44
		'Resources/Views/',
45
	];
46
47
	/**
48
     * Module files to be created.
49
     *
50
	 * @var array
51
	 */
52
	protected $moduleFiles = [
53
		'Database/Seeds/{{namespace}}DatabaseSeeder.php',
54
		'Http/routes.php',
55
		'Providers/{{namespace}}ServiceProvider.php',
56
		'Providers/RouteServiceProvider.php',
57
		'module.json'
58
	];
59
60
	/**
61
     * Module stubs used to populate defined files.
62
     *
63
	 * @var array
64
	 */
65
	protected $moduleStubs = [
66
		'seeder.stub',
67
		'routes.stub',
68
		'moduleserviceprovider.stub',
69
		'routeserviceprovider.stub',
70
		'manifest.stub'
71
	];
72
73
	/**
74
     * The modules instance.
75
     *
76
	 * @var Modules
77
	 */
78
	protected $module;
79
80
	/**
81
     * The filesystem instance.
82
     *
83
	 * @var Filesystem
84
	 */
85
	protected $files;
86
87
    /**
88
     * Array to store the configuration details.
89
     *
90
     * @var array
91
     */
92
    protected $container;
93
94
    /**
95
     * Create a new command instance.
96
     *
97
     * @param Filesystem  $files
98
     * @param Modules  $module
99
     */
100
    public function __construct(Filesystem $files, Modules $module)
101
    {
102
        parent::__construct();
103
104
        $this->files  = $files;
105
        $this->module = $module;
106
    }
107
108
    /**
109
     * Execute the console command.
110
     *
111
     * @return mixed
112
     */
113
    public function fire()
114
    {
115
        $this->container['slug']      = strtolower($this->argument('slug'));
116
        $this->container['name']      = Str::studly($this->container['slug']);
117
        $this->container['namespace'] = Str::studly($this->container['slug']);
118
119
        $this->displayHeader('make_module_introduction');
120
121
        $this->stepOne();
122
    }
123
124
    /**
125
     * Step 1: Configure module manifest.
126
     *
127
     * @return mixed
128
     */
129
    private function stepOne()
130
    {
131
        $this->displayHeader('make_module_step_1');
132
133
        $this->container['name']        = $this->ask('Please enter the name of the module:', $this->container['name']);
134
        $this->container['slug']        = $this->ask('Please enter the slug for the module:', $this->container['slug']);
135
        $this->container['namespace']   = $this->ask('Please enter the namespace for the module:', $this->container['namespace']);
136
        $this->container['version']     = $this->ask('Please enter the module version:', '1.0');
137
        $this->container['description'] = $this->ask('Please enter the description of the module:', 'This is the description for the '.$this->container['name'].' module.');
138
        $this->container['author']      = $this->ask('Please enter the author of the module:', ' ');
139
        $this->container['license']     = $this->ask('Please enter the module license:', 'MIT');
140
141
        $this->comment('You have provided the following manifest information:');
142
        $this->comment('Name:        '.$this->container['name']);
143
        $this->comment('Slug:        '.$this->container['slug']);
144
        $this->comment('Namespace:   '.$this->container['namespace']);
145
        $this->comment('Version:     '.$this->container['version']);
146
        $this->comment('Description: '.$this->container['description']);
147
        $this->comment('Author:      '.$this->container['author']);
148
        $this->comment('License:     '.$this->container['license']);
149
150
        if ($this->confirm('Do you wish to continue?')) {
151
            $this->comment('Thanks! That\'s all we need.');
152
			$this->comment('Now relax while your module is generated for you.');
153
154
            $this->generate();
155
        } else {
156
            return $this->stepOne();
157
        }
158
159
        return true;
160
    }
161
162
    /**
163
     * Generate the module.
164
     */
165
    protected function generate()
166
    {
167
        $steps = [
168
            'Generating folders...'  => 'generateFolders',
169
            'Generating .gitkeep...' => 'generateGitkeep',
170
            'Generating Files...'    => 'generateFiles',
171
            'Optimizing Laravel...'  => 'optimizeLaravel'
172
        ];
173
174
        $progress = new ProgressBar($this->output, count($steps));
175
        $progress->start();
176
177
        foreach ($steps as $message => $function) {
178
            $progress->setMessage($message);
179
180
            $this->$function();
181
182
            $progress->advance();
183
        }
184
185
        $progress->finish();
186
        $this->info("\nModule generated successfully.");
187
    }
188
189
    /**
190
	 * Generate defined module folders.
191
	 *
192
	 * @return void
193
	 */
194
	protected function generateFolders()
195
	{
196
		if (! $this->files->isDirectory($this->module->getPath())) {
197
			$this->files->makeDirectory($this->module->getPath());
198
		}
199
200
		$this->files->makeDirectory($this->getModulePath($this->container['slug'], true));
201
202
		foreach ($this->moduleFolders as $folder) {
203
			$this->files->makeDirectory($this->getModulePath($this->container['slug']).$folder);
204
		}
205
	}
206
207
    /**
208
	 * Generate defined module files.
209
	 *
210
	 * @return void
211
	 */
212
	protected function generateFiles()
213
	{
214
		foreach ($this->moduleFiles as $key => $file) {
215
			$file = $this->formatContent($file);
216
217
			$this->files->put($this->getDestinationFile($file), $this->getStubContent($key));
218
		}
219
	}
220
221
    /**
222
     * Generate .gitkeep files within generated folders.
223
     *
224
     * @return null
225
     */
226
    protected function generateGitkeep()
227
    {
228
        $modulePath = $this->getModulePath($this->container['slug']);
229
        foreach ($this->moduleFolders as $folder) {
230
            $gitkeep = $modulePath.$folder.'/.gitkeep';
231
            $this->files->put($gitkeep, '');
232
        }
233
    }
234
235
    /**
236
     * Optimize Laravel for better performance.
237
     *
238
     * @return void
239
     */
240
    protected function optimizeLaravel()
241
    {
242
        return $this->callSilent('optimize');
243
    }
244
245
    /**
246
	 * Get the path to the module.
247
	 *
248
	 * @param  string $slug
249
	 * @return string
250
	 */
251
	protected function getModulePath($slug = null, $allowNotExists = false)
252
	{
253
		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...
254
            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...
255
        }
256
257
		return $this->module->getPath();
258
	}
259
260
	/**
261
	 * Get destination file.
262
	 *
263
	 * @param  string $file
264
	 * @return string
265
	 */
266
	protected function getDestinationFile($file)
267
	{
268
		return $this->getModulePath($this->container['slug']).$this->formatContent($file);
269
	}
270
271
	/**
272
	 * Get stub content by key.
273
	 *
274
	 * @param int  $key
275
	 * @return string
276
	 */
277
	protected function getStubContent($key)
278
	{
279
		return $this->formatContent($this->files->get(__DIR__.'/../../../resources/stubs/'.$this->moduleStubs[$key]));
280
	}
281
282
	/**
283
	 * Replace placeholder text with correct values.
284
	 *
285
	 * @return string
286
	 */
287
	protected function formatContent($content)
288
	{
289
		return str_replace(
290
			['{{slug}}', '{{name}}', '{{namespace}}', '{{version}}', '{{description}}', '{{author}}', '{{path}}'],
291
			[$this->container['slug'], $this->container['name'], $this->container['namespace'], $this->container['version'], $this->container['description'], $this->container['author'], $this->module->getNamespace()],
292
			$content
293
		);
294
	}
295
296
	/**
297
	 * Pull the given stub file contents and display them on screen.
298
	 *
299
	 * @param string  $file
300
	 * @param string  $level
301
	 * @return mixed
302
	 */
303
	protected function displayHeader($file = '', $level = 'info')
304
	{
305
		$stub = $this->files->get(__DIR__.'/../../../resources/stubs/console/'.$file.'.stub');
306
		return $this->$level($stub);
307
	}
308
309
    /**
310
     * Get the console command arguments.
311
     *
312
     * @return array
313
     */
314
    protected function getArguments()
315
    {
316
        return [
317
            ['slug', InputArgument::REQUIRED, 'The slug of the module']
318
        ];
319
    }
320
}
321