AddCustomRouteContent   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 99
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 43 5
A customRoutesFileEndLine() 0 22 3
1
<?php
2
3
namespace Backpack\Base\app\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
use Symfony\Component\Process\Exception\ProcessFailedException;
8
use Symfony\Component\Process\Process;
9
10
class AddCustomRouteContent extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'backpack:base:add-custom-route
18
                                {code : HTML/PHP code that registers a route. Use either single quotes or double quotes. Never both. }';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Add HTML/PHP code to the routes/backpack/custom.php file';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @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...
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $path = 'routes/backpack/custom.php';
45
        $disk_name = config('backpack.base.root_disk_name');
46
        $disk = Storage::disk($disk_name);
47
        $code = $this->argument('code');
48
49
        if ($disk->exists($path)) {
50
            $old_file_path = $disk->path($path);
51
52
            // insert the given code before the file's last line
53
            $file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES);
54
            $end_line_number = $this->customRoutesFileEndLine($file_lines);
55
            $file_lines[$end_line_number + 1] = $file_lines[$end_line_number];
56
            $file_lines[$end_line_number] = '    '.$code;
57
            $new_file_content = implode(PHP_EOL, $file_lines);
58
59
            if ($disk->put($path, $new_file_content)) {
60
                $this->info('Successfully added code to '.$path);
61
            } else {
62
                $this->error('Could not write to file: '.$path);
63
            }
64
        } else {
65
            $command = 'php artisan vendor:publish --provider="Backpack\Base\BaseServiceProvider" --tag=custom_routes';
66
67
            $process = new Process($command, null, null, null, 300, null);
0 ignored issues
show
Unused Code introduced by
The call to Process::__construct() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Documentation introduced by
$command is of type string, but the function expects a array.

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...
68
69
            $process->run(function ($type, $buffer) {
70
                if (Process::ERR === $type) {
71
                    $this->line($buffer);
72
                } else {
73
                    $this->line($buffer);
74
                }
75
            });
76
77
            // executes after the command finishes
78
            if (!$process->isSuccessful()) {
79
                throw new ProcessFailedException($process);
80
            }
81
82
            $this->handle();
83
        }
84
    }
85
86
    private function customRoutesFileEndLine($file_lines)
87
    {
88
        // in case the last line has not been modified at all
89
        $end_line_number = array_search('}); // this should be the absolute last line of this file', $file_lines);
90
91
        if ($end_line_number) {
92
            return $end_line_number;
93
        }
94
95
        // otherwise, in case the last line HAS been modified
96
        // return the last line that has an ending in it
97
        $possible_end_lines = array_filter($file_lines, function ($k) {
98
            return strpos($k, '});') === 0;
99
        });
100
101
        if ($possible_end_lines) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $possible_end_lines of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
102
            end($possible_end_lines);
103
            $end_line_number = key($possible_end_lines);
104
105
            return $end_line_number;
106
        }
107
    }
108
}
109