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 |
||
| 61 | class FilesProvider implements IFullTextSearchProvider { |
||
| 62 | |||
| 63 | |||
| 64 | const FILES_PROVIDER_ID = 'files'; |
||
| 65 | |||
| 66 | |||
| 67 | /** @var IL10N */ |
||
| 68 | private $l10n; |
||
| 69 | |||
| 70 | /** @var ConfigService */ |
||
| 71 | private $configService; |
||
| 72 | |||
| 73 | /** @var FilesService */ |
||
| 74 | private $filesService; |
||
| 75 | |||
| 76 | /** @var SearchService */ |
||
| 77 | private $searchService; |
||
| 78 | |||
| 79 | /** @var MiscService */ |
||
| 80 | private $miscService; |
||
| 81 | |||
| 82 | /** @var IRunner */ |
||
| 83 | private $runner; |
||
| 84 | |||
| 85 | /** @var IIndexOptions */ |
||
| 86 | private $indexOptions = []; |
||
| 87 | |||
| 88 | |||
| 89 | View Code Duplication | public function __construct( |
|
| 99 | |||
| 100 | /** |
||
| 101 | * return unique id of the provider |
||
| 102 | */ |
||
| 103 | public function getId(): string { |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * return name of the provider |
||
| 110 | */ |
||
| 111 | public function getName(): string { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function getConfiguration(): array { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @param IRunner $runner |
||
| 126 | */ |
||
| 127 | public function setRunner(IRunner $runner) { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param IIndexOptions $options |
||
| 134 | */ |
||
| 135 | public function setIndexOptions(IIndexOptions $options) { |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * @return SearchTemplate |
||
| 142 | */ |
||
| 143 | public function getSearchTemplate(): SearchTemplate { |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * |
||
| 214 | */ |
||
| 215 | public function loadProvider() { |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $userId |
||
| 221 | * |
||
| 222 | * @return IndexDocument[] |
||
| 223 | * @throws InvalidPathException |
||
| 224 | * @throws NotFoundException |
||
| 225 | */ |
||
| 226 | public function generateIndexableDocuments(string $userId): array { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param IndexDocument $document |
||
| 236 | */ |
||
| 237 | public function fillIndexDocument(IndexDocument $document) { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param IndexDocument $document |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function isDocumentUpToDate(IndexDocument $document): bool { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @param IIndex $index |
||
| 256 | * |
||
| 257 | * @return IndexDocument |
||
| 258 | * @throws InvalidPathException |
||
| 259 | * @throws NotFoundException |
||
| 260 | * @throws NotPermittedException |
||
| 261 | * @throws FileIsNotIndexableException |
||
| 262 | */ |
||
| 263 | public function updateDocument(IIndex $index): IndexDocument { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @param IFullTextSearchPlatform $platform |
||
| 273 | */ |
||
| 274 | public function onInitializingIndex(IFullTextSearchPlatform $platform) { |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * @param IFullTextSearchPlatform $platform |
||
| 280 | */ |
||
| 281 | public function onResettingIndex(IFullTextSearchPlatform $platform) { |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * not used yet |
||
| 287 | */ |
||
| 288 | public function unloadProvider() { |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * before a search, improve the request |
||
| 294 | * |
||
| 295 | * @param ISearchRequest $request |
||
| 296 | */ |
||
| 297 | public function improveSearchRequest(ISearchRequest $request) { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * after a search, improve results |
||
| 304 | * |
||
| 305 | * @param ISearchResult $searchResult |
||
| 306 | */ |
||
| 307 | public function improveSearchResult(ISearchResult $searchResult) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $info |
||
| 314 | * @param string $value |
||
| 315 | */ |
||
| 316 | private function updateRunnerInfo(string $info, string $value) { |
||
| 323 | |||
| 324 | |||
| 325 | } |
||
| 326 |
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.