Completed
Pull Request — master (#3)
by thomas
18:16
created

PinRequest   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 240
ccs 97
cts 97
cp 1
rs 8.8798
c 0
b 0
f 0
wmc 44

43 Methods

Rating   Name   Duplication   Size   Complexity  
A getDesc() 0 3 1
A hasCancelButton() 0 3 1
A withCancelButton() 0 4 1
A hasRepeat() 0 3 1
A withTimeout() 0 4 1
A withError() 0 4 1
A getTitle() 0 3 1
A getError() 0 3 1
A getKeyInfo() 0 3 1
A withQualityBarTooltip() 0 4 1
A withOkButton() 0 4 1
A getQualityBarTooltip() 0 3 1
A hasTitle() 0 3 1
A withQualityBar() 0 4 1
A hasDesc() 0 3 1
A hasOkButton() 0 3 1
A withCommand() 0 3 1
A hasRepeatError() 0 3 1
A withKeyInfo() 0 4 1
A hasError() 0 3 1
A getNotOk() 0 3 1
A hasPrompt() 0 3 1
A getCommand() 0 6 2
A getOkButton() 0 3 1
A withDesc() 0 4 1
A getTimeout() 0 3 1
A withNotOk() 0 4 1
A getCancelButton() 0 3 1
A hasQualityBar() 0 3 1
A hasCommand() 0 3 1
A hasQualityBarTooltip() 0 3 1
A hasKeyInfo() 0 3 1
A hasTimeout() 0 3 1
A getCommands() 0 3 1
A getRepeat() 0 3 1
A getQualityBar() 0 3 1
A withRepeatError() 0 4 1
A withPrompt() 0 4 1
A withTitle() 0 4 1
A getRepeatError() 0 3 1
A getPrompt() 0 3 1
A withRepeat() 0 4 1
A hasNotOk() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like PinRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PinRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\PinEntry;
6
7
class PinRequest
8
{
9
    /**
10
     * keyed by the command, here we store a command => param map
11
     * @var string[]|int[]
12
     */
13
    private $commands = [];
14
15
    /**
16
     * @return string[]|int[]
17
     */
18
    public function getCommands(): array
19 2
    {
20
        return $this->commands;
21 2
    }
22 2
23
    private function withCommand(string $command, $param)
24
    {
25 1
        $this->commands[$command] = $param;
26
    }
27 1
28
    private function hasCommand(string $command): bool
29
    {
30 1
        return array_key_exists($command, $this->commands);
31
    }
32 1
33 1
    private function getCommand(string $command)
34
    {
35 1
        if (!$this->hasCommand($command)) {
36
            return null;
37
        }
38
        return $this->commands[$command];
39
    }
40
41 1
    public function withDesc(string $desc)
42
    {
43 1
        $this->withCommand(Command::SETDESC, $desc);
44
        return $this;
45
    }
46
47
    public function hasDesc(): bool
48
    {
49 1
        return $this->hasCommand(Command::SETDESC);
50
    }
51 1
52
    public function getDesc()
53
    {
54 14
        return $this->getCommand(Command::SETDESC);
55
    }
56 14
57 14
    public function withPrompt(string $desc)
58
    {
59 13
        $this->withCommand(Command::SETPROMPT, $desc);
60
        return $this;
61 13
    }
62
63
    public function hasPrompt(): bool
64 13
    {
65
        return $this->hasCommand(Command::SETPROMPT);
66 13
    }
67 13
68
    public function getPrompt()
69 13
    {
70
        return $this->getCommand(Command::SETPROMPT);
71
    }
72 2
73
    public function withKeyInfo(string $keyInfo)
74 2
    {
75 2
        $this->withCommand(Command::SETKEYINFO, $keyInfo);
76
        return $this;
77
    }
78 1
79
    public function hasKeyInfo(): bool
80 1
    {
81
        return $this->hasCommand(Command::SETKEYINFO);
82
    }
83 1
84
    public function getKeyInfo()
85 1
    {
86
        return $this->getCommand(Command::SETKEYINFO);
87
    }
88 1
89
    public function withRepeat($repeat)
90 1
    {
91 1
        $this->withCommand(Command::SETREPEAT, $repeat);
92
        return $this;
93
    }
94 1
95
    public function hasRepeat(): bool
96 1
    {
97
        return $this->hasCommand(Command::SETREPEAT);
98
    }
99 1
100
    public function getRepeat()
101 1
    {
102
        return $this->getCommand(Command::SETREPEAT);
103
    }
104 1
105
    public function withRepeatError(string $repeatError)
106 1
    {
107 1
        $this->withCommand(Command::SETREPEATERROR, $repeatError);
108
        return $this;
109
    }
110 1
111
    public function hasRepeatError(): bool
112 1
    {
113
        return $this->hasCommand(Command::SETREPEATERROR);
114
    }
115 1
116
    public function getRepeatError()
117 1
    {
118
        return $this->getCommand(Command::SETREPEATERROR);
119
    }
120 1
121
    public function withError(string $error)
122 1
    {
123 1
        $this->withCommand(Command::SETERROR, $error);
124
        return $this;
125
    }
126 1
127
    public function hasError(): bool
128 1
    {
129
        return $this->hasCommand(Command::SETERROR);
130
    }
131 1
132
    public function getError()
133 1
    {
134
        return $this->getCommand(Command::SETERROR);
135
    }
136
137
    public function withOkButton(string $ok)
138
    {
139
        $this->withCommand(Command::SETOK, $ok);
140 1
        return $this;
141
    }
142 1
143 1
    public function hasOkButton(): bool
144
    {
145
        return $this->hasCommand(Command::SETOK);
146 1
    }
147
148 1
    public function getOkButton()
149
    {
150
        return $this->getCommand(Command::SETOK);
151 1
    }
152
153 1
    public function withNotOk(string $ok)
154
    {
155
        $this->withCommand(Command::SETNOTOK, $ok);
156 1
        return $this;
157
    }
158 1
159 1
    public function hasNotOk(): bool
160
    {
161
        return $this->hasCommand(Command::SETNOTOK);
162 1
    }
163
164 1
    public function getNotOk()
165
    {
166
        return $this->getCommand(Command::SETNOTOK);
167 1
    }
168
169 1
    public function withCancelButton(string $cancel)
170
    {
171
        $this->withCommand(Command::SETCANCEL, $cancel);
172 1
        return $this;
173
    }
174 1
175 1
    public function hasCancelButton(): bool
176
    {
177
        return $this->hasCommand(Command::SETCANCEL);
178 1
    }
179
180 1
    public function getCancelButton()
181
    {
182
        return $this->getCommand(Command::SETCANCEL);
183 1
    }
184
185 1
    public function withTitle(string $title)
186
    {
187
        $this->withCommand(Command::SETTITLE, $title);
188 1
        return $this;
189
    }
190 1
191 1
    public function hasTitle(): bool
192
    {
193
        return $this->hasCommand(Command::SETTITLE);
194 1
    }
195
196 1
    public function getTitle()
197
    {
198
        return $this->getCommand(Command::SETTITLE);
199 1
    }
200
201 1
    public function withQualityBar(string $qualityBar)
202
    {
203
        $this->withCommand(Command::SETQUALITYBAR, $qualityBar);
204 1
        return $this;
205
    }
206 1
207 1
    public function hasQualityBar(): bool
208
    {
209
        return $this->hasCommand(Command::SETQUALITYBAR);
210 1
    }
211
212 1
    public function getQualityBar()
213
    {
214
        return $this->getCommand(Command::SETQUALITYBAR);
215 1
    }
216
217 1
    public function withQualityBarTooltip(string $tooltip)
218
    {
219
        $this->withCommand(Command::SETQUALITYBAR_TT, $tooltip);
220 2
        return $this;
221
    }
222 2
223 2
    public function hasQualityBarTooltip(): bool
224
    {
225
        return $this->hasCommand(Command::SETQUALITYBAR_TT);
226 1
    }
227
228 1
    public function getQualityBarTooltip()
229
    {
230
        return $this->getCommand(Command::SETQUALITYBAR_TT);
231 1
    }
232
233 1
    public function withTimeout(int $timeout)
234
    {
235
        $this->withCommand(Command::SETTIMEOUT, $timeout);
236 1
        return $this;
237
    }
238 1
239 1
    public function hasTimeout(): bool
240
    {
241
        return $this->hasCommand(Command::SETTIMEOUT);
242 1
    }
243
244 1
    public function getTimeout()
245
    {
246
        return $this->getCommand(Command::SETTIMEOUT);
247 1
    }
248
}
249