Passed
Push — solr_search ( fcad3a...dc5436 )
by
unknown
34:05 queued 14:05
created

start.php ➔ get_entity_list()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 44

Duplication

Lines 14
Ratio 31.82 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 3
dl 14
loc 44
rs 9.216
c 0
b 0
f 0
1
<?php
2
/* File: .../solr_api/start.php
3
 *
4
 * REFERENCE: http://learn.elgg.org/en/stable/guides/web-services.html#api-authentication
5
 * To get the API Key, web services must be enabled: Admin > Web Services > Create API Keys
6
 * Please note that the API calls will display limit of 10
7
 *
8
 * Author: Christine Yu <[email protected]>
9
 * Company: Government of Canada
10
 * Date: June 1st 2018
11
 *
12
 */
13
14
15
elgg_register_event_handler('init','system','solr_api_init');
16
17
function solr_api_init() {
18
19
	//$method, $function, array $parameters = NULL, $description = "",
20
	//	$call_method = "GET", $require_api_auth = false, $require_user_auth = false
21
22
	elgg_ws_expose_function(
23
		'delete.updated_index_list',
24
		'delete_updated_index_list',
25
		[
26
			'guids' => [
27
				'type' => 'array',
28
				'required' => false,
29
				'description' => 'deletes records based on collection of entity guids'
30
			]
31
		],
32
		'deletes record that already updated the solr index',
33
		'POST',
34
		true,
35
		false
36
	);
37
38
39
	elgg_ws_expose_function(
40
        'get.entity_list',
41
        'get_entity_list',
42
        [
43
            'type' => [
44
                'type' => 'string',
45
                'required' => true,
46
                'description' => 'the type of entity in string format',
47
            ],
48
            'subtype' => [
49
                'type' => 'string',
50
                'required' => false,
51
                'description' => 'the subtype of entity in string format, not required',
52
            ],
53
			'offset' => [
54
                'type' => 'int',
55
                'required' => true,
56
                'description' => 'the subtype of entity in string format, not required',
57
            ],
58
59
        ],
60
        'retrieves all entities filtered by type [and subtype]',
61
        'GET',
62
        true,
63
        false
64
	);
65
66
67
	elgg_ws_expose_function(
68
        'get.user_list',
69
        'get_user_list',
70
        [
71
        	'offset' => [
72
        		'type' => 'int',
73
        		'required' => true,
74
        		'description' => 'paging mechanism'
75
        	]
76
        ],
77
        'retrieves a user list',
78
        'GET',
79
        true,
80
        false
81
	);
82
83
	elgg_ws_expose_function(
84
        'get.group_list',
85
        'get_group_list',
86
        [
87
        	'offset' => [
88
        		'type' => 'int',
89
        		'required' => true,
90
        		'description' => 'api loads 20 groups at a time'
91
        	]
92
        ],
93
        'retrieves a group list',
94
        'GET',
95
        true,
96
        false
97
	);
98
99
100
	elgg_ws_expose_function(
101
		'get.list_of_deleted_records',
102
		'get_list_of_deleted_records',
103
		null,
104
		'retrieves a list of deleted content',
105
		'GET',
106
		true,
107
		false
108
	);
109
}
110
111
112
function delete_updated_index_list($guids) {
113
	$ids = array();
114
	foreach ($guids as $guid) {
115
		// guid must be an integer
116
		if (is_numeric($guid)) 
117
			$ids[] = $guid;
118
		else
119
			return "there is an issue deleting a record in the database, please see function delete_updated_index_list()";
120
	}
121
122
	if (sizeof($ids) > 0) {
123
		$ids = implode(",", $ids);
124
		$query = "DELETE FROM deleted_object_tracker WHERE id IN ({$ids})";
125
		$result = delete_data($query);
126
	}
127
128
	$arr[] = array('message' => 'deleted the following guid');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
129
	$arr[] = $ids;
130
131
	return $arr;
132
}
133
134
135
// this is not an api call, this is a hook that will initiate once user attempts deletion
136
function intercept_object_deletion($event, $type, $object) {
137
	$unixtime = time();
138
139
	$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})";
140
141
	// just in case for some reason, the user is able to delete the same thing twice
142
	try {
143
		$insert_record = insert_data($query);
144
	} catch (Exception $e) {
145
		error_log("error occurred: {$e}");
146
	}
147
148
	$query = "SELECT * FROM deleted_object_tracker";
149
	$deleted_records = get_data($query);
150
151
	return true;
