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 |
||
| 7 | class PinnerHelper extends RequestHelper |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Checks Pinterest API pinners response, and parses data |
||
| 11 | * with bookmarks info from it. |
||
| 12 | * |
||
| 13 | * @param array $res |
||
| 14 | * @return array |
||
| 15 | */ |
||
| 16 | public static function checkUserDataResponse($res) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates Pinterest API request to get user info according to |
||
| 32 | * username, API url and bookmarks for pagination |
||
| 33 | * |
||
| 34 | * @param string $username |
||
| 35 | * @param string $sourceUrl |
||
| 36 | * @param array $bookmarks |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public static function createUserDataRequest($username, $sourceUrl, $bookmarks) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Parses Pinterest API response with pinner name |
||
| 53 | * |
||
| 54 | * @param $res |
||
| 55 | * @return null |
||
| 56 | */ |
||
| 57 | public static function parseAccountNameResponse($res) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates Pinterest API request to login |
||
| 68 | * |
||
| 69 | * @param string $username |
||
| 70 | * @param string $password |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public static function createLoginRequest($username, $password) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Parses Pintrest Api response after login |
||
| 87 | * |
||
| 88 | * @param array $res |
||
| 89 | * @return bool |
||
| 90 | * @throws AuthException |
||
| 91 | */ |
||
| 92 | public static function parseLoginResponse($res) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Creates common Pinner request data by username |
||
| 103 | * |
||
| 104 | * @param string $username |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | protected static function createPinnerRequestData($username) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Parse bookmarks from response |
||
| 119 | * @param array $response |
||
| 120 | * @return string|null |
||
| 121 | */ |
||
| 122 | protected static function _getBookmarksFromResponse($response) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Parse data from response |
||
| 132 | * @param $response |
||
| 133 | * @return array|null |
||
| 134 | */ |
||
| 135 | View Code Duplication | protected static function _getDataFromResponse($response) |
|
| 142 | } |
||
| 143 |
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.