Completed
Pull Request — develop (#199)
by
unknown
13:59
created

AddGraph   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 55
ccs 3
cts 18
cp 0.1666
rs 10
wmc 5
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 21 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use File;
7
class AddGraph extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'make:graph
15
                        {filename : The Filename to add}
16
                        {namespace? : The namespace to use (optional) }';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Generate new graph template class to help speed up building new graphs';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @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...
29
     */
30 87
    public function __construct()
31
    {
32 87
        parent::__construct();
33 87
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $filename = $this->argument('filename');
43
        $contents = File::get(storage_path('stubs/graph.stub'));
44
        $contents = str_replace('namespace App\Graphs;', "namespace App\Graphs\\".$this->argument('namespace').";", $contents);
45
        $contents = str_replace('GraphDummy', $this->argument('filename'), $contents);
46
        $filepath =  base_path('app/Graphs/') . str_replace('\\','/',$this->argument('namespace'));
47
48
        if (!file_exists($filepath)) {
49
            File::makeDirectory($filepath,0755,true);
50
        }
51
        if (file_exists($filepath.'/'.ucfirst($this->argument('filename')).'.php')) {
52
            if (!$this->confirm("$filename alredy exists in $filepath, do you want to overwrite it?")) {
53
                $this->error('Exiting...');
54
                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...
55
            }
56
        }
57
        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...
58
59
        $this->info("Created file $filename in $filepath ");
60
    }
61
}
62