Passed
Push — main ( 3fef0f...9c29d1 )
by Dimitri
20:53 queued 16:41
created

ConfigCheck::execute()   C

Complexity

Conditions 13
Paths 49

Size

Total Lines 70
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 43
c 1
b 0
f 0
nc 49
nop 1
dl 0
loc 70
ccs 0
cts 27
cp 0
crap 182
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Utilities;
13
14
use Ahc\Cli\Output\Color;
15
use BlitzPHP\Cli\Console\Command;
16
use Kint\Kint;
17
18
/**
19
 * Verifie les valeurs d'une configuartion.
20
 */
21
class ConfigCheck extends Command
22
{
23
    /**
24
     * @var string Groupe
25
     */
26
    protected $group = 'BlitzPHP';
27
28
    protected $service = 'Service de configuration';
29
30
    /**
31
     * @var string Nom
32
     */
33
    protected $name = 'config:check';
34
35
    /**
36
     * @var string Description
37
     */
38
    protected $description = 'Vérifie les valeurs d\'un fichier de configuration.';
39
40
    /**
41
     * Arguments de la commande
42
     *
43
     * @var array<string, string>
44
     */
45
    protected $arguments = [
46
        'config' => 'La configuration dont on souhaite vérifier les valeurs.',
47
    ];
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function execute(array $params)
53
    {
54
        if (empty($file = strtolower($this->argument('config', '')))) {
55
            $this->fail('Vous devez spécifier la configuration à utiliser pour la vérification.')->eol();
56
            $this->write('  Usage: ' . $this->usage)->eol();
57
            $this->write('Exemple: config:check app')->eol();
58
            $this->write('         config:check \'BlitzPHP\Schild\Config\auth\'');
59
60
            return EXIT_ERROR;
61
        }
62
63
        if (null === $config = config()->get($file)) {
64
            $this->fail('Aucune configuration trouvée pour: ' . $file);
65
        }
66
67
        $this->center('Valeurs de la configuration ' . $this->color->ok($file));
68
69
        $this->border()->eol();
70
71
        $others = [];
72
73
        foreach ($config as $key => $val) {
74
            $options = ['fg' => Color::CYAN];
75
76
            if (is_scalar($val)) {
77
                if (is_bool($val)) {
78
                    if ($val === true) {
79
                        $options['fg'] = Color::GREEN;
80
                        $val           = 'Enabled';
81
                    } else {
82
                        $options['fg'] = Color::YELLOW;
83
                        $val           = 'Disabled';
84
                    }
85
                } elseif ('' === $val) {
86
                    $others[$key] = $val;
87
88
                    continue;
89
                }
90
91
                $this->justify($key, $val, ['second' => $options]);
92
            } else {
93
                if (empty($val = (array) $val)) {
94
                    $others[$key] = $val;
95
96
                    continue;
97
                }
98
                if (array_is_list($val)) {
99
                    $options = ['fg' => Color::PURPLE];
100
                    $this->justify($key, implode(', ', array_values($val)), ['second' => $options]);
101
                } else {
102
                    $others[$key] = $val;
103
104
                    continue;
105
                }
106
            }
107
        }
108
109
        if ($others !== []) {
110
            $this->eol()->task('Autres configuration')->eol();
111
112
            if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
113
                $this->write($this->getKintDump($others));
114
            } else {
115
                $this->write(
116
                    $this->color->line($this->getVarDump($others), ['fg' => Color::CYAN])
117
                );
118
            }
119
        }
120
121
        return EXIT_SUCCESS;
122
    }
123
124
    /**
125
     * Obtiens le dump de la config via la function d() de Kint
126
     */
127
    private function getKintDump(array $config): string
128
    {
129
        ob_start();
130
        d($config);
131
        $output = ob_get_clean();
132
133
        $output = trim($output);
134
135
        $lines = explode("\n", $output);
136
        array_splice($lines, 0, 3);
137
        array_splice($lines, -3);
138
139
        return implode("\n", $lines);
140
    }
141
142
    /**
143
     * Obtiens le dump de la config via la function var_dump() de PHP
144
     */
145
    private function getVarDump(array $config): string
146
    {
147
        ob_start();
148
        var_dump($config);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($config) looks like debug code. Are you sure you do not want to remove it?
Loading history...
149
        $output = ob_get_clean();
150
151
        return preg_replace(
152
            '!.*Commands/Utilities/ConfigCheck.php.*\n!u',
153
            '',
154
            $output
155
        );
156
    }
157
}
158