Issues (39)

src/Commands/MakeModel.php (2 issues)

1
<?php namespace Hafiz\Commands;
2
3
use CodeIgniter\CLI\BaseCommand;
4
use CodeIgniter\CLI\CLI;
5
use Hafiz\Libraries\DBHandler;
6
use Hafiz\Libraries\FileHandler;
7
8
/**
9
 * Make Command Class for a skeleton Model Class
10
 * model Class that collect property from table
11
 *
12
 * @package CodeIgniter\Commands
13
 * @extend BaseCommand
14
 */
15
class MakeModel extends BaseCommand
16
{
17
18
    /**
19
     * The group command is heading under all
20
     * commands will be listed
21
     * @var string
22
     */
23
    protected $group = 'CI4-Recharge';
24
25
    /**
26
     * The Command's name
27
     * @var string
28
     */
29
    protected $name = 'make:model';
30
31
    /**
32
     * The Command's short description
33
     * @var string
34
     */
35
    protected $description = 'Creates a Model file.';
36
37
    /**
38
     * The Command's usage
39
     * @var string
40
     */
41
    protected $usage = 'make:model [model_name] [Options]';
42
43
    /**
44
     * The Command's Arguments
45
     * @var array
46
     */
47
    protected $arguments = [
48
        'model_name' => 'The Model file name',
49
    ];
50
51
    /**
52
     * The Command's Options
53
     * @var array
54
     */
55
    protected $options = [
56
        '-n' => 'Set model namespace',
57
        '-t' => 'Set Model Database table',
58
    ];
59
60
    /**
61
     * Creates a new entity file with the current timestamp.
62
     * @param array $params
63
     * @return void
64
     */
65
    public function run(array $params = [])
66
    {
67
        helper(['inflector', 'filesystem']);
68
        $file = new FileHandler();
69
70
        $name = array_shift($params);
71
        $ns = $params['-n'] ?? CLI::getOption('n');
72
        $table = $params['-t'] ?? CLI::getOption('t');
73
74
        if (empty($name)) {
75
            $name = CLI::prompt(lang('Recharge.modelName'), null, 'required|string');
76
        }
77
78
        if (empty($name)) {
79
            CLI::error(lang('Recharge.badName'));
80
            return;
81
        }
82
83
        //namespace locator
84
        $nsinfo = $file->getNamespaceInfo($ns, 'App');
85
86
        //class & file name
87
        $ns = $nsinfo['ns'];
88
        $targetDir = $nsinfo['path'] . '/Models/';
89
        $name = singular(pascalize($name)) . (stripos($name, 'Model') === FALSE ? 'Model' : '');
90
        $filepath = $targetDir . $name . '.php';
91
92
        if ($file->verifyDirectory($filepath)) {
93
            //do we have to add table info
94
            if (!empty($table)) {
95
                $db = new DBHandler();
96
                if ($db->checkTableExist($table)) {
0 ignored issues
show
It seems like $table can also be of type boolean; however, parameter $table of Hafiz\Libraries\DBHandler::checkTableExist() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
                if ($db->checkTableExist(/** @scrutinizer ignore-type */ $table)) {
Loading history...
97
                    $properties = $db->getModelProperties($table);
98
                    extract($properties);
99
                }
100
            }
101
102
            //for soft deleted option
103
            $softDelete = 'false';
104
            $deleteField = '';
105
            $validationRules = '[]';
106
107
            if (isset($attributes)) {
108
                if (stripos($attributes, 'deleted_at') !== false) {
109
                    $softDelete = 'true';
110
                    $deleteField = "protected \$deletedField  = 'deleted_at';";
111
                }
112
            }
113
            if (isset($rules)) {
114
                $validationRules = $rules;
115
            }
116
117
            $data = [
118
                '{namespace}' => $ns,
119
                '{name}' => $name,
120
                '{created_at}' => date("d F, Y h:i:s A"),
121
                '{attributes}' => $attributes ?? NULL,
122
                '{table}' => $table ?? NULL,
123
                '{primary_id}' => $primary_id ?? NULL,
124
                '{delete_field}' => $deleteField,
125
                '{soft_delete}' => $softDelete,
126
                '{rules}' => $validationRules,
127
            ];
128
129
            //check a directory exist
130
            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...
131
                $template = $file->renderTemplate('model', $data);
132
133
134
                if (!write_file($filepath, $template)) {
135
                    CLI::error(lang('Recharge.writeError', [$filepath]));
136
                    return;
137
                }
138
139
                CLI::write('Created file: ' . CLI::color($filepath, 'green'));
140
            }
141
        }
142
    }
143
}
144