152
}
153
154
155
function get_list_of_deleted_records() {
156
	$query = "SELECT * FROM deleted_object_tracker";
157
	$deleted_records = get_data($query);
158
159
	foreach ($deleted_records as $deleted_record) {
160
		$arr[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
161
			'guid' => $deleted_record->id,
162
			'time_deleted' => $deleted_record->time_deleted
163
		);
164
	}
165
	return $arr;
0 ignored issues
show
Bug introduced by
The variable $arr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
166
}
167
168
169
function get_user_list($offset) {
170
	// @TODO check if the site url is either localhost or local ip address, replace with elgg if it is
171
	$site_url = elgg_get_site_url();
172
	$platform = explode('.', $site_url);
173
	$db_prefix = elgg_get_config('dbprefix');
174
175
	$query = "	SELECT e.guid, ue.name, ue.username, ue.email, e.type, e.time_created, e.enabled
176
				FROM {$db_prefix}users_entity ue 
177
					LEFT JOIN {$db_prefix}entities e ON ue.guid = e.guid 
178
				WHERE e.enabled = 'yes'
179
				LIMIT 10 OFFSET {$offset}";
180
181
	$users = get_data($query);
182
183
	foreach ($users as $user) {
184
		$arr[] = array (
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
185
			'guid' => $user->guid,
186
			'name' => array('en' => $user->name, 'fr' => $user->name),
187
			'username' => $user->username,
188
			'email' => $user->email,
189
			'type' => $user->type,
190
			'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created),
191
			'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created),
192
			'url' => "{$site_url}profile/{$user->username}",
193
			'platform' => $platform[0]
194
		);
195
	}
196
    return $arr;
0 ignored issues
show
Bug introduced by
The variable $arr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
197
}
198
199
200
function get_group_list($offset) {
201
	// @TODO use SQL query syntax instead of using function elgg_get_entities()
202
	$site_url = elgg_get_site_url();
203
	$platform = explode('.', $site_url);
204
205
	$groups = elgg_get_entities(array(
206
		'type' => 'group',
207
		'limit' => 10,
208
		'offset' => $offset
209
	));
210
211
	foreach ($groups as $group) {
212
213 View Code Duplication
		if (is_Json($group->name)) {
214
			$name_array = json_decode($group->name, true);
215
			$name_array['en'] = str_replace('"', '\"', $name_array['en']);
216
			$name_array['fr'] = str_replace('"', '\"', $name_array['fr']);
217
		} else {
218
			$name_array['en'] = $group->name_array;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$name_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $name_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
219
			$name_array['fr'] = $group->name;
0 ignored issues
show
Bug introduced by
The variable $name_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
220
		}
221
222 View Code Duplication
		if (is_Json($group->description)) {
223
			$description_array = json_decode($group->description, true);
224
			$description_array['en'] = str_replace('"', '\"', $description_array['en']);
225
			$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
226
		} else {
227
			$description_array['en'] = $group->description;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$description_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $description_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
228
			$description_array['fr'] = $group->description;
0 ignored issues
show
Bug introduced by
The variable $description_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
229
		}
230
231
		$arr[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
232
			'guid' => $group->getGUID(),
233
			'name' => $name_array,
234
			'description' => $description_array,
235
			'type' => $group->getType(),
236
			'access_id' => $group->access_id,
237
			'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created),
238
			'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated),
239
			'url' => $group->getURL(),
240
			'platform' => $platform[0]
241
		);
242
	}
243
    return $arr;
0 ignored issues
show
Bug introduced by
The variable $arr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
244
}
245
246
247
function get_entity_list($type, $subtype, $offset) {
248
	$site_url = elgg_get_site_url();
249
	$platform = explode('.', $site_url);
250
	$entities = elgg_get_entities(array(
251
		'type' => $type,
252
		'subtype' => $subtype,
253
		'limit' => 10,
254
		'offset' => $offset
255
	));
256
257
	foreach ($entities as $entity) {
258
		
259 View Code Duplication
		if (is_Json($entity->title)) {
260
			$title_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'en'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$title_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $title_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
261
			$title_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'fr'));
0 ignored issues
show
Bug introduced by
The variable $title_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
262
		} else {
263
			$title_array['en'] = $entity->title;
264
			$title_array['fr'] = $entity->title;
265
		}
266
267 View Code Duplication
		if (is_Json($entity->description)) {
268
			$description_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'en'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$description_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $description_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
269
			$description_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'fr'));
0 ignored issues
show
Bug introduced by
The variable $description_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
270
		} else {
271
			$description_array['en'] = $entity->description;
272
			$description_array['fr'] = $entity->description;
273
		}
274
275
		$arr[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
276
			'guid' => $entity->getGUID(), 
277
			'title' => $title_array,
278
			'description' => $description_array,
279
			'type' => $entity->getType(),
280
			'subtype' => $entity->getSubtype(),
281
			'access_id' => $entity->access_id,
282
			'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created),
283
			'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated),
284
			'url' => $entity->getURL(),
285
			'platform' => $platform[0]
286
		);
287
	}
288
289
	return $arr;
0 ignored issues
show
Bug introduced by
The variable $arr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
290
}
291
292
293
// @TODO check if this function already exist in other modules
294
function is_Json($string) {
295
	json_decode($string);
296
	return (json_last_error() == JSON_ERROR_NONE);
297
}
298
299