Conditions | 27 |
Paths | 1536 |
Total Lines | 108 |
Lines | 33 |
Ratio | 30.56 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function findByCriteria(Criteria $criteria, array $extraFields = [], bool $searchByBody = false): PaginatorAdapterInterface |
||
35 | { |
||
36 | $fields = $criteria->getFilters()->getFields(); |
||
37 | $boolFilter = new BoolQuery(); |
||
38 | |||
39 | if (null !== $criteria->getTerm() && '' !== $criteria->getTerm()) { |
||
40 | $searchBy = ['title', 'lead', 'keywords.name']; |
||
41 | |||
42 | foreach ($extraFields as $extraField) { |
||
43 | $searchBy[] = 'extra.'.$extraField; |
||
44 | } |
||
45 | |||
46 | if ($searchByBody) { |
||
47 | $searchBy[] = 'body'; |
||
48 | } |
||
49 | |||
50 | $priority = 1; |
||
51 | foreach (array_reverse($searchBy) as $key => $field) { |
||
52 | $searchBy[$key] = $field.'^'.$priority; |
||
53 | |||
54 | ++$priority; |
||
55 | } |
||
56 | |||
57 | $query = new MultiMatch(); |
||
58 | $query->setQuery($criteria->getTerm()); |
||
59 | $query->setFields($searchBy); |
||
60 | $query->setType(MultiMatch::TYPE_PHRASE); |
||
61 | $boolFilter->addMust($query); |
||
62 | } else { |
||
63 | $boolFilter->addMust(new MatchAll()); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | if (null !== $fields->get('keywords') && !empty($fields->get('keywords'))) { |
|
|
|||
67 | $bool = new BoolQuery(); |
||
68 | $bool->addFilter(new Query\Terms('keywords.name', $fields->get('keywords'))); |
||
69 | $nested = new Nested(); |
||
70 | $nested->setPath('keywords'); |
||
71 | $nested->setQuery($bool); |
||
72 | $boolFilter->addMust($nested); |
||
73 | } |
||
74 | |||
75 | View Code Duplication | if (null !== $fields->get('authors') && !empty($fields->get('authors'))) { |
|
76 | $bool = new BoolQuery(); |
||
77 | foreach ($fields->get('authors') as $author) { |
||
78 | $bool->addFilter(new Query\Match('authors.name', $author)); |
||
79 | } |
||
80 | |||
81 | $nested = new Nested(); |
||
82 | $nested->setPath('authors'); |
||
83 | $nested->setQuery($bool); |
||
84 | $boolFilter->addMust($nested); |
||
85 | } |
||
86 | |||
87 | View Code Duplication | if (null !== $fields->get('sources') && !empty($fields->get('sources'))) { |
|
88 | $nested = new Nested(); |
||
89 | $nested->setPath('sources'); |
||
90 | $boolQuery = new BoolQuery(); |
||
91 | $boolQuery->addMust(new Query\Terms('sources.name', $fields->get('sources'))); |
||
92 | $nested->setQuery($boolQuery); |
||
93 | $boolFilter->addMust($nested); |
||
94 | } |
||
95 | |||
96 | View Code Duplication | if (null !== $fields->get('statuses') && !empty($fields->get('statuses'))) { |
|
97 | $boolFilter->addFilter(new Query\Terms('status', $fields->get('statuses'))); |
||
98 | } |
||
99 | |||
100 | if (null !== $fields->get('metadata') && !empty($fields->get('metadata'))) { |
||
101 | foreach ($fields->get('metadata') as $key => $values) { |
||
102 | foreach ((array) $values as $value) { |
||
103 | $boolFilter->addFilter(new Query\Match($key, $value)); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if (null !== $fields->get('tenantCode')) { |
||
109 | $boolFilter->addFilter(new Term(['tenantCode' => $fields->get('tenantCode')])); |
||
110 | } |
||
111 | |||
112 | $bool = new BoolQuery(); |
||
113 | View Code Duplication | if (null !== $fields->get('routes') && !empty($fields->get('routes'))) { |
|
114 | $bool->addFilter(new Query\Terms('route.id', $fields->get('routes'))); |
||
115 | } |
||
116 | |||
117 | if (null !== $fields->get('publishedAfter') || null !== $fields->get('publishedBefore')) { |
||
118 | $boolFilter->addFilter(new Range( |
||
119 | 'publishedAt', |
||
120 | [ |
||
121 | 'gte' => null !== $fields->get('publishedAfter') ? $fields->get('publishedAfter')->format('Y-m-d') : null, |
||
122 | 'lte' => null !== $fields->get('publishedBefore') ? $fields->get('publishedBefore')->format('Y-m-d') : null, |
||
123 | ] |
||
124 | )); |
||
125 | |||
126 | $boolFilter->addFilter(new \Elastica\Query\Term(['isPublishable' => true])); |
||
127 | } |
||
128 | |||
129 | if (!empty($bool->getParams())) { |
||
130 | $boolFilter->addMust($bool); |
||
131 | } |
||
132 | |||
133 | $query = Query::create($boolFilter) |
||
134 | ->addSort([ |
||
135 | $criteria->getOrder()->getField() => $criteria->getOrder()->getDirection(), |
||
136 | ]); |
||
137 | |||
138 | $query->setSize(SearchResultLoader::MAX_RESULTS); |
||
139 | |||
140 | return $this->createPaginatorAdapter($query); |
||
141 | } |
||
142 | |||
174 |
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.