PinRequest   B
last analyzed

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