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 |
||
54 | class UnifiedSearchProvider implements IProvider { |
||
55 | |||
56 | |||
57 | const PROVIDER_ID = 'fulltextsearch'; |
||
58 | const ORDER = 1; |
||
59 | |||
60 | |||
61 | use TArrayTools; |
||
62 | |||
63 | |||
64 | /** @var IL10N */ |
||
65 | private $l10n; |
||
66 | |||
67 | /** @var IURLGenerator */ |
||
68 | private $urlGenerator; |
||
69 | |||
70 | /** @var SearchService */ |
||
71 | private $searchService; |
||
72 | |||
73 | /** @var ConfigService */ |
||
74 | private $configService; |
||
75 | |||
76 | /** @var MiscService */ |
||
77 | private $miscService; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * UnifiedSearchProvider constructor. |
||
82 | * |
||
83 | * @param IL10N $l10n |
||
84 | * @param IURLGenerator $urlGenerator |
||
85 | * @param SearchService $searchService |
||
86 | * @param ConfigService $configService |
||
87 | * @param MiscService $miscService |
||
88 | */ |
||
89 | View Code Duplication | public function __construct( |
|
99 | |||
100 | |||
101 | /** |
||
102 | * return unique id of the provider |
||
103 | */ |
||
104 | public function getId(): string { |
||
107 | |||
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getName(): string { |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @param string $route |
||
119 | * @param array $routeParameters |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | public function getOrder(string $route, array $routeParameters): int { |
||
126 | |||
127 | |||
128 | /** |
||
129 | * @param IUser $user |
||
130 | * @param ISearchQuery $query |
||
131 | * |
||
132 | * @return SearchResult |
||
133 | */ |
||
134 | public function search(IUser $user, ISearchQuery $query): SearchResult { |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @param $query |
||
152 | * |
||
153 | * @return ISearchRequest |
||
154 | */ |
||
155 | private function generateSearchRequest(ISearchQuery $query): ISearchRequest { |
||
172 | |||
173 | |||
174 | /** |
||
175 | * @param ISearchResult[] $searchResult |
||
176 | * |
||
177 | * @return UnifiedSearchResult[] |
||
178 | */ |
||
179 | private function convertSearchResult(array $searchResult): array { |
||
205 | |||
206 | } |
||
207 | |||
208 |
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.