Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractCommand
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
15 / 15
19
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 dispatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setParams
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setHelp
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setScriptName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHelp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getScriptName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasHelp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasScriptName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Console\Command;
16
17use Pop\Application;
18use Pop\Dispatch;
19use Pop\Console\Console;
20
21/**
22 * Console abstract command class
23 *
24 * @category   Pop
25 * @package    Pop\Console
26 * @author     Nick Sagona, III <nick@popphp.org>
27 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
28 * @license    https://www.popphp.org/license     New BSD License
29 * @version    5.0.0
30 */
31abstract class AbstractCommand extends Dispatch\AbstractDispatcher implements Dispatch\DispatchableInterface, CommandInterface
32{
33
34    /**
35     * Traits
36     */
37    use Dispatch\ConsoleTrait {
38        Dispatch\ConsoleTrait::__construct as private traitConstruct;
39    }
40
41    /**
42     * Command name
43     * @var ?string
44     */
45    protected ?string $name = null;
46
47    /**
48     * Command params
49     * @var ?string
50     */
51    protected ?string $params = null;
52
53    /**
54     * Command help
55     * @var ?string
56     */
57    protected ?string $help = null;
58
59    /**
60     * Script name the command was registered under (e.g. './app'), used to
61     * recover the command's bare, unprefixed name for subcommand matching
62     * @var ?string
63     */
64    protected ?string $scriptName = null;
65
66    /**
67     * Instantiate the command object
68     *
69     * @param ?Application $application
70     * @param Console      $console
71     * @param ?string      $name
72     * @param ?string      $params
73     * @param ?string      $help
74     */
75    public function __construct(
76        ?Application $application = null, Console $console = new Console(120),
77        ?string $name = null, ?string $params = null, ?string $help = null
78    )
79    {
80        $this->traitConstruct($application, $console);
81
82        if ($name !== null) {
83            $this->setName($name);
84        }
85        if ($params !== null) {
86            $this->setParams($params);
87        }
88        if ($help !== null) {
89            $this->setHelp($help);
90        }
91    }
92
93    /**
94     * Dispatch the command
95     *
96     * Resolves to the 'handle' action when none is given, always passing it through
97     * as an explicit action name rather than relying on AbstractDispatcher's own
98     * default-action fallback, which does not forward $params.
99     *
100     * @param  ?string $action
101     * @param  ?array  $params
102     * @return void
103     */
104    public function dispatch(?string $action = null, ?array $params = null): void
105    {
106        parent::dispatch($action ?? 'handle', $params);
107    }
108
109    /**
110     * Set the command name
111     *
112     * @param  string $name
113     * @return static
114     */
115    public function setName(string $name): static
116    {
117        $this->name = $name;
118        return $this;
119    }
120
121    /**
122     * Set the command params
123     *
124     * @param  string $params
125     * @return static
126     */
127    public function setParams(string $params): static
128    {
129        $this->params = $params;
130        return $this;
131    }
132
133    /**
134     * Set the command help
135     *
136     * @param  string $help
137     * @return static
138     */
139    public function setHelp(string $help): static
140    {
141        $this->help = $help;
142        return $this;
143    }
144
145    /**
146     * Set the script name the command was registered under
147     *
148     * @param  string $scriptName
149     * @return static
150     */
151    public function setScriptName(string $scriptName): static
152    {
153        $this->scriptName = $scriptName;
154        return $this;
155    }
156
157    /**
158     * Get the command name
159     *
160     * @return ?string
161     */
162    public function getName(): ?string
163    {
164        return $this->name;
165    }
166
167    /**
168     * Get the command params
169     *
170     * @return ?string
171     */
172    public function getParams(): ?string
173    {
174        return $this->params;
175    }
176
177    /**
178     * Get the command help
179     *
180     * @return ?string
181     */
182    public function getHelp(): ?string
183    {
184        return $this->help;
185    }
186
187    /**
188     * Get the script name the command was registered under
189     *
190     * @return ?string
191     */
192    public function getScriptName(): ?string
193    {
194        return $this->scriptName;
195    }
196
197    /**
198     * Determine if the command has name
199     *
200     * @return bool
201     */
202    public function hasName(): bool
203    {
204        return ($this->name !== null);
205    }
206
207    /**
208     * Determine if the command has params
209     *
210     * @return bool
211     */
212    public function hasParams(): bool
213    {
214        return ($this->params !== null);
215    }
216
217    /**
218     * Determine if the command has help
219     *
220     * @return bool
221     */
222    public function hasHelp(): bool
223    {
224        return ($this->help !== null);
225    }
226
227    /**
228     * Determine if the command has a script name
229     *
230     * @return bool
231     */
232    public function hasScriptName(): bool
233    {
234        return ($this->scriptName !== null);
235    }
236
237    /**
238     * Return the command name as string
239     *
240     * @return string
241     */
242    public function __toString(): string
243    {
244        return $this->name . (($this->params !== null) ? ' ' . $this->params : null);
245    }
246
247}