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 |
||
24 | class FilterContainer extends ParameterBag |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function add(array $parameters = []) |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function set($key, $value) |
||
45 | |||
46 | /** |
||
47 | * Filters accepted. |
||
48 | * |
||
49 | * @param RelationInterface $relation |
||
50 | * |
||
51 | * @return FilterInterface[] |
||
52 | */ |
||
53 | public function getFiltersByRelation(RelationInterface $relation) |
||
57 | |||
58 | /** |
||
59 | * Builds search request according to given filters. |
||
60 | * |
||
61 | * @param Request $request |
||
62 | * |
||
63 | * @return SearchRequest |
||
64 | */ |
||
65 | View Code Duplication | public function buildSearchRequest(Request $request) |
|
79 | |||
80 | /** |
||
81 | * Builds elastic search query by given SearchRequest and filters. |
||
82 | * |
||
83 | * @param SearchRequest $request |
||
84 | * @param FilterInterface[]|null $filters |
||
85 | * |
||
86 | * @return Search |
||
87 | */ |
||
88 | View Code Duplication | public function buildSearch(SearchRequest $request, $filters = null) |
|
101 | } |
||
102 |
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.