Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/profile/views/default/profile/fields.php (1 issue)

implicit conversion of array to boolean.

Best Practice Bug Minor
1
<?php
2
/**
3
 * @uses $vars['entity']       The user entity
4
 * @uses $vars['microformats'] Mapping of fieldnames to microformats
5
 * @uses $vars['fields']       Array of profile fields to show
6
 */
7
8
$microformats = [
9
	'mobile' => 'tel p-tel',
10
	'phone' => 'tel p-tel',
11
	'website' => 'url u-url',
12
	'contactemail' => 'email u-email',
13
];
14
$microformats = array_merge($microformats, (array) elgg_extract('microformats', $vars, []));
15
16
$user = elgg_extract('entity', $vars);
17
if (!($user instanceof ElggUser)) {
18
	return;
19
}
20
21
$fields = (array) elgg_extract('fields', $vars, []);
22
if (empty($fields)) {
23
	return;
24
}
25
26
// move description to the bottom of the list
27
if (isset($fields['description'])) {
28
	$temp = $fields['description'];
29
	unset($fields['description']);
30
	$fields['description'] = $temp;
31
}
32
33
foreach ($fields as $shortname => $valtype) {
34
	$annotations = $user->getAnnotations([
35
		'annotation_names' => "profile:$shortname",
36
		'limit' => false,
37
	]);
38
	$values = array_map(function (ElggAnnotation $a) {
39
		return $a->value;
40
	}, $annotations);
41
42
	if (!$values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values 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...
43
		continue;
44
	}
45
	// emulate metadata API
46
	$value = (count($values) === 1) ? $values[0] : $values;
47
48
	// validate urls
49
	if ($valtype == 'url' && !preg_match('~^https?\://~i', $value)) {
50
		$value = "http://$value";
51
	}
52
53
	$class = elgg_extract($shortname, $microformats, '');
54
55
	$field_title = elgg_echo("profile:{$shortname}");
56
	$field_value = elgg_format_element('span', [
57
		'class' => $class,
58
	], elgg_view("output/{$valtype}", [
59
		'value' => $value,
60
	]));
61
62
	echo <<<___FIELD
63
	<div class='clearfix profile-field'>
64
		<div class='elgg-col elgg-col-1of5'>
65
			<b>{$field_title}:</b>
66
		</div>
67
		<div class='elgg-col elgg-col-4of5'>
68
			{$field_value}
69
		</div>
70
	</div>
71
___FIELD;
72
}
73