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 |
||
31 | class Chat extends Entity |
||
32 | { |
||
33 | public function __construct($data) |
||
34 | { |
||
35 | parent::__construct($data); |
||
36 | |||
37 | if (!$this->getType()) { |
||
38 | if ($this->getId() > 0) { |
||
39 | $this->type = 'private'; |
||
40 | } elseif ($this->getId() < 0) { |
||
41 | $this->type = 'group'; |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Try mention |
||
48 | * |
||
49 | * @return string|null |
||
50 | */ |
||
51 | public function tryMention() |
||
52 | 19 | { |
|
53 | if ($this->isPrivateChat()) { |
||
54 | 19 | View Code Duplication | if ($this->username === null) { |
|
|||
55 | 19 | if ($this->last_name !== null) { |
|
56 | return $this->first_name . ' ' . $this->last_name; |
||
57 | } |
||
58 | |||
59 | 19 | return $this->first_name; |
|
60 | 15 | } |
|
61 | |||
62 | 10 | return '@' . $this->username; |
|
63 | 9 | } |
|
64 | 1 | ||
65 | 1 | return $this->getTitle(); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Check if this is a group chat |
||
70 | * |
||
71 | 19 | * @return bool |
|
72 | 19 | */ |
|
73 | 19 | public function isGroupChat() |
|
74 | 19 | { |
|
75 | 19 | return $this->type === 'group' || $this->id < 0; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Check if this is a private chat |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isPrivateChat() |
||
87 | |||
88 | /** |
||
89 | * Check if this is a super group |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function isSuperGroup() |
||
97 | |||
98 | /** |
||
99 | * Check if this is a channel |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function isChannel() |
||
107 | } |
||
108 |
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.