Passed
Push — master ( 5063d9...a1994b )
by Jeroen
22:08
created

search_hooks.php ➔ search_objects_hook()   C

Complexity

Conditions 10
Paths 70

Size

Total Lines 64
Code Lines 38

Duplication

Lines 29
Ratio 45.31 %

Importance

Changes 0
Metric Value
cc 10
eloc 38
nc 70
nop 4
dl 29
loc 64
rs 6.309
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Elgg core search.
4
 *
5
 * @package Elgg
6
 * @subpackage Search
7
 */
8
9
/**
10
 * Get objects that match the search parameters.
11
 *
12
 * @param string $hook   Hook name
13
 * @param string $type   Hook type
14
 * @param array  $value  Empty array
15
 * @param array  $params Search parameters
16
 * @return array
17
 */
18
function search_objects_hook($hook, $type, $value, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
20
	$params['joins'] = (array) elgg_extract('joins', $params, []);
21
	$params['wheres'] = (array) elgg_extract('wheres', $params, []);
22
	
23
	$query = sanitise_string($params['query']);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
24
	$query_parts = explode(' ', $query);
25
26
	$db_prefix = elgg_get_config('dbprefix');
27
	
28
	$params['joins'][] = "JOIN {$db_prefix}metadata md ON e.guid = md.entity_guid";
29
	
30
	$fields = ['title', 'description'];
31
	$wheres = [];
32 View Code Duplication
	foreach ($fields as $field) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
33
		$sublikes = [];
34
		foreach ($query_parts as $query_part) {
35
			$query_part = sanitise_string($query_part);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
36
			if (strlen($query_part) == 0) {
37
				continue;
38
			}
39
			$sublikes[] = "(md.value LIKE '%{$query_part}%')";
40
		}
41
		
42
		if (empty($sublikes)) {
43
			continue;
44
		}
45
		
46
		$wheres[] = "(md.name = '{$field}' AND (" . implode(' AND ', $sublikes) . "))";
47
	}
48
	
49 View Code Duplication
	if (!empty($wheres)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
50
		$params['wheres'][] = '(' . implode(' OR ', $wheres) . ')';
51
	}
52
	
53
	$params['count'] = true;
54
	$count = elgg_get_entities($params);
55
	
56
	// no need to continue if nothing here.
57
	if (!$count) {
58
		return ['entities' => [], 'count' => $count];
59
	}
60
	
61
	$params['count'] = false;
