Completed
Push — master ( dcc042...9e46d5 )
by Nicolas
16:30
created

src/Commands/GeneratorCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
7
use Nwidart\Modules\Generators\FileGenerator;
8
9
abstract class GeneratorCommand extends Command
10
{
11
    /**
12
     * The name of 'name' argument.
13
     *
14
     * @var string
15
     */
16
    protected $argumentName = '';
17
18
    /**
19
     * Get template contents.
20
     *
21
     * @return string
22
     */
23
    abstract protected function getTemplateContents();
24
25
    /**
26
     * Get the destination file path.
27
     *
28
     * @return string
29
     */
30
    abstract protected function getDestinationFilePath();
31
32
    /**
33
     * Execute the console command.
34
     */
35 115
    public function handle()
36
    {
37 115
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
38
39 115 View Code Duplication
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
40 19
            $this->laravel['files']->makeDirectory($dir, 0777, true);
41
        }
42
43 115
        $contents = $this->getTemplateContents();
44
45
        try {
46 115
            $overwriteFile = $this->hasOption('force') ? $this->option('force') : false;
47 115
            (new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate();
48
49 115
            $this->info("Created : {$path}");
50 5
        } catch (FileAlreadyExistException $e) {
51 5
            $this->error("File : {$path} already exists.");
52
        }
53 115
    }
54
55
    /**
56
     * Get class name.
57
     *
58
     * @return string
59
     */
60 115
    public function getClass()
61
    {
62 115
        return class_basename($this->argument($this->argumentName));
0 ignored issues
show
It seems like $this->argument($this->argumentName) targeting Illuminate\Console\Command::argument() can also be of type array or null; however, class_basename() does only seem to accept string|object, 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...
63
    }
64
65
    /**
66
     * Get default namespace.
67
     *
68
     * @return string
69
     */
70 7
    public function getDefaultNamespace() : string
71
    {
72 7
        return '';
73
    }
74
75
    /**
76
     * Get class namespace.
77
     *
78
     * @param \Nwidart\Modules\Module $module
79
     *
80
     * @return string
81
     */
82 115
    public function getClassNamespace($module)
83
    {
84 115
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
85
86 115
        $extra = str_replace('/', '\\', $extra);
87
88 115
        $namespace = $this->laravel['modules']->config('namespace');
89
90 115
        $namespace .= '\\' . $module->getStudlyName();
91
92 115
        $namespace .= '\\' . $this->getDefaultNamespace();
93
94 115
        $namespace .= '\\' . $extra;
95
96 115
        $namespace = str_replace('/', '\\', $namespace);
97
98 115
        return trim($namespace, '\\');
99
    }
100
}
101