MakeConfig::run()   B
last analyzed

Complexity

Conditions 6
Paths 14

Size

Total Lines 53
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 53
rs 8.8977
cc 6
nc 14
nop 1

How to fix   Long Method   

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 namespace Hafiz\Commands;
2
3
use CodeIgniter\CLI\BaseCommand;
4
use CodeIgniter\CLI\CLI;
5
use Hafiz\Libraries\FileHandler;
6
7
/**
8
 * Make Command class for create a empty skeleton
9
 * Configuration File on specific namespace location
10
 *
11
 * Class MakeConfig
12
 * @package Hafiz\Commands
13
 */
14
class MakeConfig extends BaseCommand
15
{
16
    /**
17
     * The group command is heading under all
18
     * commands will be listed
19
     * @var string
20
     */
21
    protected $group = 'CI4-Recharge';
22
23
    /**
24
     * The Command's name
25
     * @var string
26
     */
27
    protected $name = 'make:config';
28
29
    /**
30
     * The Command's short description
31
     * @var string
32
     */
33
    protected $description = 'Creates a Configuration file.';
34
35
    /**
36
     * The Command's usage
37
     * @var string
38
     */
39
    protected $usage = 'make:config [config_name] [Options]';
40
41
    /**
42
     * The Command's Arguments
43
     * @var array
44
     */
45
    protected $arguments = [
46
        'config_name' => 'The configuration file name',
47
    ];
48
49
    /**
50
     * The Command's Options
51
     * @var array
52
     */
53
    protected $options = [
54
        '-n' => 'Set Configuration namespace',
55
    ];
56
57
    /**
58
     * Creates a new configuration file with The current timestamp.
59
     * @param array $params
60
     * @return void
61
     */
62
    public function run(array $params = [])
63
    {
64
        /**
65
         * Calling all Library and helpers
66
         */
67
        helper(['inflector', 'filesystem']);
68
        $file = new FileHandler();
69
70
        /**
71
         * Input Configuration name from input
72
         */
73
        $name = array_shift($params);
74
75
        //if namespace is given
76
        $ns = $params['-n'] ?? CLI::getOption('n');
77
78
        if (empty($name))
79
            $name = CLI::prompt(lang('Recharge.configName'), null, 'required|string');
80
81
        if (empty($name)) {
82
            CLI::error(lang('Recharge.badName'));
83
            return;
84
        }
85
86
        //namespace locator
87
        $nsinfo = $file->getNamespaceInfo($ns, 'Config');
88
89
        //class & file name
90
        $name = pascalize($name);
91
92
        //target Dir
93
        if ($nsinfo['default']) {
94
            $targetDir = $nsinfo['path'] . '/';
95
            $ns = $nsinfo['ns'];
96
        } else {
97
            $targetDir = $nsinfo['path'] . '/Config/';
98
            $ns = $nsinfo['ns'] . '\Config';
99
        }
100
101
        $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A")];
102
103
        $filepath = $targetDir . $name . '.php';
104
105
        //check a directory exist
106
        if ($file->checkFileExist($filepath) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
107
            $template = $file->renderTemplate('config', $data);
108
109
            if (!write_file($filepath, $template)) {
110
                CLI::error(lang('Recharge.writeError', [$filepath]));
111
                return;
112
            }
113
114
            CLI::write('Created file: ' . CLI::color($filepath, 'green'));
115
        }
116
    }
117
}
118