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 |
||
| 39 | class Extension implements IExtension { |
||
| 40 | const APP_NAME = 'comments'; |
||
| 41 | |||
| 42 | const ADD_COMMENT_SUBJECT = 'add_comment_subject'; |
||
| 43 | const ADD_COMMENT_MESSAGE = 'add_comment_message'; |
||
| 44 | |||
| 45 | /** @var IFactory */ |
||
| 46 | protected $languageFactory; |
||
| 47 | |||
| 48 | /** @var IManager */ |
||
| 49 | protected $activityManager; |
||
| 50 | |||
| 51 | /** @var ICommentsManager */ |
||
| 52 | protected $commentsManager; |
||
| 53 | |||
| 54 | /** @var IURLGenerator */ |
||
| 55 | protected $URLGenerator; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param IFactory $languageFactory |
||
| 59 | * @param IManager $activityManager |
||
| 60 | * @param ICommentsManager $commentsManager |
||
| 61 | * @param IURLGenerator $URLGenerator |
||
| 62 | */ |
||
| 63 | public function __construct(IFactory $languageFactory, IManager $activityManager, ICommentsManager $commentsManager, IURLGenerator $URLGenerator) { |
||
| 64 | $this->languageFactory = $languageFactory; |
||
| 65 | $this->activityManager = $activityManager; |
||
| 66 | $this->commentsManager = $commentsManager; |
||
| 67 | $this->URLGenerator = $URLGenerator; |
||
| 68 | } |
||
| 69 | |||
| 70 | protected function getL10N($languageCode = null) { |
||
| 71 | return $this->languageFactory->get(self::APP_NAME, $languageCode); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The extension can return an array of additional notification types. |
||
| 76 | * If no additional types are to be added false is to be returned |
||
| 77 | * |
||
| 78 | * @param string $languageCode |
||
| 79 | * @return array|false |
||
| 80 | */ |
||
| 81 | public function getNotificationTypes($languageCode) { |
||
| 82 | $l = $this->getL10N($languageCode); |
||
| 83 | |||
| 84 | return array( |
||
| 85 | self::APP_NAME => [ |
||
| 86 | 'desc' => (string) $l->t('<strong>Comments</strong> for files'), |
||
| 87 | 'methods' => [self::METHOD_MAIL, self::METHOD_STREAM], |
||
| 88 | ], |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * For a given method additional types to be displayed in the settings can be returned. |
||
| 94 | * In case no additional types are to be added false is to be returned. |
||
| 95 | * |
||
| 96 | * @param string $method |
||
| 97 | * @return array|false |
||
| 98 | */ |
||
| 99 | public function getDefaultTypes($method) { |
||
| 100 | return $method === self::METHOD_STREAM ? [self::APP_NAME] : false; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * A string naming the css class for the icon to be used can be returned. |
||
| 105 | * If no icon is known for the given type false is to be returned. |
||
| 106 | * |
||
| 107 | * @param string $type |
||
| 108 | * @return string|false |
||
| 109 | */ |
||
| 110 | public function getTypeIcon($type) { |
||
| 111 | switch ($type) { |
||
| 112 | case self::APP_NAME: |
||
| 113 | return 'icon-comment'; |
||
| 114 | } |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The extension can translate a given message to the requested languages. |
||
| 121 | * If no translation is available false is to be returned. |
||
| 122 | * |
||
| 123 | * @param string $app |
||
| 124 | * @param string $text |
||
| 125 | * @param array $params |
||
| 126 | * @param boolean $stripPath |
||
| 127 | * @param boolean $highlightParams |
||
| 128 | * @param string $languageCode |
||
| 129 | * @return string|false |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { |
|
|
|
|||
| 132 | if ($app !== self::APP_NAME) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | $l = $this->getL10N($languageCode); |
||
| 137 | |||
| 138 | if ($this->activityManager->isFormattingFilteredObject()) { |
||
| 139 | $translation = $this->translateShort($text, $l, $params); |
||
| 140 | if ($translation !== false) { |
||
| 141 | return $translation; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $this->translateLong($text, $l, $params); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $text |
||
| 150 | * @param IL10N $l |
||
| 151 | * @param array $params |
||
| 152 | * @return bool|string |
||
| 153 | */ |
||
| 154 | protected function translateShort($text, IL10N $l, array $params) { |
||
| 155 | |||
| 156 | switch ($text) { |
||
| 157 | case self::ADD_COMMENT_SUBJECT: |
||
| 158 | if ($this->authorIsCurrentUser($params[0])) { |
||
| 159 | return (string) $l->t('You commented'); |
||
| 160 | } |
||
| 161 | return (string) $l->t('%1$s commented', $params); |
||
| 162 | case self::ADD_COMMENT_MESSAGE: |
||
| 163 | return $this->convertParameterToComment($params[0]); |
||
| 164 | } |
||
| 165 | |||
| 166 | return false; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $text |
||
| 171 | * @param IL10N $l |
||
| 172 | * @param array $params |
||
| 173 | * @return bool|string |
||
| 174 | */ |
||
| 175 | protected function translateLong($text, IL10N $l, array $params) { |
||
| 176 | |||
| 177 | switch ($text) { |
||
| 178 | case self::ADD_COMMENT_SUBJECT: |
||
| 179 | if ($this->authorIsCurrentUser($params[0])) { |
||
| 180 | return (string) $l->t('You commented on %2$s', $params); |
||
| 181 | } |
||
| 182 | return (string) $l->t('%1$s commented on %2$s', $params); |
||
| 183 | case self::ADD_COMMENT_MESSAGE: |
||
| 184 | return $this->convertParameterToComment($params[0]); |
||
| 185 | } |
||
| 186 | |||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Check if the author is the current user |
||
| 192 | * |
||
| 193 | * @param string $user Parameter e.g. `<user display-name="admin">admin</user>` |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | protected function authorIsCurrentUser($user) { |
||
| 197 | try { |
||
| 198 | return strip_tags($user) === $this->activityManager->getCurrentUserId(); |
||
| 199 | } catch (\UnexpectedValueException $e) { |
||
| 200 | return false; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * The extension can define the type of parameters for translation |
||
| 206 | * |
||
| 207 | * Currently known types are: |
||
| 208 | * * file => will strip away the path of the file and add a tooltip with it |
||
| 209 | * * username => will add the avatar of the user |
||
| 210 | * |
||
| 211 | * @param string $app |
||
| 212 | * @param string $text |
||
| 213 | * @return array|false |
||
| 214 | */ |
||
| 215 | public function getSpecialParameterList($app, $text) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * The extension can define the parameter grouping by returning the index as integer. |
||
| 231 | * In case no grouping is required false is to be returned. |
||
| 232 | * |
||
| 233 | * @param array $activity |
||
| 234 | * @return integer|false |
||
| 235 | */ |
||
| 236 | public function getGroupParameter($activity) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * The extension can define additional navigation entries. The array returned has to contain two keys 'top' |
||
| 242 | * and 'apps' which hold arrays with the relevant entries. |
||
| 243 | * If no further entries are to be added false is no be returned. |
||
| 244 | * |
||
| 245 | * @return array|false |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function getNavigation() { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * The extension can check if a custom filter (given by a query string like filter=abc) is valid or not. |
||
| 264 | * |
||
| 265 | * @param string $filterValue |
||
| 266 | * @return boolean |
||
| 267 | */ |
||
| 268 | public function isFilterValid($filterValue) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * The extension can filter the types based on the filter if required. |
||
| 274 | * In case no filter is to be applied false is to be returned unchanged. |
||
| 275 | * |
||
| 276 | * @param array $types |
||
| 277 | * @param string $filter |
||
| 278 | * @return array|false |
||
| 279 | */ |
||
| 280 | public function filterNotificationTypes($types, $filter) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * For a given filter the extension can specify the sql query conditions including parameters for that query. |
||
| 289 | * In case the extension does not know the filter false is to be returned. |
||
| 290 | * The query condition and the parameters are to be returned as array with two elements. |
||
| 291 | * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%')); |
||
| 292 | * |
||
| 293 | * @param string $filter |
||
| 294 | * @return array|false |
||
| 295 | */ |
||
| 296 | public function getQueryForFilter($filter) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $parameter |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | protected function convertParameterToComment($parameter) { |
||
| 305 | if (preg_match('/^\<parameter\>(\d*)\<\/parameter\>$/', $parameter, $matches)) { |
||
| 306 | try { |
||
| 307 | $comment = $this->commentsManager->get((int) $matches[1]); |
||
| 308 | $message = $comment->getMessage(); |
||
| 337 | |||
| 338 | protected function regexSafeUser($uid, $displayName) { |
||
| 342 | } |
||
| 343 |
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.