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 |
||
| 35 | class MiscService { |
||
| 36 | |||
| 37 | /** @var ILogger */ |
||
| 38 | private $logger; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | private $appName; |
||
| 42 | |||
| 43 | /** @var IUserManager */ |
||
| 44 | private $userManager; |
||
| 45 | |||
| 46 | public function __construct(ILogger $logger, $appName, IUserManager $userManager) { |
||
| 51 | |||
| 52 | public function log($message, $level = 2) { |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * return the real userId, with its real case |
||
| 64 | * |
||
| 65 | * @param $userId |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | * @throws NoUserException |
||
| 69 | */ |
||
| 70 | public function getRealUserId($userId) { |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $ident |
||
| 82 | * @param int $type |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public static function getDisplay($ident, $type) { |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $display |
||
| 98 | * @param string $ident |
||
| 99 | * @param int $type |
||
| 100 | */ |
||
| 101 | private static function getDisplayMember(&$display, $ident, $type) { |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $display |
||
| 116 | * @param string $ident |
||
| 117 | * @param int $type |
||
| 118 | */ |
||
| 119 | private static function getDisplayContact(&$display, $ident, $type) { |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $ident |
||
| 131 | * |
||
| 132 | * @return mixed|string |
||
| 133 | */ |
||
| 134 | public static function getContactData($ident) { |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $display |
||
| 154 | * @param array $contact |
||
| 155 | */ |
||
| 156 | private static function getDisplayContactFromArray(&$display, $contact) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * return Display Name if user exists and display name exists. |
||
| 172 | * returns Exception if user does not exist. |
||
| 173 | * |
||
| 174 | * However, with noException set to true, will return userId even if user does not exist |
||
| 175 | * |
||
| 176 | * @param $userId |
||
| 177 | * @param bool $noException |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | * @throws NoUserException |
||
| 181 | */ |
||
| 182 | public function getDisplayName($userId, $noException = false) { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 198 | * |
||
| 199 | * @param string $result |
||
| 200 | */ |
||
| 201 | public function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 215 | |||
| 216 | } |
||
| 217 | |||
| 218 |