Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
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 117
    public function handle()
36
    {
37 117
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
38
39 117 View Code Duplication
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
40 19
            $this->laravel['files']->makeDirectory($dir, 0777, true);
41
        }
42
43 117
        $contents = $this->getTemplateContents();
44
45
        try {
46 117
            $overwriteFile = $this->hasOption('force') ? $this->option('force') : false;
47 117
            (new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate();
48
49 117
            $this->info("Created : {$path}");
50 5
        } catch (FileAlreadyExistException $e) {
51 5
            $this->error("File : {$path} already exists.");
52
        }
53 117
    }
54
55
    /**
56
     * Get class name.
57
     *
58
     * @return string
59
     */
60 117
    public function getClass()
61
    {
62 117
        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
    public function getDefaultNamespace() : string
71
    {
72
        return '';
73
    }
74
75
    /**
76
     * Get class namespace.
77
     *
78
     * @param \Nwidart\Modules\Module $module
79
     *
80
     * @return string
81
     */
82 117
    public function getClassNamespace($module)
83
    {
84 117
        $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
85
86 117
        $extra = str_replace('/', '\\', $extra);
87
88 117
        $namespace = $this->laravel['modules']->config('namespace');
89
90 117
        $namespace .= '\\' . $module->getStudlyName();
91
92 117
        $namespace .= '\\' . $this->getDefaultNamespace();
93
94 117
        $namespace .= '\\' . $extra;
95
96 117
        $namespace = str_replace('/', '\\', $namespace);
97
98 117
        return trim($namespace, '\\');
99
    }
100
}
101