Passed
Push — main ( 6f590f...2e3c65 )
by Nils
03:37
created

Command::getExplanation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Command;
4
5
use Startwind\Forrest\Command\Parameters\Parameter;
6
use Startwind\Forrest\Command\Parameters\PasswordParameter;
7
8
class Command implements \JsonSerializable
9
{
10
    private bool $isRunnable = true;
11
12
    private string $fullyQualifiedIdentifier = '';
13
14
    private string $outputFormat = '';
15
16
    private bool $allowedInHistory = true;
17
18
    private string $explanation = "";
19
20
    private float $score = -1;
21
22
    public function setOutputFormat(string $output): void
23
    {
24
        $this->outputFormat = $output;
25
    }
26
27
    /**
28
     * @var Parameter[]
29
     */
30
    private array $parameters = [];
31
32
    private array $plainCommandArray = [];
33
34
    public function __construct(private string $name, private readonly string $description, private readonly string $prompt, string $explanation = "")
35
    {
36
        $this->explanation = $explanation;
37
    }
38
39
    /**
40
     * @return float
41
     */
42
    public function getScore(): float
43
    {
44
        return $this->score;
45
    }
46
47
    /**
48
     * @param float $score
49
     */
50
    public function setScore(float $score): void
51
    {
52
        $this->score = $score;
53
    }
54
55
    /**
56
     * @param array $plainCommandArray
57
     */
58
    public function setPlainCommandArray(array $plainCommandArray): void
59
    {
60
        $this->plainCommandArray = $plainCommandArray;
61
    }
62
63
    /**
64
     * Return true if the prompt can be run via Forrest.
65
     */
66
    public function isRunnable(): bool
67
    {
68
        return $this->isRunnable;
69
    }
70
71
    /**
72
     * @param string $name
73
     */
74
    public function setName(string $name): void
75
    {
76
        $this->name = $name;
77
    }
78
79
    /**
80
     * If this method is called Forrest will not run the command but only show it.
81
     */
82
    public function flagAsNotRunnable(): void
83
    {
84
        $this->isRunnable = false;
85
    }
86
87
    /**
88
     * Return the prompt.
89
     */
90
    public function getPrompt(): string
91
    {
92
        return $this->prompt;
93
    }
94
95
    /**
96
     * Return the name of the command.
97
     */
98
    public function getName(): string
99
    {
100
        return $this->name;
101
    }
102
103
    /**
104
     * Return the description of the command.
105
     */
106
    public function getDescription(): string
107
    {
108
        return $this->description;
109
    }
110
111
    /**
112
     * Set all parameter definitions
113
     */
114
    public function setParameters(array $parameters): void
115
    {
116
        foreach ($parameters as $identifier => $parameter) {
117
            $this->setParameter($identifier, $parameter);
118
        }
119
    }
120
121
    /**
122
     * Set a single parameter definition
123
     */
124
    private function setParameter(string $identifier, Parameter $parameter): void
125
    {
126
        $this->parameters[$identifier] = $parameter;
127
    }
128
129
    /**
130
     * Return the parameters that have to be inserted.
131
     */
132
    public function getParameters(): array
133
    {
134
        return $this->parameters;
135
    }
136
137
    public function isParameterMissing(array $values): bool
138
    {
139
        foreach (array_keys($this->getParameters()) as $identifier) {
140
            if (!array_key_exists($identifier, $values)) {
141
                return true;
142
            }
143
        }
144
        return false;
145
    }
146
147
    /**
148
     * Return the checksum of the command. This is used to check if a command
149
     * was changed.
150
     */
151
    public function getChecksum(): string
152
    {
153
        return md5($this->getPrompt());
154
    }
155
156
    public function getFullyQualifiedIdentifier(): string
157
    {
158
        return $this->fullyQualifiedIdentifier;
159
    }
160
161
    public function setFullyQualifiedIdentifier(string $fullyQualifiedIdentifier): void
162
    {
163
        $this->fullyQualifiedIdentifier = $fullyQualifiedIdentifier;
164
    }
165
166
    public function formatOutput(string $output): string
167
    {
168
        if (!$this->outputFormat) {
169
            return $output;
170
        }
171
172
        return (sprintf($this->outputFormat, trim($output)));
173
    }
174
175
    public function isAllowedInHistory(): bool
176
    {
177
        if (!$this->allowedInHistory) {
178
            return false;
179
        }
180
181
        foreach ($this->parameters as $parameter) {
182
            if ($parameter instanceof PasswordParameter) {
183
                return false;
184
            }
185
        }
186
187
        return true;
188
    }
189
190
    public function setAllowedInHistory(bool $allowedInHistory): void
191
    {
192
        $this->allowedInHistory = $allowedInHistory;
193
    }
194
195
    public function getExplanation(): string
196
    {
197
        return $this->explanation;
198
    }
199
200
    public function jsonSerialize(): array
201
    {
202
        if ($this->plainCommandArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->plainCommandArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
203
            return $this->plainCommandArray;
204
        } else {
205
            $command = [
206
                'name' => $this->getName(),
207
                'description' => $this->getDescription(),
208
                'prompt' => $this->getPrompt()
209
            ];
210
            foreach ($this->getParameters() as $identifier => $parameter) {
211
                $command['parameters'][$identifier] = $parameter->jsonSerialize();
212
            }
213
            return $command;
214
        }
215
    }
216
}
217