• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 8677009716

13 Apr 2024 11:45PM UTC coverage: 84.44% (-2.2%) from 86.607%
8677009716

push

github

web-flow
Merge pull request #8776 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class-v3

fix: Cannot declare class CodeIgniter\Config\Services, because the name is already in use

0 of 3 new or added lines in 1 file covered. (0.0%)

478 existing lines in 72 files now uncovered.

20318 of 24062 relevant lines covered (84.44%)

188.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/system/Commands/Generators/ModelGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Commands\Generators;
15

16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
18
use CodeIgniter\CLI\GeneratorTrait;
19

20
/**
21
 * Generates a skeleton Model file.
22
 */
23
class ModelGenerator extends BaseCommand
24
{
25
    use GeneratorTrait;
26

27
    /**
28
     * The Command's Group
29
     *
30
     * @var string
31
     */
32
    protected $group = 'Generators';
33

34
    /**
35
     * The Command's Name
36
     *
37
     * @var string
38
     */
39
    protected $name = 'make:model';
40

41
    /**
42
     * The Command's Description
43
     *
44
     * @var string
45
     */
46
    protected $description = 'Generates a new model file.';
47

48
    /**
49
     * The Command's Usage
50
     *
51
     * @var string
52
     */
53
    protected $usage = 'make:model <name> [options]';
54

55
    /**
56
     * The Command's Arguments
57
     *
58
     * @var array<string, string>
59
     */
60
    protected $arguments = [
61
        'name' => 'The model class name.',
62
    ];
63

64
    /**
65
     * The Command's Options
66
     *
67
     * @var array<string, string>
68
     */
69
    protected $options = [
70
        '--table'     => 'Supply a table name. Default: "the lowercased plural of the class name".',
71
        '--dbgroup'   => 'Database group to use. Default: "default".',
72
        '--return'    => 'Return type, Options: [array, object, entity]. Default: "array".',
73
        '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
74
        '--suffix'    => 'Append the component title to the class name (e.g. User => UserModel).',
75
        '--force'     => 'Force overwrite existing file.',
76
    ];
77

78
    /**
79
     * Actually execute a command.
80
     */
81
    public function run(array $params)
82
    {
83
        $this->component = 'Model';
15✔
84
        $this->directory = 'Models';
15✔
85
        $this->template  = 'model.tpl.php';
15✔
86

87
        $this->classNameLang = 'CLI.generator.className.model';
15✔
88
        $this->generateClass($params);
15✔
89
    }
90

91
    /**
92
     * Prepare options and do the necessary replacements.
93
     */
94
    protected function prepare(string $class): string
95
    {
96
        $table   = $this->getOption('table');
14✔
97
        $dbGroup = $this->getOption('dbgroup');
14✔
98
        $return  = $this->getOption('return');
14✔
99

100
        $baseClass = class_basename($class);
14✔
101

102
        if (preg_match('/^(\S+)Model$/i', $baseClass, $match) === 1) {
14✔
103
            $baseClass = $match[1];
3✔
104
        }
105

106
        $table  = is_string($table) ? $table : plural(strtolower($baseClass));
14✔
107
        $return = is_string($return) ? $return : 'array';
14✔
108

109
        if (! in_array($return, ['array', 'object', 'entity'], true)) {
14✔
110
            // @codeCoverageIgnoreStart
UNCOV
111
            $return = CLI::prompt(lang('CLI.generator.returnType'), ['array', 'object', 'entity'], 'required');
×
UNCOV
112
            CLI::newLine();
×
113
            // @codeCoverageIgnoreEnd
114
        }
115

116
        if ($return === 'entity') {
14✔
117
            $return = str_replace('Models', 'Entities', $class);
4✔
118

119
            if (preg_match('/^(\S+)Model$/i', $return, $match) === 1) {
4✔
120
                $return = $match[1];
2✔
121

122
                if ($this->getOption('suffix')) {
2✔
123
                    $return .= 'Entity';
2✔
124
                }
125
            }
126

127
            $return = '\\' . trim($return, '\\') . '::class';
4✔
128
            $this->call('make:entity', array_merge([$baseClass], $this->params));
4✔
129
        } else {
130
            $return = "'{$return}'";
10✔
131
        }
132

133
        return $this->parseTemplate($class, ['{dbGroup}', '{table}', '{return}'], [$dbGroup, $table, $return], compact('dbGroup'));
14✔
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc