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 |
||
| 10 | class Laposta |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $apiKey; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var Client |
||
| 19 | */ |
||
| 20 | private $client; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Laposta constructor. |
||
| 24 | * @param null|string $apiKey |
||
| 25 | * @param null|Client $client |
||
| 26 | * @throws \Exception |
||
| 27 | */ |
||
| 28 | public function __construct($apiKey = null, $client = null) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return List_[] |
||
| 49 | */ |
||
| 50 | public function getLists() : array |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param $listId |
||
| 65 | * @return List_ |
||
| 66 | */ |
||
| 67 | public function getList($listId) : List_ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param List_ $list |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function createList(List_ $list) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param List_ $list |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function updateList(List_ $list) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param List_ $list |
||
| 102 | */ |
||
| 103 | public function deleteList(List_ $list) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param List_|string $list |
||
| 112 | * @param string $state |
||
| 113 | * @return Member[] |
||
| 114 | */ |
||
| 115 | public function getMembers($list, $state = Member::STATE_ACTIVE) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $uri |
||
| 139 | * @param array $options |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | private function get($uri, $options = []) : array |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $uri |
||
| 149 | * @param array $options |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | private function post($uri, $options = []) : array |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $uri |
||
| 159 | * @param array $options |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | private function delete($uri, $options = []) : array |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $method |
||
| 169 | * @param string $uri |
||
| 170 | * @param array $options |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | private function request($method, $uri, $options = []) : array |
||
| 181 | } |
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.