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() { |
||
| 42 | View Code Duplication | switch ($this->getType()) { |
|
|
|
|||
| 43 | case self::CIRCLES_PERSONAL: |
||
| 44 | return 'Personal'; |
||
| 45 | case self::CIRCLES_SECRET: |
||
| 46 | return 'Secret'; |
||
| 47 | case self::CIRCLES_CLOSED: |
||
| 48 | return 'Closed'; |
||
| 49 | case self::CIRCLES_PUBLIC: |
||
| 50 | return 'Public'; |
||
| 51 | case self::CIRCLES_ALL: |
||
| 52 | return 'All'; |
||
| 53 | } |
||
| 54 | |||
| 55 | return 'none'; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getTypeLongString() { |
||
| 61 | |||
| 62 | |||
| 63 | public function getInfo() { |
||
| 66 | |||
| 67 | |||
| 68 | public function jsonSerialize() { |
||
| 98 | |||
| 99 | |||
| 100 | public function getArray($full = false, $light = false) { |
||
| 105 | |||
| 106 | |||
| 107 | public function getJson($full = false, $light = false) { |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * set all infos from an Array. |
||
| 120 | * |
||
| 121 | * @param $arr |
||
| 122 | * |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | public static function fromArray($arr) { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $arr |
||
| 145 | * @param $key |
||
| 146 | * @param int $type |
||
| 147 | * |
||
| 148 | * @return null|Member |
||
| 149 | */ |
||
| 150 | private static function getMemberFromArray($arr, $key, $type = Member::TYPE_USER) { |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @param array $arr |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | private static function getLinksFromArray($arr) { |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * @param array $arr |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | private static function getSettingsFromArray($arr) { |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $json |
||
| 197 | * |
||
| 198 | * @return Circle |
||
| 199 | */ |
||
| 200 | public static function fromJSON($json) { |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * @throws CircleTypeNotValidException |
||
| 207 | */ |
||
| 208 | public function cantBePersonal() { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * @throws FederatedCircleNotAllowedException |
||
| 219 | */ |
||
| 220 | public function hasToBeFederated() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param $type |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public static function typeLongString($type) { |
||
| 249 | |||
| 250 | |||
| 251 | } |
||
| 252 | |||
| 254 |
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.