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 BaseMember implements \JsonSerializable { |
||
32 | |||
33 | const LEVEL_NONE = 0; |
||
34 | const LEVEL_MEMBER = 1; |
||
35 | const LEVEL_MODERATOR = 4; |
||
36 | const LEVEL_ADMIN = 8; |
||
37 | const LEVEL_OWNER = 9; |
||
38 | |||
39 | const STATUS_NONMEMBER = 'Unknown'; |
||
40 | const STATUS_INVITED = 'Invited'; |
||
41 | const STATUS_REQUEST = 'Requesting'; |
||
42 | const STATUS_MEMBER = 'Member'; |
||
43 | const STATUS_BLOCKED = 'Blocked'; |
||
44 | const STATUS_KICKED = 'Kicked'; |
||
45 | |||
46 | /** @var int */ |
||
47 | private $circleId; |
||
48 | |||
49 | |||
50 | /** @var L10N */ |
||
51 | protected $l10n; |
||
52 | |||
53 | /** @var string */ |
||
54 | private $userId; |
||
55 | |||
56 | /** @var int */ |
||
57 | private $level; |
||
58 | |||
59 | /** @var string */ |
||
60 | private $status; |
||
61 | |||
62 | /** @var string */ |
||
63 | private $note; |
||
64 | |||
65 | /** @var string */ |
||
66 | private $joined; |
||
67 | |||
68 | public function __construct($l10n, $userId = '', $circleId = -1) { |
||
80 | |||
81 | |||
82 | public function setCircleId($circleId) { |
||
87 | |||
88 | public function getCircleId() { |
||
91 | |||
92 | |||
93 | public function setUserId($userId) { |
||
98 | |||
99 | public function getUserId() { |
||
102 | |||
103 | |||
104 | public function setLevel($level) { |
||
109 | |||
110 | public function getLevel() { |
||
113 | |||
114 | |||
115 | public function setNote($note) { |
||
120 | |||
121 | public function getNote() { |
||
124 | |||
125 | |||
126 | public function setStatus($status) { |
||
135 | |||
136 | public function getStatus() { |
||
139 | |||
140 | |||
141 | public function setJoined($joined) { |
||
146 | |||
147 | public function getJoined() { |
||
150 | |||
151 | |||
152 | public function isLevel($level) { |
||
155 | |||
156 | |||
157 | public function isAlmostMember() { |
||
161 | |||
162 | |||
163 | protected function setAsAMember($level = 1) { |
||
167 | |||
168 | |||
169 | public static function fromArray2($l10n, $arr) { |
||
189 | |||
190 | |||
191 | public static function fromJSON($l10n, $json) { |
||
194 | |||
195 | View Code Duplication | public function jsonSerialize() { |
|
|
|||
196 | return array( |
||
197 | 'circle_id' => $this->getCircleId(), |
||
198 | 'user_id' => $this->getUserId(), |
||
199 | 'level' => $this->getLevel(), |
||
200 | 'level_string' => $this->getLevelString(), |
||
201 | 'status' => $this->getStatus(), |
||
202 | 'note' => $this->getNote(), |
||
203 | 'joined' => $this->getJoined() |
||
204 | ); |
||
205 | } |
||
206 | |||
207 | public function getLevelString() { |
||
223 | } |
||
224 |
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.