Conditions | 8 |
Paths | 24 |
Total Lines | 155 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
62 | public function buildQuery( |
||
63 | $itemsPerPage, $workspaceFilter, $from = 0, $bookmarkIdentifiers = [], $filters = [], |
||
64 | $excludeFilters = [], $sortField = null, $sortOrder = null, $queryString = null |
||
65 | ) |
||
66 | { |
||
67 | |||
68 | if ($workspaceFilter) { |
||
|
|||
69 | $workspaceFilter = [0 => $workspaceFilter]; |
||
70 | } |
||
71 | |||
72 | // The base filter. |
||
73 | $queryFilter = [ |
||
74 | 'bool' => [ |
||
75 | 'must' => [ |
||
76 | [ |
||
77 | 'bool' => [ |
||
78 | 'should' => $workspaceFilter |
||
79 | ] |
||
80 | ] |
||
81 | ] |
||
82 | ] |
||
83 | ]; |
||
84 | |||
85 | if (!($excludeFilters && array_key_exists('bookmarks', $excludeFilters))) { |
||
86 | // Add user document bookmarks. |
||
87 | |||
88 | if ($bookmarkIdentifiers && is_array($bookmarkIdentifiers)) { |
||
89 | $queryFilter['bool']['must'][0]['bool']['should'][] = [ |
||
90 | 'terms' => [ |
||
91 | '_id' => array_values(array_filter($bookmarkIdentifiers)) |
||
92 | ] |
||
93 | ]; |
||
94 | } |
||
95 | } else { |
||
96 | // Show only user document bookmarks. |
||
97 | $queryFilter['bool']['must'][0] = [ |
||
98 | 'terms' => [ |
||
99 | '_id' => $bookmarkIdentifiers |
||
100 | ] |
||
101 | ]; |
||
102 | } |
||
103 | |||
104 | $filterPart = $this->buildFilterQueryPart($filters, $excludeFilters); |
||
105 | |||
106 | if ($filterPart) { |
||
107 | $queryFilter['bool']['must'][] = $filterPart; |
||
108 | } |
||
109 | |||
110 | if (!is_null($queryString)) { |
||
111 | $query = [ |
||
112 | 'bool' => [ |
||
113 | 'must' => [ |
||
114 | 'query_string' => [ |
||
115 | 'query' => $queryString |
||
116 | ] |
||
117 | ], |
||
118 | 'filter' => $queryFilter |
||
119 | ] |
||
120 | ]; |
||
121 | } else { |
||
122 | $query = [ |
||
123 | 'bool' => [ |
||
124 | 'must' => [ |
||
125 | 'match_all' => (object)[] |
||
126 | ], |
||
127 | 'filter' => $queryFilter |
||
128 | ] |
||
129 | ]; |
||
130 | |||
131 | } |
||
132 | |||
133 | |||
134 | // Put together the complete query. |
||
135 | $fullQuery = [ |
||
136 | 'body' => [ |
||
137 | 'size' => $itemsPerPage, |
||
138 | 'from' => $from, |
||
139 | 'query' => $query, |
||
140 | 'sort' => $this->buildSortQueryPart($sortField, $sortOrder), |
||
141 | 'aggs' => [ |
||
142 | 'aliasState' => [ |
||
143 | 'terms' => [ |
||
144 | 'field' => 'aliasState' |
||
145 | ] |
||
146 | ], |
||
147 | 'year' => [ |
||
148 | 'terms' => [ |
||
149 | 'field' => 'year' |
||
150 | ] |
||
151 | ], |
||
152 | 'doctype' => [ |
||
153 | 'terms' => [ |
||
154 | 'field' => 'doctype' |
||
155 | ] |
||
156 | ], |
||
157 | 'hasFiles' => [ |
||
158 | 'terms' => [ |
||
159 | 'field' => 'hasFiles' |
||
160 | ] |
||
161 | ], |
||
162 | 'universityCollection' => [ |
||
163 | 'terms' => [ |
||
164 | 'script' => [ |
||
165 | 'lang' => 'painless', |
||
166 | 'source' => |
||
167 | "for (int i = 0; i < doc['collections'].length; ++i) {". |
||
168 | " if(doc['collections'][i] =='". |
||
169 | $this->clientConfigurationManager->getUniversityCollection() ."') {". |
||
170 | " return 'true';". |
||
171 | " }". |
||
172 | "}". |
||
173 | "return 'false';" |
||
174 | ] |
||
175 | ] |
||
176 | ], |
||
177 | 'persons' => [ |
||
178 | 'terms' => [ |
||
179 | 'field' => 'personData.name', |
||
180 | 'size'=> 1000 |
||
181 | ] |
||
182 | ], |
||
183 | 'creatorRole' => [ |
||
184 | 'terms' => [ |
||
185 | 'script' => [ |
||
186 | 'lang' => 'painless', |
||
187 | 'source' => |
||
188 | "if (". |
||
189 | " doc['creator'].size() > 0 &&". |
||
190 | " doc['creator'].value == '".$this->security->getUser()->getUid()."') {". |
||
191 | " return 'self';". |
||
192 | "}". |
||
193 | "if (". |
||
194 | " doc['creatorRole'].size() > 0 &&". |
||
195 | " doc['creatorRole'].value == '".Security::ROLE_LIBRARIAN."'". |
||
196 | ") {". |
||
197 | " return 'librarian';". |
||
198 | "}". |
||
199 | "if (". |
||
200 | " doc['creatorRole'].size() > 0 &&". |
||
201 | " doc['creatorRole'].value == '".Security::ROLE_RESEARCHER."'". |
||
202 | ") {". |
||
203 | " return 'user';". |
||
204 | "}". |
||
205 | "return 'unknown';" |
||
206 | ] |
||
207 | ] |
||
208 | ] |
||
209 | |||
210 | ] |
||
211 | ] |
||
212 | ]; |
||
213 | |||
214 | // \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($fullQuery, null, 20); |
||
215 | |||
216 | return $fullQuery; |
||
217 | |||
540 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.