Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

BabelCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
dl 0
loc 100
rs 10
c 1
b 0
f 1
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 1
A strlen() 0 7 2
A getColumnWidth() 0 12 4
A listBabelCommands() 0 15 3
1
<?php
2
3
namespace App\Console\Commands\Babel;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Artisan;
7
use Illuminate\Support\Str;
8
9
class BabelCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'babel';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'List all babel commands';
24
25
    /**
26
     * @var string
27
     */
28
    public static $logo = <<<LOGO
29
30
███╗   ██╗ ██████╗      ██╗    ██████╗  █████╗ ██████╗ ███████╗██╗
31
████╗  ██║██╔═══██╗     ██║    ██╔══██╗██╔══██╗██╔══██╗██╔════╝██║
32
██╔██╗ ██║██║   ██║     ██║    ██████╔╝███████║██████╔╝█████╗  ██║
33
██║╚██╗██║██║   ██║██   ██║    ██╔══██╗██╔══██║██╔══██╗██╔══╝  ██║
34
██║ ╚████║╚██████╔╝╚█████╔╝    ██████╔╝██║  ██║██████╔╝███████╗███████╗
35
╚═╝  ╚═══╝ ╚═════╝  ╚════╝     ╚═════╝ ╚═╝  ╚═╝╚═════╝ ╚══════╝╚══════╝
36
37
LOGO;
38
39
    /**
40
     * Execute the console command.
41
     */
42
    public function handle()
43
    {
44
        $this->line(static::$logo);
45
        $this->line(sprintf('NOJ <comment>version</comment> <info>%s</info>',version()));
46
47
        $this->comment('');
48
        $this->comment('Available commands:');
49
50
        $this->listBabelCommands();
51
    }
52
53
    /**
54
     * List all babel commands.
55
     *
56
     * @return void
57
     */
58
    protected function listBabelCommands()
59
    {
60
        $commands = collect(Artisan::all())->mapWithKeys(function ($command, $key) {
61
            if (Str::startsWith($key, 'babel:')) {
62
                return [$key => $command];
63
            }
64
65
            return [];
66
        })->toArray();
67
68
        $width = $this->getColumnWidth($commands);
69
70
        /** @var Command $command */
71
        foreach ($commands as $command) {
72
            $this->line(sprintf(" %-{$width}s %s", $command->getName(), $command->getDescription()));
73
        }
74
    }
75
76
    /**
77
     * @param (Command|string)[] $commands
78
     *
79
     * @return int
80
     */
81
    private function getColumnWidth(array $commands)
82
    {
83
        $widths = [];
84
85
        foreach ($commands as $command) {
86
            $widths[] = static::strlen($command->getName());
87
            foreach ($command->getAliases() as $alias) {
88
                $widths[] = static::strlen($alias);
89
            }
90
        }
91
92
        return $widths ? max($widths) + 2 : 0;
93
    }
94
95
    /**
96
     * Returns the length of a string, using mb_strwidth if it is available.
97
     *
98
     * @param string $string The string to check its length
99
     *
100
     * @return int The length of the string
101
     */
102
    public static function strlen($string)
103
    {
104
        if (false === $encoding = mb_detect_encoding($string, null, true)) {
105
            return strlen($string);
106
        }
107
108
        return mb_strwidth($string, $encoding);
109
    }
110
}
111