• 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

89.47
/system/CLI/BaseCommand.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\CLI;
15

16
use Config\Exceptions;
17
use Psr\Log\LoggerInterface;
18
use ReflectionException;
19
use Throwable;
20

21
/**
22
 * BaseCommand is the base class used in creating CLI commands.
23
 *
24
 * @property array<string, string> $arguments
25
 * @property Commands              $commands
26
 * @property string                $description
27
 * @property string                $group
28
 * @property LoggerInterface       $logger
29
 * @property string                $name
30
 * @property array<string, string> $options
31
 * @property string                $usage
32
 */
33
abstract class BaseCommand
34
{
35
    /**
36
     * The group the command is lumped under
37
     * when listing commands.
38
     *
39
     * @var string
40
     */
41
    protected $group;
42

43
    /**
44
     * The Command's name
45
     *
46
     * @var string
47
     */
48
    protected $name;
49

50
    /**
51
     * the Command's usage description
52
     *
53
     * @var string
54
     */
55
    protected $usage;
56

57
    /**
58
     * the Command's short description
59
     *
60
     * @var string
61
     */
62
    protected $description;
63

64
    /**
65
     * the Command's options description
66
     *
67
     * @var array<string, string>
68
     */
69
    protected $options = [];
70

71
    /**
72
     * the Command's Arguments description
73
     *
74
     * @var array<string, string>
75
     */
76
    protected $arguments = [];
77

78
    /**
79
     * The Logger to use for a command
80
     *
81
     * @var LoggerInterface
82
     */
83
    protected $logger;
84

85
    /**
86
     * Instance of Commands so
87
     * commands can call other commands.
88
     *
89
     * @var Commands
90
     */
91
    protected $commands;
92

93
    public function __construct(LoggerInterface $logger, Commands $commands)
94
    {
95
        $this->logger   = $logger;
158✔
96
        $this->commands = $commands;
158✔
97
    }
98

99
    /**
100
     * Actually execute a command.
101
     *
102
     * @param array<int|string, string|null> $params
103
     *
104
     * @return int|void
105
     */
106
    abstract public function run(array $params);
107

108
    /**
109
     * Can be used by a command to run other commands.
110
     *
111
     * @param array<int|string, string|null> $params
112
     *
113
     * @return int|void
114
     *
115
     * @throws ReflectionException
116
     */
117
    protected function call(string $command, array $params = [])
118
    {
119
        return $this->commands->run($command, $params);
10✔
120
    }
121

122
    /**
123
     * A simple method to display an error with line/file, in child commands.
124
     *
125
     * @return void
126
     */
127
    protected function showError(Throwable $e)
128
    {
129
        $exception = $e;
3✔
130
        $message   = $e->getMessage();
3✔
131
        $config    = config(Exceptions::class);
3✔
132

133
        require $config->errorViewPath . '/cli/error_exception.php';
3✔
134
    }
135

136
    /**
137
     * Show Help includes (Usage, Arguments, Description, Options).
138
     *
139
     * @return void
140
     */
141
    public function showHelp()
142
    {
143
        CLI::write(lang('CLI.helpUsage'), 'yellow');
8✔
144

145
        if (isset($this->usage)) {
8✔
146
            $usage = $this->usage;
7✔
147
        } else {
148
            $usage = $this->name;
1✔
149

150
            if ($this->arguments !== []) {
1✔
151
                $usage .= ' [arguments]';
1✔
152
            }
153
        }
154

155
        CLI::write($this->setPad($usage, 0, 0, 2));
8✔
156

157
        if (isset($this->description)) {
8✔
158
            CLI::newLine();
8✔
159
            CLI::write(lang('CLI.helpDescription'), 'yellow');
8✔
160
            CLI::write($this->setPad($this->description, 0, 0, 2));
8✔
161
        }
162

163
        if ($this->arguments !== []) {
8✔
164
            CLI::newLine();
7✔
165
            CLI::write(lang('CLI.helpArguments'), 'yellow');
7✔
166
            $length = max(array_map('strlen', array_keys($this->arguments)));
7✔
167

168
            foreach ($this->arguments as $argument => $description) {
7✔
169
                CLI::write(CLI::color($this->setPad($argument, $length, 2, 2), 'green') . $description);
7✔
170
            }
171
        }
172

173
        if ($this->options !== []) {
8✔
174
            CLI::newLine();
2✔
175
            CLI::write(lang('CLI.helpOptions'), 'yellow');
2✔
176
            $length = max(array_map('strlen', array_keys($this->options)));
2✔
177

178
            foreach ($this->options as $option => $description) {
2✔
179
                CLI::write(CLI::color($this->setPad($option, $length, 2, 2), 'green') . $description);
2✔
180
            }
181
        }
182
    }
183

184
    /**
185
     * Pads our string out so that all titles are the same length to nicely line up descriptions.
186
     *
187
     * @param int $extra How many extra spaces to add at the end
188
     */
189
    public function setPad(string $item, int $max, int $extra = 2, int $indent = 0): string
190
    {
191
        $max += $extra + $indent;
12✔
192

193
        return str_pad(str_repeat(' ', $indent) . $item, $max);
12✔
194
    }
195

196
    /**
197
     * Get pad for $key => $value array output
198
     *
199
     * @param array<string, string> $array
200
     *
201
     * @deprecated Use setPad() instead.
202
     *
203
     * @codeCoverageIgnore
204
     */
205
    public function getPad(array $array, int $pad): int
206
    {
UNCOV
207
        $max = 0;
×
208

UNCOV
209
        foreach (array_keys($array) as $key) {
×
UNCOV
210
            $max = max($max, strlen($key));
×
211
        }
212

UNCOV
213
        return $max + $pad;
×
214
    }
215

216
    /**
217
     * Makes it simple to access our protected properties.
218
     *
219
     * @return array<string, string>|Commands|LoggerInterface|string|null
220
     */
221
    public function __get(string $key)
222
    {
223
        return $this->{$key} ?? null;
52✔
224
    }
225

226
    /**
227
     * Makes it simple to check our protected properties.
228
     */
229
    public function __isset(string $key): bool
230
    {
231
        return isset($this->{$key});
53✔
232
    }
233
}
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