Completed
Pull Request — develop (#199)
by
unknown
05:24
created

AddGraph::handle()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 21
ccs 0
cts 15
cp 0
crap 20
rs 9.0534
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use File;
7
8
class AddGraph extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'make:graph
16
                        {filename : The Filename to add}
17
                        {namespace? : The namespace to use (optional) }';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Generate new graph template class to help speed up building new graphs';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31 87
    public function __construct()
32
    {
33 87
        parent::__construct();
34 87
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $filename = $this->argument('filename');
44
        $contents = File::get(storage_path('stubs/graph.stub'));
45
        $contents = str_replace('namespace App\Graphs;', "namespace App\Graphs\\".$this->argument('namespace').";", $contents);
46
        $contents = str_replace('GraphDummy', $this->argument('filename'), $contents);
47
        $filepath = base_path('app/Graphs/') . str_replace('\\','/', $this->argument('namespace'));
48
49
        if (!file_exists($filepath)) {
50
            File::makeDirectory($filepath, 0755, true);
51
        }
52
        if (file_exists($filepath.'/'.ucfirst($this->argument('filename')).'.php')) {
53
            if (!$this->confirm("$filename alredy exists in $filepath, do you want to overwrite it?")) {
54
                $this->error('Exiting...');
55
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
56
            }
57
        }
58
        File::put($filepath.'/'.ucfirst($this->argument('filename')).'.php', $contents, 'private');
0 ignored issues
show
Documentation introduced by
'private' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60
        $this->info("Created file $filename in $filepath");
61
    }
62
}
63