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 |
||
| 12 | abstract class Provider |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * List of methods that require logged status. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $loginRequiredFor = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Instance of the API Request. |
||
| 23 | * |
||
| 24 | * @var Request |
||
| 25 | */ |
||
| 26 | protected $request; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Response |
||
| 30 | */ |
||
| 31 | protected $response; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param Request $request |
||
| 35 | * @param Response $response |
||
| 36 | */ |
||
| 37 | public function __construct(Request $request, Response $response) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Executes a POST request to Pinterest API. |
||
| 45 | * |
||
| 46 | * @param array $requestOptions |
||
| 47 | * @param string $resourceUrl |
||
| 48 | * @param bool $returnResponse |
||
| 49 | * |
||
| 50 | * @return Response|bool |
||
| 51 | */ |
||
| 52 | protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Executes a GET request to Pinterest API. |
||
| 64 | * |
||
| 65 | * @param array $requestOptions |
||
| 66 | * @param string $resourceUrl |
||
| 67 | * @return array|bool |
||
| 68 | */ |
||
| 69 | View Code Duplication | protected function execGetRequest(array $requestOptions = [], $resourceUrl = '') |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Executes a GET request to Pinterest API with pagination. |
||
| 80 | * |
||
| 81 | * @param array $requestOptions |
||
| 82 | * @param string $resourceUrl |
||
| 83 | * @param array $bookmarks |
||
| 84 | * @return Response |
||
| 85 | */ |
||
| 86 | View Code Duplication | protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = []) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param $url |
||
| 97 | * @param string $postString |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | protected function execute($url, $postString = "") |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getEntityIdName() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Executes pagination GET request. |
||
| 119 | * |
||
| 120 | * @param array $data |
||
| 121 | * @param string $url |
||
| 122 | * @param array $bookmarks |
||
| 123 | * @return Response |
||
| 124 | */ |
||
| 125 | public function getPaginatedData(array $data, $url, $bookmarks = []) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $method |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function checkMethodRequiresLogin($method) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function isLoggedIn() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Simply makes GET request to some url. |
||
| 150 | * @param string $url |
||
| 151 | * @return array|bool |
||
| 152 | */ |
||
| 153 | public function visitPage($url = '') |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $res |
||
| 160 | * @return Response |
||
| 161 | */ |
||
| 162 | protected function processResult($res) |
||
| 166 | } |
||
| 167 |
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.