62 View Code Duplication
	if (isset($params['sort']) || !isset($params['order_by'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
63
		$params['order_by'] = search_get_order_by_sql('e', 'oe', $params['sort'], $params['order']);
64
	}
65
	$params['preload_owners'] = true;
66
	$entities = elgg_get_entities($params);
67
68
	// add the volatile data for why these entities have been returned.
69 View Code Duplication
	foreach ($entities as $entity) {
1 ignored issue
show
Bug introduced by
The expression $entities of type object<ElggBatch>|false|integer|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
70
		$title = search_get_highlighted_relevant_substrings($entity->getDisplayName(), $params['query']);
71
		$entity->setVolatileData('search_matched_title', $title);
72
73
		$desc = search_get_highlighted_relevant_substrings($entity->description, $params['query']);
74
		$entity->setVolatileData('search_matched_description', $desc);
75
	}
76
77
	return [
78
		'entities' => $entities,
79
		'count' => $count,
80
	];
81
}
82
83
/**
84
 * Get groups that match the search parameters.
85
 *
86
 * @param string $hook   Hook name
87
 * @param string $type   Hook type
88
 * @param array  $value  Empty array
89
 * @param array  $params Search parameters
90
 * @return array
91
 */
92
function search_groups_hook($hook, $type, $value, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
94
	$params['joins'] = (array) elgg_extract('joins', $params, []);
95
	$params['wheres'] = (array) elgg_extract('wheres', $params, []);
96
	
97
	$query = sanitise_string($params['query']);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
98
	$query_parts = explode(' ', $query);
99
100
	$db_prefix = elgg_get_config('dbprefix');
101
102
	$params['joins'][] = "JOIN {$db_prefix}metadata md ON e.guid = md.entity_guid";
103
	
104
	$fields = ['name', 'description'];
105
	$wheres = [];
106 View Code Duplication
	foreach ($fields as $field) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
107
		$sublikes = [];
108
		foreach ($query_parts as $query_part) {
109
			$query_part = sanitise_string($query_part);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
110
			if (strlen($query_part) == 0) {
111
				continue;
112
			}
113
			$sublikes[] = "(md.value LIKE '%{$query_part}%')";
114
		}
115
		
116
		if (empty($sublikes)) {
117
			continue;
118
		}
119
		
120
		$wheres[] = "(md.name = '{$field}' AND (" . implode(' AND ', $sublikes) . "))";
121
	}
122
	
123 View Code Duplication
	if (!empty($wheres)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
124
		$params['wheres'][] = '(' . implode(' OR ', $wheres) . ')';
125
	}
126
		
127
	$params['count'] = true;
128
	
129
	$count = elgg_get_entities($params);
130
	
131
	// no need to continue if nothing here.
132
	if (!$count) {
133
		return ['entities' => [], 'count' => $count];
134
	}
135
	
136
	$params['count'] = false;
137 View Code Duplication
	if (isset($params['sort']) || !isset($params['order_by'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
138
		$params['order_by'] = search_get_order_by_sql('e', '', $params['sort'], $params['order']);
139
	}
140
	$entities = elgg_get_entities($params);
141
142
	// add the volatile data for why these entities have been returned.
143 View Code Duplication
	foreach ($entities as $entity) {
1 ignored issue
show
Bug introduced by
The expression $entities of type object<ElggBatch>|false|integer|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
144
		$name = search_get_highlighted_relevant_substrings($entity->getDisplayName(), $query);
145
		$entity->setVolatileData('search_matched_title', $name);
146
147
		$description = search_get_highlighted_relevant_substrings($entity->description, $query);
148
		$entity->setVolatileData('search_matched_description', $description);
149
	}
150
151
	return [
152
		'entities' => $entities,
153
		'count' => $count,
154
	];
155
}
156
157
/**
158
 * Get users that match the search parameters.
159
 *
160
 * Searches on username, display name, and profile fields
161
 *
162
 * @param string $hook   Hook name
163
 * @param string $type   Hook type
164
 * @param array  $value  Empty array
165
 * @param array  $params Search parameters
166
 * @return array
167
 */
168
function search_users_hook($hook, $type, $value, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
170
	$params['joins'] = (array) elgg_extract('joins', $params, []);
171
	$params['wheres'] = (array) elgg_extract('wheres', $params, []);
172
	
173
	$query = sanitise_string($params['query']);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
174
	$query_parts = explode(' ', $query);
175
176
	$db_prefix = elgg_get_config('dbprefix');
177
	
178
	$params['joins'][] = "JOIN {$db_prefix}metadata md ON e.guid = md.entity_guid";
179
	
180
	$fields = ['username', 'name'];
181
	$wheres = [];
182 View Code Duplication
	foreach ($fields as $field) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
183
		$sublikes = [];
184
		foreach ($query_parts as $query_part) {
185
			$query_part = sanitise_string($query_part);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
186
			if (strlen($query_part) == 0) {
187
				continue;
188
			}
189
			$sublikes[] = "(md.value LIKE '%{$query_part}%')";
190
		}
191
		
192
		if (empty($sublikes)) {
193
			continue;
194
		}
195
		
196
		$wheres[] = "(md.name = '{$field}' AND (" . implode(' AND ', $sublikes) . "))";
197
	}
198
	
199
	// profile fields
200
	$profile_fields = array_keys(elgg_get_config('profile_fields'));
201
	if (!empty($profile_fields)) {
202
		$params['joins'][] = "JOIN {$db_prefix}annotations an ON e.guid = an.entity_guid";
203
		
204
		// get the where clauses for the annotation names
205
		// can't use egef_annotations() because the n_table join comes too late.
206
		$clauses = _elgg_entities_get_metastrings_options('annotation', [
207
			'annotation_names' => $profile_fields,
208
209
			// avoid notices
210
			'annotation_values' => null,
211
			'annotation_name_value_pairs' => null,
212
			'annotation_name_value_pairs_operator' => null,
213
			'annotation_case_sensitive' => null,
214
			'order_by_annotation' => null,
215
			'annotation_owner_guids' => null,
216
		]);
217
218
		$params['joins'] = array_merge($clauses['joins'], $params['joins']);
219
		$wheres[] = "(({$clauses['wheres'][0]}) AND an.value LIKE '%$query%')";
220
	}
221
222 View Code Duplication
	if (!empty($wheres)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
223
		$params['wheres'][] = '(' . implode(' OR ', $wheres) . ')';
224
	}
225
226
	$params['count'] = true;
227
	$count = elgg_get_entities($params);
228
229
	// no need to continue if nothing here.
230
	if (!$count) {
231
		return ['entities' => [], 'count' => $count];
232
	}
233
	
234
	$params['count'] = false;
235 View Code Duplication
	if (isset($params['sort']) || !isset($params['order_by'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
236
		$params['order_by'] = search_get_order_by_sql('e', 'ue', $params['sort'], $params['order']);
237
	}
238
	$entities = elgg_get_entities($params);
239
	/* @var ElggUser[] $entities */
240
241
	// add the volatile data for why these entities have been returned.
242
	foreach ($entities as $entity) {
0 ignored issues
show
Bug introduced by
The expression $entities of type object<ElggBatch>|false|integer|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
243
		$title = search_get_highlighted_relevant_substrings($entity->getDisplayName(), $query);
244
245
		// include the username if it matches but the display name doesn't.
246
		if (false !== strpos($entity->username, $query)) {
247
			$username = search_get_highlighted_relevant_substrings($entity->username, $query);
248
			$title .= " ($username)";
249
		}
250
251
		$entity->setVolatileData('search_matched_title', $title);
252
253
		if (!empty($profile_fields)) {
254
			$matched = '';
255
			foreach ($profile_fields as $shortname) {
256
				$annotations = $entity->getAnnotations([
257
					'annotation_names' => "profile:$shortname",
258
					'limit' => false,
259
				]);
260
				$values = array_map(function (ElggAnnotation $a) {
261
					return $a->value;
262
				}, $annotations);
263
				foreach ($values as $text) {
264
					if (stristr($text, $query)) {
265
						$matched .= elgg_echo("profile:{$shortname}") . ': '
266
								. search_get_highlighted_relevant_substrings($text, $query);
267
					}
268
				}
269
			}
270
	
271
			$entity->setVolatileData('search_matched_description', $matched);
272
		}
273
	}
274
275
	return [
276
		'entities' => $entities,
277
		'count' => $count,
278
	];
279
}
280
281
/**
282
 * Get entities with tags that match the search parameters.
283
 *
284
 * @param string $hook   Hook name
285
 * @param string $type   Hook type
286
 * @param array  $value  Empty array
287
 * @param array  $params Search parameters
288
 * @return array
289
 */
290
function search_tags_hook($hook, $type, $value, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
291
292
	$params['joins'] = (array) elgg_extract('joins', $params, []);
293
	$params['wheres'] = (array) elgg_extract('wheres', $params, []);
294
295
	$db_prefix = elgg_get_config('dbprefix');
296
297
	$valid_tag_names = elgg_get_registered_tag_metadata_names();
298
299
	// @todo will need to split this up to support searching multiple tags at once.
300
	$query = sanitise_string($params['query']);
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
301
302
	// if passed a tag metadata name, only search on that tag name.
303
	// tag_name isn't included in the params because it's specific to
304
	// tag searches.
305
	if ($tag_names = get_input('tag_names')) {
306
		if (is_array($tag_names)) {
307
			$search_tag_names = $tag_names;
308
		} else {
309
			$search_tag_names = [$tag_names];
310
		}
311
312
		// check these are valid to avoid arbitrary metadata searches.
313
		foreach ($search_tag_names as $i => $tag_name) {
314
			if (!in_array($tag_name, $valid_tag_names)) {
315
				unset($search_tag_names[$i]);
316
			}
317
		}
318
	} else {
319
		$search_tag_names = $valid_tag_names;
320
	}
321
322
	if (!$search_tag_names) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search_tag_names of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
323
		return ['entities' => [], 'count' => $count];
0 ignored issues
show
Bug introduced by
The variable $count seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
324
	}
325
326
	// don't use elgg_get_entities_from_metadata() here because of
327
	// performance issues.  since we don't care what matches at this point
328
	// use an IN clause to grab everything that matches at once and sort
329
	// out the matches later.
330
	$params['joins'][] = "JOIN {$db_prefix}metadata md on e.guid = md.entity_guid";
331
332
	$access = _elgg_get_access_where_sql([
333
		'table_alias' => 'md',
334
		'guid_column' => 'entity_guid',
335
	]);
336
	$sanitised_tags = [];
337
338
	foreach ($search_tag_names as $tag) {
339
		$sanitised_tags[] = '"' . sanitise_string($tag) . '"';
0 ignored issues
show
Deprecated Code introduced by
The function sanitise_string() has been deprecated with message: Use query parameters where possible

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
340
	}
341
342
	$tags_in = implode(',', $sanitised_tags);
343
344
	$params['wheres'][] = "(md.name IN ($tags_in) AND md.value = '$query' AND $access)";
345
346
	$params['count'] = true;
347
	$count = elgg_get_entities($params);
348
349
	// no need to continue if nothing here.
350
	if (!$count) {
351
		return ['entities' => [], 'count' => $count];
352
	}
353
	
354
	$params['count'] = false;
355 View Code Duplication
	if (isset($params['sort']) || !isset($params['order_by'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
356
		$params['order_by'] = search_get_order_by_sql('e', null, $params['sort'], $params['order']);
357
	}
358
	$entities = elgg_get_entities($params);
359
360
	// add the volatile data for why these entities have been returned.
361
	foreach ($entities as $entity) {
0 ignored issues
show
Bug introduced by
The expression $entities of type object<ElggBatch>|false|integer|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
362
		$matched_tags_strs = [];
363
364
		// get tags for each tag name requested to find which ones matched.
365
		foreach ($search_tag_names as $tag_name) {
366
			$tags = $entity->getTags($tag_name);
367
368
			// @todo make one long tag string and run this through the highlight
369
			// function.  This might be confusing as it could chop off
370
			// the tag labels.
371
			if (in_array(strtolower($query), array_map('strtolower', $tags))) {
372
				if (is_array($tags)) {
373
					$tag_name_str = elgg_echo("tag_names:$tag_name");
374
					$matched_tags_strs[] = "$tag_name_str: " . implode(', ', $tags);
375
				}
376
			}
377
		}
378
379
		$title_str = elgg_get_excerpt($entity->getDisplayName(), 300);
380
		$desc_str = elgg_get_excerpt($entity->description, 300);
381
		
382
		$tags_str = implode('. ', $matched_tags_strs);
383
		$tags_str = search_get_highlighted_relevant_substrings($tags_str, $params['query'], 30, 300, true);
384
385
		$entity->setVolatileData('search_matched_title', $title_str);
386
		$entity->setVolatileData('search_matched_description', $desc_str);
387
		$entity->setVolatileData('search_matched_extra', $tags_str);
388
	}
389
390
	return [
391
		'entities' => $entities,
392
		'count' => $count,
393
	];
394
}
395
396
/**
397
 * Register tags as a custom search type.
398
 *
399
 * @param string $hook   Hook name
400
 * @param string $type   Hook type
401
 * @param array  $value  Array of custom search types
402
 * @param array  $params Search parameters
403
 * @return array
404
 */
405
function search_custom_types_tags_hook($hook, $type, $value, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
406
	$value[] = 'tags';
407
	return $value;
408
}
409