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 |
||
| 31 | class Api |
||
| 32 | { |
||
| 33 | const DEFAULT_LIMIT = 30; |
||
| 34 | |||
| 35 | 5 | static public function get() { |
|
| 36 | 5 | $app = new AppInfo\Application(); |
|
| 37 | /** @var Data $data */ |
||
| 38 | 5 | $data = $app->getContainer()->query('ActivityData'); |
|
| 39 | |||
| 40 | 5 | $start = isset($_GET['start']) ? (int) $_GET['start'] : 0; |
|
| 41 | 5 | $count = isset($_GET['count']) ? (int) $_GET['count'] : self::DEFAULT_LIMIT; |
|
| 42 | 5 | $user = $app->getContainer()->getServer()->getUserSession()->getUser()->getUID(); |
|
| 43 | |||
| 44 | 5 | if ($start !== 0) { |
|
| 45 | 2 | $start = self::getSinceFromOffset($user, $start); |
|
| 46 | } |
||
| 47 | |||
| 48 | 5 | $activities = $data->get( |
|
| 49 | 5 | $app->getContainer()->query('GroupHelper'), |
|
| 50 | 5 | $app->getContainer()->query('UserSettings'), |
|
| 51 | 5 | $user, $start, $count, 'desc', 'all' |
|
| 52 | ); |
||
| 53 | |||
| 54 | 5 | $entries = array(); |
|
| 55 | 5 | foreach($activities['data'] as $entry) { |
|
| 56 | 4 | $entries[] = array( |
|
| 57 | 4 | 'id' => $entry['activity_id'], |
|
| 58 | 4 | 'subject' => self::parseMessage($entry['subject_prepared']), |
|
| 59 | 4 | 'message' => self::parseMessage($entry['message_prepared']), |
|
| 60 | 4 | 'file' => $entry['object_name'], |
|
| 61 | 4 | 'link' => $entry['link'], |
|
| 62 | 4 | 'date' => date('c', $entry['timestamp']), |
|
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | 5 | return new \OC_OCS_Result($entries); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $user |
||
| 71 | * @param int $offset |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | 2 | protected static function getSinceFromOffset($user, $offset) { |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Parse the parameters in the subject and message |
||
| 96 | * |
||
| 97 | * @param string $message |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | 4 | protected static function parseMessage($message) { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Parse collections |
||
| 108 | * |
||
| 109 | * @param string $message |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 4 | protected static function parseCollections($message) { |
|
| 113 | return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) { |
||
| 114 | $parameterList = explode('><', $match[1]); |
||
| 115 | $parameterListLength = sizeof($parameterList); |
||
| 116 | |||
| 117 | $parameters = []; |
||
| 118 | View Code Duplication | for ($i = 0; $i < $parameterListLength; $i++) { |
|
|
|
|||
| 119 | $parameter = $parameterList[$i]; |
||
| 120 | if ($i > 0) { |
||
| 121 | $parameter = '<' . $parameter; |
||
| 122 | } |
||
| 123 | if ($i + 1 < $parameterListLength) { |
||
| 124 | $parameter = $parameter . '>'; |
||
| 125 | } |
||
| 126 | |||
| 127 | $parameters[] = self::parseParameters($parameter); |
||
| 128 | } |
||
| 129 | if ($parameterListLength === 1) { |
||
| 130 | return array_pop($parameters); |
||
| 131 | } else { |
||
| 132 | $l = \OC::$server->getL10NFactory()->get('activity'); |
||
| 133 | $lastParameter = array_pop($parameters); |
||
| 134 | return $l->t('%s and %s', [ |
||
| 135 | implode($l->t(', '), $parameters), |
||
| 136 | $lastParameter, |
||
| 137 | ]); |
||
| 138 | } |
||
| 139 | 4 | }, $message); |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Parse the parameters in the subject and message |
||
| 144 | * |
||
| 145 | * @param string $message |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 4 | protected static function parseParameters($message) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Display the parameter value |
||
| 158 | * |
||
| 159 | * @param string $message |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | 4 | protected static function parseUntypedParameters($message) { |
|
| 163 | return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) { |
||
| 164 | return $match[1]; |
||
| 165 | 4 | }, $message); |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Display the users display name |
||
| 170 | * |
||
| 171 | * @param string $message |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | 4 | protected static function parseUserParameters($message) { |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Display the full cloud id |
||
| 182 | * |
||
| 183 | * @param string $message |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | 4 | protected static function parseFederatedCloudIDParameters($message) { |
|
| 187 | return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) { |
||
| 188 | return $match[1]; |
||
| 189 | 4 | }, $message); |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Display the path for files |
||
| 194 | * |
||
| 195 | * @param string $message |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | protected static function parseFileParameters($message) { |
||
| 203 | } |
||
| 204 |
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.