DdCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 2
A isAllowedToRun() 0 8 2
1
<?php
2
3
namespace Spatie\ArtisanDd;
4
5
use Illuminate\Console\Command;
6
7
class DdCommand extends Command
8
{
9
    protected $signature = 'dd {code*}';
10
11
    protected $description = 'Run the given code and dump the result';
12
13
    public function handle()
14
    {
15
        if (! $this->isAllowedToRun()) {
16
            $this->error('This command can only run if the environment variable `ALLOW_DD_COMMAND` is set to `true` or in local environment');
17
18
            return;
19
        }
20
21
        return collect($this->argument('code'))
22
            ->map(function (string $command) {
23
                return rtrim($command, ';');
24
            })
25
            ->map(function (string $sanitizedCommand) {
26
                return eval("dump({$sanitizedCommand});");
27
            })
28
            ->implode(PHP_EOL);
29
    }
30
31
    protected function isAllowedToRun(): bool
32
    {
33
        if (env('ALLOW_DD_COMMAND') === true) {
34
            return true;
35
        }
36
37
        return app()->environment('local');
38
    }
39
}
40