Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Commands/PublishConfigurationCommand.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 Illuminate\Support\Str;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class PublishConfigurationCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'module:publish-config';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Publish a module\'s config files to the application';
25
26
    /**
27
     * Execute the console command.
28
     */
29 View Code Duplication
    public function handle()
30
    {
31
        if ($module = $this->argument('module')) {
32
            $this->publishConfiguration($module);
0 ignored issues
show
It seems like $module defined by $this->argument('module') on line 31 can also be of type array; however, Nwidart\Modules\Commands...:publishConfiguration() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33
34
            return;
35
        }
36
37
        foreach ($this->laravel['modules']->allEnabled() as $module) {
38
            $this->publishConfiguration($module->getName());
39
        }
40
    }
41
42
    /**
43
     * @param string $module
44
     * @return string
45
     */
46
    private function getServiceProviderForModule($module)
47
    {
48
        $namespace = $this->laravel['config']->get('modules.namespace');
49
        $studlyName = Str::studly($module);
50
51
        return "$namespace\\$studlyName\\Providers\\{$studlyName}ServiceProvider";
52
    }
53
54
    /**
55
     * @param string $module
56
     */
57
    private function publishConfiguration($module)
58
    {
59
        $this->call('vendor:publish', [
60
            '--provider' => $this->getServiceProviderForModule($module),
61
            '--force' => $this->option('force'),
62
            '--tag' => ['config'],
63
        ]);
64
    }
65
66
    /**
67
     * Get the console command arguments.
68
     *
69
     * @return array
70
     */
71 120
    protected function getArguments()
72
    {
73
        return [
74 120
            ['module', InputArgument::OPTIONAL, 'The name of module being used.'],
75
        ];
76
    }
77
78
    /**
79
     * @return array
80
     */
81 120
    protected function getOptions()
82
    {
83
        return [
84 120
            ['--force', '-f', InputOption::VALUE_NONE, 'Force the publishing of config files'],
85
        ];
86
    }
87
}
88