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 |
||
| 51 | class ApiController extends Controller { |
||
| 52 | /** @var TagService */ |
||
| 53 | private $tagService; |
||
| 54 | /** @var IManager **/ |
||
| 55 | private $shareManager; |
||
| 56 | /** @var IPreview */ |
||
| 57 | private $previewManager; |
||
| 58 | /** IUserSession */ |
||
| 59 | private $userSession; |
||
| 60 | /** IConfig */ |
||
| 61 | private $config; |
||
| 62 | /** @var Folder */ |
||
| 63 | private $userFolder; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $appName |
||
| 67 | * @param IRequest $request |
||
| 68 | * @param IUserSession $userSession |
||
| 69 | * @param TagService $tagService |
||
| 70 | * @param IPreview $previewManager |
||
| 71 | * @param IManager $shareManager |
||
| 72 | * @param IConfig $config |
||
| 73 | * @param Folder $userFolder |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function __construct($appName, |
|
|
|
|||
| 76 | IRequest $request, |
||
| 77 | IUserSession $userSession, |
||
| 78 | TagService $tagService, |
||
| 79 | IPreview $previewManager, |
||
| 80 | IManager $shareManager, |
||
| 81 | IConfig $config, |
||
| 82 | Folder $userFolder) { |
||
| 83 | parent::__construct($appName, $request); |
||
| 84 | $this->userSession = $userSession; |
||
| 85 | $this->tagService = $tagService; |
||
| 86 | $this->previewManager = $previewManager; |
||
| 87 | $this->shareManager = $shareManager; |
||
| 88 | $this->config = $config; |
||
| 89 | $this->userFolder = $userFolder; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets a thumbnail of the specified file |
||
| 94 | * |
||
| 95 | * @since API version 1.0 |
||
| 96 | * |
||
| 97 | * @NoAdminRequired |
||
| 98 | * @NoCSRFRequired |
||
| 99 | * @StrictCookieRequired |
||
| 100 | * |
||
| 101 | * @param int $x |
||
| 102 | * @param int $y |
||
| 103 | * @param string $file URL-encoded filename |
||
| 104 | * @return DataResponse|DataDisplayResponse |
||
| 105 | */ |
||
| 106 | public function getThumbnail($x, $y, $file) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Updates the info of the specified file path |
||
| 121 | * The passed tags are absolute, which means they will |
||
| 122 | * replace the actual tag selection. |
||
| 123 | * |
||
| 124 | * @NoAdminRequired |
||
| 125 | * |
||
| 126 | * @param string $path path |
||
| 127 | * @param array|string $tags array of tags |
||
| 128 | * @return DataResponse |
||
| 129 | */ |
||
| 130 | public function updateFileTags($path, $tags = null) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param \OCP\Files\Node[] $nodes |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | private function formatNodes(array $nodes) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns a list of recently modifed files. |
||
| 178 | * |
||
| 179 | * @NoAdminRequired |
||
| 180 | * |
||
| 181 | * @return DataResponse |
||
| 182 | */ |
||
| 183 | public function getRecentFiles() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return a list of share types for outgoing shares |
||
| 191 | * |
||
| 192 | * @param Node $node file node |
||
| 193 | * |
||
| 194 | * @return int[] array of share types |
||
| 195 | */ |
||
| 196 | View Code Duplication | private function getShareTypes(Node $node) { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Change the default sort mode |
||
| 223 | * |
||
| 224 | * @NoAdminRequired |
||
| 225 | * |
||
| 226 | * @param string $mode |
||
| 227 | * @param string $direction |
||
| 228 | * @return Response |
||
| 229 | */ |
||
| 230 | public function updateFileSorting($mode, $direction) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Toggle default for showing/hiding hidden files |
||
| 245 | * |
||
| 246 | * @NoAdminRequired |
||
| 247 | * |
||
| 248 | * @param bool $show |
||
| 249 | */ |
||
| 250 | public function showHiddenFiles($show) { |
||
| 254 | |||
| 255 | } |
||
| 256 |
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.