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 |
||
| 55 | class ApiController extends Controller { |
||
| 56 | /** @var TagService */ |
||
| 57 | private $tagService; |
||
| 58 | /** @var IManager * */ |
||
| 59 | private $shareManager; |
||
| 60 | /** @var IPreview */ |
||
| 61 | private $previewManager; |
||
| 62 | /** IUserSession */ |
||
| 63 | private $userSession; |
||
| 64 | /** IConfig */ |
||
| 65 | private $config; |
||
| 66 | /** @var Folder */ |
||
| 67 | private $userFolder; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $appName |
||
| 71 | * @param IRequest $request |
||
| 72 | * @param IUserSession $userSession |
||
| 73 | * @param TagService $tagService |
||
| 74 | * @param IPreview $previewManager |
||
| 75 | * @param IManager $shareManager |
||
| 76 | * @param IConfig $config |
||
| 77 | * @param Folder $userFolder |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function __construct($appName, |
|
| 80 | IRequest $request, |
||
| 81 | IUserSession $userSession, |
||
| 82 | TagService $tagService, |
||
| 83 | IPreview $previewManager, |
||
| 84 | IManager $shareManager, |
||
| 85 | IConfig $config, |
||
| 86 | Folder $userFolder) { |
||
| 87 | parent::__construct($appName, $request); |
||
| 88 | $this->userSession = $userSession; |
||
| 89 | $this->tagService = $tagService; |
||
| 90 | $this->previewManager = $previewManager; |
||
| 91 | $this->shareManager = $shareManager; |
||
| 92 | $this->config = $config; |
||
| 93 | $this->userFolder = $userFolder; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Gets a thumbnail of the specified file |
||
| 98 | * |
||
| 99 | * @since API version 1.0 |
||
| 100 | * |
||
| 101 | * @NoAdminRequired |
||
| 102 | * @NoCSRFRequired |
||
| 103 | * @StrictCookieRequired |
||
| 104 | * |
||
| 105 | * @param int $x |
||
| 106 | * @param int $y |
||
| 107 | * @param string $file URL-encoded filename |
||
| 108 | * @return DataResponse|FileDisplayResponse |
||
| 109 | */ |
||
| 110 | public function getThumbnail($x, $y, $file) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Updates the info of the specified file path |
||
| 134 | * The passed tags are absolute, which means they will |
||
| 135 | * replace the actual tag selection. |
||
| 136 | * |
||
| 137 | * @NoAdminRequired |
||
| 138 | * |
||
| 139 | * @param string $path path |
||
| 140 | * @param array|string $tags array of tags |
||
| 141 | * @return DataResponse |
||
| 142 | */ |
||
| 143 | public function updateFileTags($path, $tags = null) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param \OCP\Files\Node[] $nodes |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | private function formatNodes(array $nodes) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns a list of recently modifed files. |
||
| 191 | * |
||
| 192 | * @NoAdminRequired |
||
| 193 | * |
||
| 194 | * @return DataResponse |
||
| 195 | */ |
||
| 196 | public function getRecentFiles() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return a list of share types for outgoing shares |
||
| 204 | * |
||
| 205 | * @param Node $node file node |
||
| 206 | * |
||
| 207 | * @return int[] array of share types |
||
| 208 | */ |
||
| 209 | View Code Duplication | private function getShareTypes(Node $node) { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Change the default sort mode |
||
| 238 | * |
||
| 239 | * @NoAdminRequired |
||
| 240 | * |
||
| 241 | * @param string $mode |
||
| 242 | * @param string $direction |
||
| 243 | * @return Response |
||
| 244 | */ |
||
| 245 | public function updateFileSorting($mode, $direction) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Toggle default for showing/hiding hidden files |
||
| 260 | * |
||
| 261 | * @NoAdminRequired |
||
| 262 | * |
||
| 263 | * @param bool $show |
||
| 264 | */ |
||
| 265 | public function showHiddenFiles($show) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Toggle default for showing/hiding xxx folder |
||
| 272 | * |
||
| 273 | * @NoAdminRequired |
||
| 274 | * |
||
| 275 | * @param bool $show |
||
| 276 | * @param bool $key the key of the folder |
||
| 277 | * |
||
| 278 | * @return Response |
||
| 279 | */ |
||
| 280 | public function toggleShowFolder(int $show, string $key) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get sorting-order for custom sorting |
||
| 297 | * |
||
| 298 | * @NoAdminRequired |
||
| 299 | * |
||
| 300 | * @param String |
||
| 301 | * @return String |
||
| 302 | */ |
||
| 303 | public function getNodeType($folderpath) { |
||
| 307 | |||
| 308 | } |
||
| 309 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.