Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractCommand
95.24% covered (success)
95.24%
20 / 21
91.67% covered (success)
91.67%
11 / 12
16
0.00% covered (danger)
0.00%
0 / 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
 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
 hasName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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
 __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 <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
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 <dev@noladev.com>
27 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
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     * Instantiate the command object
61     *
62     * @param ?Application $application
63     * @param Console      $console
64     * @param ?string      $name
65     * @param ?string      $params
66     * @param ?string      $help
67     */
68    public function __construct(
69        ?Application $application = null, Console $console = new Console(120),
70        ?string $name = null, ?string $params = null, ?string $help = null
71    )
72    {
73        $this->traitConstruct($application, $console);
74
75        if ($name !== null) {
76            $this->setName($name);
77        }
78        if ($params !== null) {
79            $this->setParams($params);
80        }
81        if ($help !== null) {
82            $this->setHelp($help);
83        }
84    }
85
86    /**
87     * Dispatch the command
88     *
89     * Resolves to the 'handle' action when none is given, always passing it through
90     * as an explicit action name rather than relying on AbstractDispatcher's own
91     * default-action fallback, which does not forward $params.
92     *
93     * @param  ?string $action
94     * @param  ?array  $params
95     * @return void
96     */
97    public function dispatch(?string $action = null, ?array $params = null): void
98    {
99        parent::dispatch($action ?? 'handle', $params);
100    }
101
102    /**
103     * Set the command name
104     *
105     * @param  string $name
106     * @return static
107     */
108    public function setName(string $name): static
109    {
110        $this->name = $name;
111        return $this;
112    }
113
114    /**
115     * Set the command params
116     *
117     * @param  string $params
118     * @return static
119     */
120    public function setParams(string $params): static
121    {
122        $this->params = $params;
123        return $this;
124    }
125
126    /**
127     * Set the command help
128     *
129     * @param  string $help
130     * @return static
131     */
132    public function setHelp(string $help): static
133    {
134        $this->help = $help;
135        return $this;
136    }
137
138    /**
139     * Get the command name
140     *
141     * @return ?string
142     */
143    public function getName(): ?string
144    {
145        return $this->name;
146    }
147
148    /**
149     * Get the command params
150     *
151     * @return ?string
152     */
153    public function getParams(): ?string
154    {
155        return $this->params;
156    }
157
158    /**
159     * Get the command help
160     *
161     * @return ?string
162     */
163    public function getHelp(): ?string
164    {
165        return $this->help;
166    }
167
168    /**
169     * Determine if the command has name
170     *
171     * @return bool
172     */
173    public function hasName(): bool
174    {
175        return ($this->name !== null);
176    }
177
178    /**
179     * Determine if the command has params
180     *
181     * @return bool
182     */
183    public function hasParams(): bool
184    {
185        return ($this->params !== null);
186    }
187
188    /**
189     * Determine if the command has help
190     *
191     * @return bool
192     */
193    public function hasHelp(): bool
194    {
195        return ($this->help !== null);
196    }
197
198    /**
199     * Return the command name as string
200     *
201     * @return string
202     */
203    public function __toString(): string
204    {
205        return $this->name . (($this->params !== null) ? ' ' . $this->params : null);
206    }
207
208}