Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php declare(strict_types=1); |
||
17 | class Avtr |
||
18 | { |
||
19 | /** |
||
20 | * URL For the API. |
||
21 | */ |
||
22 | const URL = 'https://avtr.io/avtr.'; |
||
23 | |||
24 | /** |
||
25 | * Config parameters. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $config = [ |
||
30 | 'initials' => null, |
||
31 | 'first_name' => null, |
||
32 | 'last_name' => null, |
||
33 | 'email' => null, |
||
34 | 'letter_count' => null, |
||
35 | 'background' => null, |
||
36 | 'size' => null, |
||
37 | 'rounded_corners' => null, |
||
38 | 'shape' => null, |
||
39 | 'theme' => null, |
||
40 | 'text_case' => null, |
||
41 | 'text_color' => null, |
||
42 | 'font_weight' => null, |
||
43 | 'font' => null, |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Default Format that should be returned. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $format = 'png'; |
||
52 | |||
53 | /** |
||
54 | * Avtr constructor. |
||
55 | * |
||
56 | * @param string $value |
||
57 | */ |
||
58 | 20 | public function __construct(string $value = '') |
|
69 | |||
70 | /** |
||
71 | * Returns the URL. |
||
72 | * |
||
73 | * @return String |
||
74 | */ |
||
75 | 19 | public function toUrl(): String |
|
81 | |||
82 | /** |
||
83 | * Sets the format that should be returned. |
||
84 | * |
||
85 | * @param string $format |
||
86 | * |
||
87 | * @return Avtr |
||
88 | * @throws InvalidFormatException |
||
89 | */ |
||
90 | 3 | public function format(string $format): Avtr |
|
100 | |||
101 | /** |
||
102 | * Sets the first name. |
||
103 | * |
||
104 | * @param string $firstName |
||
105 | * |
||
106 | * @return Avtr |
||
107 | */ |
||
108 | 1 | public function firstName(string $firstName): Avtr |
|
114 | |||
115 | /** |
||
116 | * Sets the last name. |
||
117 | * |
||
118 | * @param string $lastName |
||
119 | * |
||
120 | * @return Avtr |
||
121 | */ |
||
122 | 1 | public function lastName(string $lastName): Avtr |
|
128 | |||
129 | /** |
||
130 | * Sets the letter count. |
||
131 | * |
||
132 | * @param int $count |
||
133 | * |
||
134 | * @return Avtr |
||
135 | */ |
||
136 | 1 | View Code Duplication | public function letterCount(int $count): Avtr |
145 | |||
146 | /** |
||
147 | * Sets the background colour. |
||
148 | * |
||
149 | * @param int $r |
||
150 | * @param int $g |
||
151 | * @param int $b |
||
152 | * @param float $a |
||
153 | * |
||
154 | * @return Avtr |
||
155 | */ |
||
156 | 2 | public function background(int $r, int $g, int $b, float $a = 1): Avtr |
|
162 | |||
163 | /** |
||
164 | * Sets the image size. |
||
165 | * |
||
166 | * @param int $size |
||
167 | * |
||
168 | * @return Avtr |
||
169 | */ |
||
170 | 2 | View Code Duplication | public function size(int $size): Avtr |
180 | |||
181 | /** |
||
182 | * Sets whether or not the square image should have rounded corners. |
||
183 | * |
||
184 | * @param bool $roundedCorners |
||
185 | * |
||
186 | * @return Avtr |
||
187 | */ |
||
188 | 2 | public function roundedCorners(bool $roundedCorners): Avtr |
|
194 | |||
195 | /** |
||
196 | * Sets the image shape. |
||
197 | * |
||
198 | * @param string $shape |
||
199 | * |
||
200 | * @return Avtr |
||
201 | * @throws InvalidShapeException |
||
202 | */ |
||
203 | 2 | public function shape(string $shape): Avtr |
|
213 | |||
214 | /** |
||
215 | * Sets the theme. |
||
216 | * |
||
217 | * @param string $theme |
||
218 | * |
||
219 | * @return Avtr |
||
220 | * @throws InvalidThemeException |
||
221 | */ |
||
222 | 2 | public function theme(string $theme): Avtr |
|
232 | |||
233 | /** |
||
234 | * Sets the text case. |
||
235 | * |
||
236 | * @param string $textCase |
||
237 | * |
||
238 | * @return Avtr |
||
239 | * @throws InvalidTextCaseException |
||
240 | */ |
||
241 | 2 | public function textCase(string $textCase): Avtr |
|
251 | |||
252 | /** |
||
253 | * Sets the text colour. |
||
254 | * |
||
255 | * @param int $r |
||
256 | * @param int $g |
||
257 | * @param int $b |
||
258 | * @param float $a |
||
259 | * |
||
260 | * @return Avtr |
||
261 | */ |
||
262 | 2 | public function color(int $r, int $g, int $b, float $a = 1): Avtr |
|
268 | |||
269 | /** |
||
270 | * Sets the font weight. |
||
271 | * |
||
272 | * @param int $fontWeight |
||
273 | * |
||
274 | * @return Avtr |
||
275 | */ |
||
276 | 2 | public function fontWeight(int $fontWeight): Avtr |
|
283 | |||
284 | /** |
||
285 | * Sets the font. |
||
286 | * |
||
287 | * @param string $font |
||
288 | * |
||
289 | * @return Avtr |
||
290 | * @throws InvalidFontException |
||
291 | */ |
||
292 | 2 | public function font(string $font): Avtr |
|
304 | |||
305 | /** |
||
306 | * Validates and sets the appropriate config value for colours. |
||
307 | * |
||
308 | * @param string $where |
||
309 | * @param int $r |
||
310 | * @param int $g |
||
311 | * @param int $b |
||
312 | * @param float $a |
||
313 | */ |
||
314 | 3 | private function setColour(string $where, int $r, int $g, int $b, float $a = 1) |
|
323 | } |
||
324 |