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 | class VKUsers extends VKAPI { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * API Method for this class |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $apiMethod = 'users.'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default Fields For Selection |
||
| 22 | */ |
||
| 23 | const standardFields = [ 'sex', 'online', 'country', 'city', 'bdate' ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * VKUsers constructor. |
||
| 27 | * @param VKBase $vkObject |
||
| 28 | */ |
||
| 29 | public function __construct(VKBase $vkObject) { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Returns detailed information on users |
||
| 36 | * @param string[] $usersIDs |
||
| 37 | * @param array $requestFields |
||
| 38 | * @param string $nameCase |
||
| 39 | * @return mixed |
||
| 40 | * @throws VKException |
||
| 41 | */ |
||
| 42 | public function get($usersIDs, $requestFields = self::standardFields, $nameCase = 'nom') { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns a list of users matching the search criteria |
||
| 61 | * @param string $searchQuery |
||
| 62 | * @param int $isOnline |
||
| 63 | * @param array $requestFields |
||
| 64 | * @param int $sortBy |
||
| 65 | * @param int $displayCount |
||
| 66 | * @return array |
||
| 67 | * @throws VKException |
||
| 68 | */ |
||
| 69 | public function search($searchQuery, $isOnline = 1, $requestFields = self::standardFields, $sortBy = 0, $displayCount = 5) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns information whether a user installed the application |
||
| 91 | * @param int $userID |
||
| 92 | * @return mixed |
||
| 93 | * @throws VKException |
||
| 94 | */ |
||
| 95 | public function isAppUser($userID) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns a list of IDs of users and communities followed by the user |
||
| 105 | * @param int $userID |
||
| 106 | * @param int $combineResults |
||
| 107 | * @param array $requestFields |
||
| 108 | * @param int $resultCount |
||
| 109 | * @return mixed |
||
| 110 | * @throws VKException |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function getSubscriptions($userID, $combineResults = 0, $requestFields = self::standardFields, $resultCount = 20) { |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Returns a list of IDs of followers of the user in question, sorted by date added, most recent first |
||
| 125 | * @param int $userID |
||
| 126 | * @param int $setOffset |
||
| 127 | * @param int $displayCount |
||
| 128 | * @param array $requestFields |
||
| 129 | * @param string $nameCase |
||
| 130 | * @return mixed |
||
| 131 | * @throws VKException |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function getFollowers($userID, $setOffset = 0, $displayCount = 100, $requestFields = self::standardFields, $nameCase = 'nom') { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get Nearby Users Based On Current Latitude and Longitude |
||
| 147 | * @param float $currentLatitude |
||
| 148 | * @param float $currentLongitude |
||
| 149 | * @param int $setTimeOut |
||
| 150 | * @param int $setRadius |
||
| 151 | * @param array $requestFields |
||
| 152 | * @param string $nameCase |
||
| 153 | * @return mixed |
||
| 154 | * @throws VKException |
||
| 155 | */ |
||
| 156 | public function getNearby($currentLatitude, $currentLongitude, $setTimeOut = 7200, $setRadius = 1, $requestFields = self::standardFields, $nameCase = 'nom') { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return fields which are allowed to be used |
||
| 171 | * @param $fieldsArray |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | private function returnAllowedFields($fieldsArray) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Return Allowed Name Case |
||
| 187 | * @param string $ncValue |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | private function returnAllowedNC($ncValue) { |
||
| 197 | } |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.