Completed
Push — master ( 86e937...b4da1d )
by Song
02:27
created

ConfigCommand::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Arr;
7
8
class ConfigCommand extends Command
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'admin:config {path?}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Compare the difference between the admin config file and the original';
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function handle()
28
    {
29
        $path = $this->argument('path') ?: 'config/admin.php';
30
31
        $current  = require $path;
32
        $original = require __DIR__.'/../../config/admin.php';
33
34
        $added   = $this->diff($current, $original);
35
        $removed = $this->diff($original, $current);
36
37
        if ($added->isEmpty() && $removed->isEmpty()) {
38
            $this->info('Configuration items have not been modified');
39
            return;
40
        }
41
42
        $this->line("The admin config file `$path`:");
43
44
        $this->printDiff('Added', $added);
45
        $this->printDiff('Removed', $removed, true);
46
47
        $this->line('');
48
        $this->comment('Please open `vendor/encore/laravel-admin/config/admin.php` to check the difference');
49
    }
50
51
    protected function diff(array $from, array $to)
52
    {
53
        return collect(Arr::dot($from))
0 ignored issues
show
Documentation introduced by
$from is of type array, but the function expects a object<Illuminate\Support\iterable>.

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...
54
            ->keys()
55
            ->reject(function ($key) use ($to) {
56
                return Arr::has($to, $key);
57
            });
58
    }
59
60
    protected function printDiff($title, $diff, $error = false)
61
    {
62
        if ($diff->isEmpty()) {
63
            return;
64
        }
65
66
        $this->line('');
67
        $this->comment("{$title}:");
68
69
        $diff->each(function ($key) use ($error) {
70
            if ($error) {
71
                $this->error("    {$key}");
72
            } else {
73
                $this->info("    {$key}");
74
            }
75
        });
76
    }
77
}
78