Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#15)
by Owen
02:08
created

CrudOverwrite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
7
class CrudOverwrite extends Command
8
{
9
    /**
10
     * The console command name.
11
     *
12
     * @var string
13
     */
14
    protected $name = 'backpack:overwrite';
15
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'backpack:overwrite
22
                            {type : what are you overwriting? field/column/button}
23
                            {name : name of the field}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Publishes a built in field/column/button for modification';
31
32
    public $packageDir = 'vendor/backpack/crud/src/resources/views/';
33
    public $appDir = 'resources/views/vendor/backpack/crud/';
34
35
    /**
36
     * Create a new command instance.
37
     *
38
     * @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...
39
     */
40
    public function __construct()
41
    {
42
        parent::__construct();
43
    }
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return mixed
49
     */
50
    public function handle()
51
    {
52
        $this->type = strtolower($this->argument('type'));
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
        $this->name = strtolower($this->argument('name'));
54
55
        switch ($this->type) {
56
            case 'field':
57
            case 'fields':
58
                return $this->publishField();
59
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
60
            case 'button':
61
            case 'buttons':
62
                return $this->publishButton();
63
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64
            case 'column':
65
            case 'columns':
66
                return $this->publishColumn();
67
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
68
            default:
69
                return $this->error('Please pick from field, column or button');
70
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
71
        }
72
    }
73
74
    protected function processPublish($file, $label)
75
    {
76
        $sourceFile = $this->packageDir.$file.'.blade.php';
77
        $copiedFile = $this->appDir.$file.'.blade.php';
78
79
        if (! file_exists($sourceFile)) {
80
            return $this->error(
81
                'Cannot find source '.$label.' at '
82
                .$sourceFile.
83
                ' - make sure you\'ve picked a real '.$label.' type'
84
            );
85
        } else {
86
            $canCopy = true;
87
88
            if (file_exists($copiedFile)) {
89
                $canCopy = $this->confirm(
90
                    'File already exists at '
91
                    .$copiedFile.
92
                    ' - do you want to overwrite it?'
93
                );
94
            }
95
96
            if ($canCopy) {
97
                $path = pathinfo($copiedFile);
98
99
                if (! file_exists($path['dirname'])) {
100
                    mkdir($path['dirname'], 0755, true);
101
                }
102
103
                if (copy($sourceFile, $copiedFile)) {
104
                    $this->info('Copied to '.$copiedFile);
105
                } else {
106
                    return $this->error(
107
                        'Failed to copy '
108
                        .$sourceFile.
109
                        ' to '
110
                        .$copiedFile.
111
                        ' for unknown reason'
112
                    );
113
                }
114
            }
115
        }
116
    }
117
118
    protected function publishField()
119
    {
120
        return $this->processPublish('fields/'.$this->name, 'field');
121
    }
122
123
    protected function publishColumn()
124
    {
125
        return $this->processPublish('columns/'.$this->name, 'column');
126
    }
127
128
    protected function publishButton()
129
    {
130
        return $this->processPublish('buttons/'.$this->name, 'button');
131
    }
132
}
133