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 |
||
| 32 | class Circle extends BaseCircle implements \JsonSerializable { |
||
| 33 | |||
| 34 | /** @var bool */ |
||
| 35 | private $fullJson = false; |
||
| 36 | |||
| 37 | /** @var bool */ |
||
| 38 | private $lightJson = false; |
||
| 39 | |||
| 40 | |||
| 41 | public function getTypeString() { |
||
| 57 | |||
| 58 | public function getTypeLongString() { |
||
| 61 | |||
| 62 | |||
| 63 | public function getInfo() { |
||
| 66 | |||
| 67 | |||
| 68 | public function jsonSerialize() { |
||
| 93 | |||
| 94 | |||
| 95 | public function getJson($full = false, $light = false) { |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | // /** |
||
| 108 | // * set all infos from an Array. |
||
| 109 | // * |
||
| 110 | // * @param $arr |
||
| 111 | // * |
||
| 112 | // * @return $this |
||
| 113 | // */ |
||
| 114 | // public function fromArray($arr) { |
||
| 115 | // $this->setId($arr['id']); |
||
| 116 | // $this->setName($arr['name']); |
||
| 117 | // $this->setUniqueId($arr['unique_id']); |
||
| 118 | // $this->setDescription($arr['description']); |
||
| 119 | // $this->setType($arr['type']); |
||
| 120 | // $this->setCreation($arr['creation']); |
||
| 121 | //// $this->setOwnerMemberFromArray($arr); |
||
| 122 | //// $this->setUserMemberFromArray($arr); |
||
| 123 | // |
||
| 124 | // return $this; |
||
| 125 | // } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * set all infos from an Array. |
||
| 129 | * |
||
| 130 | * @param $l10n |
||
| 131 | * @param $arr |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public static function fromArray2($l10n, $arr) { |
||
| 160 | |||
| 161 | |||
| 162 | public static function fromJSON($l10n, $json) { |
||
| 165 | |||
| 166 | |||
| 167 | // |
||
| 168 | // |
||
| 169 | // /** |
||
| 170 | // * set User Infos from Array |
||
| 171 | // * |
||
| 172 | // * @param $array |
||
| 173 | // */ |
||
| 174 | // // TODO rewrite the function based of setOwnerMemberFromArray() |
||
| 175 | // private function setUserMemberFromArray($array) { |
||
| 176 | // if (key_exists('status', $array) |
||
| 177 | // && key_exists('level', $array) |
||
| 178 | // && key_exists('joined', $array) |
||
| 179 | // ) { |
||
| 180 | // $user = new Member($this->l10n); |
||
| 181 | // $user->setStatus($array['status']); |
||
| 182 | // $user->setLevel($array['level']); |
||
| 183 | // $user->setJoined($array['joined']); |
||
| 184 | // $this->setUser($user); |
||
| 185 | // } |
||
| 186 | // } |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * @throws CircleTypeNotValid |
||
| 191 | */ |
||
| 192 | public function cantBePersonal() { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @throws FederatedCircleNotAllowedException |
||
| 203 | */ |
||
| 204 | public function hasToBeFederated() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param $type |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public static function typeLongString($type) { |
||
| 233 | |||
| 234 | |||
| 235 | } |
||
| 236 | |||
| 238 |
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.