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 |
||
12 | class PaginateElasticaQuerySubscriber implements EventSubscriberInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var Request |
||
16 | */ |
||
17 | private $request; |
||
18 | |||
19 | /** |
||
20 | * @param RequestStack|Request $requestStack |
||
21 | */ |
||
22 | 9 | public function setRequest($requestStack) |
|
30 | |||
31 | /** |
||
32 | * @param ItemsEvent $event |
||
33 | */ |
||
34 | 9 | public function items(ItemsEvent $event) |
|
53 | |||
54 | /** |
||
55 | * Adds knp paging sort to query. |
||
56 | * |
||
57 | * @param ItemsEvent $event |
||
58 | */ |
||
59 | 9 | protected function setSorting(ItemsEvent $event) |
|
74 | |||
75 | 8 | protected function getSort($sortField, array $options = []) |
|
76 | { |
||
77 | $sort = [ |
||
78 | 8 | 'order' => $this->getSortDirection($sortField, $options), |
|
79 | ]; |
||
80 | |||
81 | 7 | View Code Duplication | if (isset($options['sortNestedPath'])) { |
|
|||
82 | 4 | $path = is_callable($options['sortNestedPath']) ? |
|
83 | 4 | $options['sortNestedPath']($sortField) : $options['sortNestedPath']; |
|
84 | |||
85 | 4 | if (!empty($path)) { |
|
86 | 4 | $sort['nested_path'] = $path; |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 7 | View Code Duplication | if (isset($options['sortNestedFilter'])) { |
91 | 2 | $filter = is_callable($options['sortNestedFilter']) ? |
|
92 | 2 | $options['sortNestedFilter']($sortField) : $options['sortNestedFilter']; |
|
93 | |||
94 | 2 | if (!empty($filter)) { |
|
95 | 2 | $sort['nested_filter'] = $filter; |
|
96 | } |
||
97 | } |
||
98 | |||
99 | 7 | return $sort; |
|
100 | } |
||
101 | |||
102 | 8 | protected function getSortDirection($sortField, array $options = []) |
|
103 | { |
||
104 | 8 | $dir = 'asc'; |
|
105 | 8 | $sortDirection = $this->request->get($options['sortDirectionParameterName']); |
|
106 | |||
107 | 8 | if (empty($sortDirection) && isset($options['defaultSortDirection'])) { |
|
108 | $sortDirection = $options['defaultSortDirection']; |
||
109 | } |
||
110 | |||
111 | 8 | if ('desc' === strtolower($sortDirection)) { |
|
112 | 1 | $dir = 'desc'; |
|
113 | } |
||
114 | |||
115 | // check if the requested sort field is in the sort whitelist |
||
116 | 8 | if (isset($options['sortFieldWhitelist']) && !in_array($sortField, $options['sortFieldWhitelist'])) { |
|
117 | 1 | throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist', $sortField)); |
|
118 | } |
||
119 | |||
120 | 7 | return $dir; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return array |
||
125 | */ |
||
126 | 4 | public static function getSubscribedEvents() |
|
132 | } |
||
133 |
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.