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