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 |
||
15 | View Code Duplication | class PhotoSize extends Entity |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * @var mixed|null |
||
19 | */ |
||
20 | protected $file_id; |
||
21 | |||
22 | /** |
||
23 | * @var mixed|null |
||
24 | */ |
||
25 | protected $width; |
||
26 | |||
27 | /** |
||
28 | * @var mixed|null |
||
29 | */ |
||
30 | protected $height; |
||
31 | |||
32 | /** |
||
33 | * @var mixed|null |
||
34 | */ |
||
35 | protected $file_size; |
||
36 | |||
37 | /** |
||
38 | * PhotoSize constructor. |
||
39 | * |
||
40 | * @param array $data |
||
41 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
42 | */ |
||
43 | 1 | public function __construct(array $data) |
|
44 | { |
||
45 | |||
46 | 1 | $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; |
|
47 | 1 | if (empty($this->file_id)) { |
|
48 | throw new TelegramException('file_id is empty!'); |
||
49 | } |
||
50 | |||
51 | 1 | $this->width = isset($data['width']) ? $data['width'] : null; |
|
52 | 1 | if (empty($this->width)) { |
|
53 | throw new TelegramException('width is empty!'); |
||
54 | } |
||
55 | |||
56 | 1 | $this->height = isset($data['height']) ? $data['height'] : null; |
|
57 | 1 | if (empty($this->height)) { |
|
58 | throw new TelegramException('height is empty!'); |
||
59 | } |
||
60 | |||
61 | 1 | $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; |
|
62 | 1 | } |
|
63 | |||
64 | /** |
||
65 | * Get file id |
||
66 | * |
||
67 | * @return mixed|null |
||
68 | */ |
||
69 | public function getFileId() |
||
73 | |||
74 | /** |
||
75 | * Get width |
||
76 | * |
||
77 | * @return mixed|null |
||
78 | */ |
||
79 | public function getWidth() |
||
83 | |||
84 | /** |
||
85 | * Get height |
||
86 | * |
||
87 | * @return mixed|null |
||
88 | */ |
||
89 | public function getHeight() |
||
93 | |||
94 | /** |
||
95 | * Get file size |
||
96 | * |
||
97 | * @return mixed|null |
||
98 | */ |
||
99 | public function getFileSize() |
||
103 | } |
||
104 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.