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 |
||
| 40 | class SearchService { |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $userId; |
||
| 44 | |||
| 45 | /** @var FilesService */ |
||
| 46 | private $filesService; |
||
| 47 | |||
| 48 | /** @var ConfigService */ |
||
| 49 | private $configService; |
||
| 50 | |||
| 51 | /** @var MiscService */ |
||
| 52 | private $miscService; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * SearchService constructor. |
||
| 57 | * |
||
| 58 | * @param string $userId |
||
| 59 | * @param FilesService $filesService |
||
| 60 | * @param ConfigService $configService |
||
| 61 | * @param MiscService $miscService |
||
| 62 | * |
||
| 63 | * @internal param IProviderFactory $factory |
||
| 64 | */ |
||
| 65 | public function __construct( |
||
| 66 | $userId, FilesService $filesService, ConfigService $configService, MiscService $miscService |
||
| 67 | ) { |
||
| 68 | $this->userId = $userId; |
||
| 69 | $this->filesService = $filesService; |
||
| 70 | $this->configService = $configService; |
||
| 71 | $this->miscService = $miscService; |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @param SearchRequest $request |
||
| 77 | */ |
||
| 78 | public function improveSearchRequest(SearchRequest $request) { |
||
| 79 | $this->searchQueryShareNames($request); |
||
| 80 | $this->searchQueryWithinDir($request); |
||
| 81 | $this->searchQueryInOptions($request); |
||
| 82 | $this->searchQueryFiltersExtension($request); |
||
| 83 | $this->searchQueryFiltersSource($request); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @param SearchRequest $request |
||
| 89 | */ |
||
| 90 | private function searchQueryShareNames(SearchRequest $request) { |
||
| 91 | $username = MiscService::secureUsername($request->getAuthor()); |
||
| 92 | $request->addField('share_names.' . $username); |
||
| 93 | |||
| 94 | $request->addWildcardField('title'); |
||
| 95 | $request->addWildcardField('share_names.' . $username); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param SearchRequest $request |
||
| 101 | */ |
||
| 102 | private function searchQueryWithinDir(SearchRequest $request) { |
||
| 103 | |||
| 104 | $currentDir = $request->getOption('files_within_dir'); |
||
| 105 | if ($currentDir === '') { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | |||
| 109 | $username = MiscService::secureUsername($request->getAuthor()); |
||
| 110 | $currentDir = MiscService::noBeginSlash(MiscService::endSlash($currentDir)); |
||
| 111 | $request->addRegexFilters( |
||
| 112 | [ |
||
| 113 | ['share_names.' . $username => $currentDir . '*'], |
||
| 114 | ['title' => $currentDir . '*'] |
||
| 115 | ] |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param SearchRequest $request |
||
| 122 | */ |
||
| 123 | private function searchQueryFiltersExtension(SearchRequest $request) { |
||
| 124 | $extension = $request->getOption('files_extension'); |
||
| 125 | if ($extension === '') { |
||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | $username = MiscService::secureUsername($request->getAuthor()); |
||
| 130 | $request->addRegexFilters( |
||
| 131 | [ |
||
| 132 | ['share_names.' . $username => '.*\.' . $extension], |
||
| 133 | ['title' => '.*\.' . $extension] |
||
| 134 | ] |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @param SearchRequest $request |
||
| 141 | */ |
||
| 142 | private function searchQueryFiltersSource(SearchRequest $request) { |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * @param SearchRequest $request |
||
| 161 | */ |
||
| 162 | private function searchQueryInOptions(SearchRequest $request) { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * @param SearchRequest $request |
||
| 183 | * @param string $tag |
||
| 184 | * @param mixed $cond |
||
| 185 | */ |
||
| 186 | private function addTagToSearchRequest(SearchRequest $request, $tag, $cond) { |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @param SearchResult $searchResult |
||
| 195 | */ |
||
| 196 | public function improveSearchResult(SearchResult $searchResult) { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param FilesDocument $document |
||
| 218 | * |
||
| 219 | * @throws Exception |
||
| 220 | */ |
||
| 221 | private function setDocumentInfo(FilesDocument $document) { |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @param FilesDocument $document |
||
| 234 | * @param Node $file |
||
| 235 | */ |
||
| 236 | private function setDocumentInfoFromFile(FilesDocument $document, Node $file) { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * @param FilesDocument $document |
||
| 261 | */ |
||
| 262 | private function setDocumentTitle(FilesDocument $document) { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @param FilesDocument $document |
||
| 273 | */ |
||
| 274 | private function setDocumentLink(FilesDocument $document) { |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @param FilesDocument $document |
||
| 287 | * @param $dir |
||
| 288 | * @param $filename |
||
| 289 | */ |
||
| 290 | View Code Duplication | private function setDocumentLinkFile(FilesDocument $document, $dir, $filename) { |
|
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * @param FilesDocument $document |
||
| 310 | * @param $dir |
||
| 311 | */ |
||
| 312 | View Code Duplication | private function setDocumentLinkDir(FilesDocument $document, $dir) { |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param int $fileId |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | private function getWebdavId($fileId) { |
||
| 336 | |||
| 337 | |||
| 338 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.