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 |
||
| 38 | class MiscService { |
||
| 39 | |||
| 40 | /** @var ILogger */ |
||
| 41 | private $logger; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $appName; |
||
| 45 | |||
| 46 | /** @var IUserManager */ |
||
| 47 | private $userManager; |
||
| 48 | |||
| 49 | public function __construct(ILogger $logger, $appName, IUserManager $userManager) { |
||
| 54 | |||
| 55 | public function log($message, $level = 2) { |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @param $arr |
||
| 67 | * @param $k |
||
| 68 | * |
||
| 69 | * @return string|array |
||
| 70 | */ |
||
| 71 | public static function get($arr, $k) { |
||
| 78 | |||
| 79 | |||
| 80 | public static function mustContains($data, $arr) { |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * @param $data |
||
| 95 | * |
||
| 96 | * @return DataResponse |
||
| 97 | */ |
||
| 98 | public function fail($data) { |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $data |
||
| 110 | * |
||
| 111 | * @return DataResponse |
||
| 112 | */ |
||
| 113 | public function success($data) { |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * return the real userId, with its real case |
||
| 123 | * |
||
| 124 | * @param $userId |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | * @throws NoUserException |
||
| 128 | */ |
||
| 129 | public function getRealUserId($userId) { |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $ident |
||
| 141 | * @param int $type |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public static function getDisplay($ident, $type) { |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $display |
||
| 157 | * @param string $ident |
||
| 158 | * @param int $type |
||
| 159 | */ |
||
| 160 | private static function getDisplayMember(&$display, $ident, $type) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $display |
||
| 175 | * @param string $ident |
||
| 176 | * @param int $type |
||
| 177 | */ |
||
| 178 | private static function getDisplayContact(&$display, $ident, $type) { |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @param $ident |
||
| 190 | * |
||
| 191 | * @deprecated - move this somewhere else, no static if possible |
||
| 192 | * @return mixed|string |
||
| 193 | */ |
||
| 194 | public static function getContactData($ident) { |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $display |
||
| 214 | * @param array $contact |
||
| 215 | */ |
||
| 216 | private static function getDisplayContactFromArray(&$display, $contact) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * return Display Name if user exists and display name exists. |
||
| 232 | * returns Exception if user does not exist. |
||
| 233 | * |
||
| 234 | * However, with noException set to true, will return userId even if user does not exist |
||
| 235 | * |
||
| 236 | * @param $userId |
||
| 237 | * @param bool $noException |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | * @throws NoUserException |
||
| 241 | */ |
||
| 242 | public function getDisplayName($userId, $noException = false) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 258 | * |
||
| 259 | * @param string $result |
||
| 260 | */ |
||
| 261 | public function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 275 | |||
| 276 | } |
||
| 277 | |||
| 278 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.