We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 42 |
| Paths | 17472 |
| Total Lines | 192 |
| Code Lines | 105 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 1 |
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 |
||
| 217 | public function makeFacetsMenuArray($facets) |
||
| 218 | { |
||
| 219 | $menuArray = []; |
||
| 220 | // Set default value for facet search. |
||
| 221 | $search = [ |
||
| 222 | 'query' => '*:*', |
||
| 223 | 'params' => [ |
||
| 224 | 'component' => [ |
||
| 225 | 'facetset' => [ |
||
| 226 | 'facet' => [] |
||
| 227 | ] |
||
| 228 | ] |
||
| 229 | ] |
||
| 230 | ]; |
||
| 231 | |||
| 232 | // Set needed parameters for facet search. |
||
| 233 | if (empty($search['params']['filterquery'])) { |
||
| 234 | $search['params']['filterquery'] = []; |
||
| 235 | } |
||
| 236 | |||
| 237 | $fields = Solr::getFields(); |
||
| 238 | |||
| 239 | // Set search query. |
||
| 240 | $searchParams = $this->searchParams; |
||
| 241 | if ( |
||
| 242 | (!empty($searchParams['fulltext'])) |
||
| 243 | || preg_match('/' . $fields['fulltext'] . ':\((.*)\)/', trim($searchParams['query']), $matches) |
||
| 244 | ) { |
||
| 245 | // If the query already is a fulltext query e.g using the facets |
||
| 246 | $searchParams['query'] = empty($matches[1]) ? $searchParams['query'] : $matches[1]; |
||
| 247 | // Search in fulltext field if applicable. Query must not be empty! |
||
| 248 | if (!empty($this->searchParams['query'])) { |
||
| 249 | $search['query'] = $fields['fulltext'] . ':(' . Solr::escapeQuery(trim($searchParams['query'])) . ')'; |
||
| 250 | } |
||
| 251 | } else { |
||
| 252 | // Retain given search field if valid. |
||
| 253 | if (!empty($searchParams['query'])) { |
||
| 254 | $search['query'] = Solr::escapeQueryKeepField(trim($searchParams['query']), $this->settings['storagePid']); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | // if collections are given, we prepare the collection query string |
||
| 259 | // extract collections from collection parameter |
||
| 260 | $collection = null; |
||
| 261 | if ($this->searchParams['collection']) { |
||
| 262 | foreach(explode(',', $this->searchParams['collection']) as $collectionEntry) { |
||
| 263 | $collection[] = $this->collectionRepository->findByUid($collectionEntry); |
||
| 264 | } |
||
| 265 | |||
| 266 | } |
||
| 267 | if ($collection) { |
||
| 268 | $collectionsQueryString = ''; |
||
| 269 | $virtualCollectionsQueryString = ''; |
||
| 270 | foreach ($collection as $collectionEntry) { |
||
| 271 | // check for virtual collections query string |
||
| 272 | if($collectionEntry->getIndexSearch()) { |
||
| 273 | $virtualCollectionsQueryString .= empty($virtualCollectionsQueryString) ? '(' . $collectionEntry->getIndexSearch() . ')' : ' OR ('. $collectionEntry->getIndexSearch() . ')' ; |
||
| 274 | } |
||
| 275 | else { |
||
| 276 | $collectionsQueryString .= empty($collectionsQueryString) ? '"' . $collectionEntry->getIndexName() . '"' : ' OR "' . $collectionEntry->getIndexName() . '"'; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | // distinguish between simple collection browsing and actual searching within the collection(s) |
||
| 281 | if(!empty($collectionsQueryString)) { |
||
| 282 | if(empty($searchParams['query'])) { |
||
| 283 | $collectionsQueryString = '(collection_faceting:(' . $collectionsQueryString . ') AND toplevel:true AND partof:0)'; |
||
| 284 | } else { |
||
| 285 | $collectionsQueryString = '(collection_faceting:(' . $collectionsQueryString . '))'; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | // virtual collections might query documents that are neither toplevel:true nor partof:0 and need to be searched separatly |
||
| 290 | if(!empty($virtualCollectionsQueryString)) { |
||
| 291 | $virtualCollectionsQueryString = '(' . $virtualCollectionsQueryString . ')'; |
||
| 292 | } |
||
| 293 | |||
| 294 | // combine both querystrings into a single filterquery via OR if both are given, otherwise pass either of those |
||
| 295 | $search['params']['filterquery'][]['query'] = implode(" OR ", array_filter([$collectionsQueryString, $virtualCollectionsQueryString])); |
||
| 296 | } |
||
| 297 | |||
| 298 | // add filter query for date search |
||
| 299 | if (!empty($this->searchParams['dateFrom']) && !empty($this->searchParams['dateTo'])) { |
||
| 300 | // combine dateFrom and dateTo into filterquery as range search |
||
| 301 | $search['params']['filterquery'][]['query'] = '{!join from=' . $fields['uid'] . ' to=' . $fields['uid'] . '}' . $fields['date'] . ':[' . $this->searchParams['dateFrom'] . ' TO ' . $this->searchParams['dateTo'] . ']'; |
||
| 302 | } |
||
| 303 | |||
| 304 | // Add extended search query. |
||
| 305 | if ( |
||
| 306 | !empty($searchParams['extQuery']) |
||
| 307 | && is_array($searchParams['extQuery']) |
||
| 308 | ) { |
||
| 309 | // If the search query is already set by the simple search field, we have to reset it. |
||
| 310 | $search['query'] = ''; |
||
| 311 | $allowedOperators = ['AND', 'OR', 'NOT']; |
||
| 312 | $numberOfExtQueries = count($searchParams['extQuery']); |
||
| 313 | for ($i = 0; $i < $numberOfExtQueries; $i++) { |
||
| 314 | if (!empty($searchParams['extQuery'][$i])) { |
||
| 315 | if ( |
||
| 316 | in_array($searchParams['extOperator'][$i], $allowedOperators) |
||
| 317 | ) { |
||
| 318 | if (!empty($search['query'])) { |
||
| 319 | $search['query'] .= ' ' . $searchParams['extOperator'][$i] . ' '; |
||
| 320 | } |
||
| 321 | $search['query'] .= Indexer::getIndexFieldName($searchParams['extField'][$i], $this->settings['storagePid']) . ':(' . Solr::escapeQuery($searchParams['extQuery'][$i]) . ')'; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | if (isset($this->searchParams['fq']) && is_array($this->searchParams['fq'])) { |
||
| 328 | foreach ($this->searchParams['fq'] as $fq) { |
||
| 329 | $search['params']['filterquery'][]['query'] = $fq; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | // Get applicable facets. |
||
| 334 | $solr = Solr::getInstance($this->settings['solrcore']); |
||
| 335 | if (!$solr->ready) { |
||
| 336 | $this->logger->error('Apache Solr not available'); |
||
| 337 | return []; |
||
| 338 | } |
||
| 339 | |||
| 340 | foreach (array_keys($facets) as $field) { |
||
| 341 | $search['params']['component']['facetset']['facet'][] = [ |
||
| 342 | 'type' => 'field', |
||
| 343 | 'mincount' => '1', |
||
| 344 | 'key' => $field, |
||
| 345 | 'field' => $field, |
||
| 346 | 'limit' => $this->settings['limitFacets'], |
||
| 347 | 'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' |
||
| 348 | ]; |
||
| 349 | } |
||
| 350 | |||
| 351 | // Set additional query parameters. |
||
| 352 | $search['params']['start'] = 0; |
||
| 353 | $search['params']['rows'] = 0; |
||
| 354 | // Set query. |
||
| 355 | $search['params']['query'] = $search['query']; |
||
| 356 | // Perform search. |
||
| 357 | $selectQuery = $solr->service->createSelect($search['params']); |
||
| 358 | $results = $solr->service->select($selectQuery); |
||
| 359 | $facet = $results->getFacetSet(); |
||
| 360 | |||
| 361 | $facetCollectionArray = []; |
||
| 362 | |||
| 363 | // replace everything expect numbers and comma |
||
| 364 | $facetCollections = preg_replace('/[^\d,]/', '', $this->settings['facetCollections']); |
||
| 365 | |||
| 366 | if (!empty($facetCollections)) { |
||
| 367 | $collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); |
||
| 368 | |||
| 369 | /** @var Collection $collection */ |
||
| 370 | foreach ($collections as $collection) { |
||
| 371 | $facetCollectionArray[] = $collection->getIndexName(); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | // Process results. |
||
| 376 | if ($facet) { |
||
| 377 | foreach ($facet as $field => $values) { |
||
| 378 | $entryArray = []; |
||
| 379 | $entryArray['field'] = substr($field, 0, strpos($field, '_faceting')); |
||
| 380 | $entryArray['count'] = 0; |
||
| 381 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 382 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 383 | // Count number of facet values. |
||
| 384 | $i = 0; |
||
| 385 | foreach ($values as $value => $count) { |
||
| 386 | if ($count > 0) { |
||
| 387 | // check if facet collection configuration exists |
||
| 388 | if (!empty($this->settings['facetCollections'])) { |
||
| 389 | if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { |
||
| 390 | continue; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | $entryArray['count']++; |
||
| 394 | if ($entryArray['ITEM_STATE'] == 'NO') { |
||
| 395 | $entryArray['ITEM_STATE'] = 'IFSUB'; |
||
| 396 | } |
||
| 397 | $entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); |
||
| 398 | if (++$i == $this->settings['limit']) { |
||
| 399 | break; |
||
| 400 | } |
||
| 401 | } else { |
||
| 402 | break; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | $menuArray[] = $entryArray; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | return $menuArray; |
||
| 409 | } |
||
| 512 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.