Conditions | 7 |
Paths | 24 |
Total Lines | 57 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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:
1 | <?php declare(strict_types=1); |
||
41 | public function search(string $term, array $entities, Context $context, int $limit = 5): array |
||
42 | { |
||
43 | $index = []; |
||
44 | $term = (string) mb_eregi_replace('\s(or)\s', '|', $term); |
||
45 | $term = (string) mb_eregi_replace('\s(and)\s', ' + ', $term); |
||
46 | $term = (string) mb_eregi_replace('\s(not)\s', ' -', $term); |
||
47 | |||
48 | foreach ($entities as $entityName) { |
||
49 | if (!$context->isAllowed($entityName . ':' . AclRoleDefinition::PRIVILEGE_READ)) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | $indexer = $this->registry->getIndexer($entityName); |
||
54 | $alias = $this->adminEsHelper->getIndex($indexer->getName()); |
||
55 | $index[] = ['index' => $alias]; |
||
56 | |||
57 | $index[] = $indexer->globalCriteria($term, $this->buildSearch($term, $limit))->toArray(); |
||
58 | } |
||
59 | |||
60 | $responses = $this->client->msearch(['body' => $index]); |
||
61 | |||
62 | $result = []; |
||
63 | foreach ($responses['responses'] as $response) { |
||
64 | if (empty($response['hits']['hits'])) { |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | $index = $response['hits']['hits'][0]['_index']; |
||
69 | |||
70 | $result[$index] = [ |
||
71 | 'total' => $response['hits']['total']['value'], |
||
72 | 'hits' => [], |
||
73 | ]; |
||
74 | |||
75 | foreach ($response['hits']['hits'] as $hit) { |
||
76 | $result[$index]['hits'][] = [ |
||
77 | 'id' => $hit['_id'], |
||
78 | 'score' => $hit['_score'], |
||
79 | 'parameters' => $hit['_source']['parameters'], |
||
80 | 'entityName' => $hit['_source']['entityName'], |
||
81 | ]; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $mapped = []; |
||
86 | foreach ($result as $index => $values) { |
||
87 | $entityName = $values['hits'][0]['entityName']; |
||
88 | $indexer = $this->registry->getIndexer($entityName); |
||
89 | |||
90 | $data = $indexer->globalData($values, $context); |
||
91 | $data['indexer'] = $indexer->getName(); |
||
92 | $data['index'] = (string) $index; |
||
93 | |||
94 | $mapped[$indexer->getEntity()] = $data; |
||
95 | } |
||
96 | |||
97 | return $mapped; |
||
98 | } |
||
121 |