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 Circle extends BaseCircle implements \JsonSerializable { |
||
| 32 | |||
| 33 | public function getTypeString() { |
||
| 34 | View Code Duplication | switch ($this->getType()) { |
|
|
|
|||
| 35 | case self::CIRCLES_PERSONAL: |
||
| 36 | return 'Personal'; |
||
| 37 | case self::CIRCLES_HIDDEN: |
||
| 38 | return 'Hidden'; |
||
| 39 | case self::CIRCLES_PRIVATE: |
||
| 40 | return 'Private'; |
||
| 41 | case self::CIRCLES_PUBLIC: |
||
| 42 | return 'Public'; |
||
| 43 | case self::CIRCLES_ALL: |
||
| 44 | return 'All'; |
||
| 45 | } |
||
| 46 | |||
| 47 | return 'none'; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function getTypeLongString() { |
||
| 51 | return self::typeLongString($this->getType()); |
||
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | public function getInfo() { |
||
| 56 | return $this->getTypeLongString(); |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | public function jsonSerialize() { |
||
| 61 | return array( |
||
| 62 | 'id' => $this->getId(), |
||
| 63 | 'name' => $this->getName(), |
||
| 64 | 'owner' => $this->getOwner(), |
||
| 65 | 'user' => $this->getUser(), |
||
| 66 | 'description' => $this->getDescription(), |
||
| 67 | 'type' => $this->getTypeString(), |
||
| 68 | 'creation' => $this->getCreation(), |
||
| 69 | 'typeString' => $this->getTypeString(), |
||
| 70 | 'typeLongString' => $this->getTypeLongString(), |
||
| 71 | 'unique_id' => $this->getUniqueId(), |
||
| 72 | 'members' => $this->getMembers(), |
||
| 73 | 'links' => $this->getRemote() |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * set all infos from an Array. |
||
| 79 | * |
||
| 80 | * @param $arr |
||
| 81 | * |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | public function fromArray($arr) { |
||
| 85 | $this->setId($arr['id']); |
||
| 86 | $this->setName($arr['name']); |
||
| 87 | $this->setUniqueId($arr['unique_id']); |
||
| 88 | $this->setDescription($arr['description']); |
||
| 89 | $this->setType($arr['type']); |
||
| 90 | $this->setCreation($arr['creation']); |
||
| 91 | $this->setOwnerMemberFromArray($arr); |
||
| 92 | $this->setUserMemberFromArray($arr); |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | public static function fromJSON($l10n, $json) { |
||
| 99 | $circle = new Circle($l10n); |
||
| 100 | $circle->fromArray(json_decode($json, true)); |
||
| 101 | |||
| 102 | return $circle; |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * set Owner Infos from Array |
||
| 108 | * |
||
| 109 | * @param $array |
||
| 110 | */ |
||
| 111 | private function setOwnerMemberFromArray(& $array) { |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * set User Infos from Array |
||
| 122 | * |
||
| 123 | * @param $array |
||
| 124 | */ |
||
| 125 | private function setUserMemberFromArray(& $array) { |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @throws CircleTypeNotValid |
||
| 141 | */ |
||
| 142 | public function cantBePersonal() { |
||
| 143 | if ($this->getType() === self::CIRCLES_PERSONAL) { |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @param $type |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public static function typeLongString($type) { |
||
| 172 | |||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 177 |
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.