Passed
Push — api-test ( 1d82bb...9c7455 )
by
unknown
55:12
created

start.php ➔ delete_updated_index_list()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 5
nop 1
dl 0
loc 21
rs 9.0534
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
	$site_url = elgg_get_site_url();
171
	$db_prefix = elgg_get_config('dbprefix');
172
173
	$query = "	SELECT e.guid, ue.name, ue.username, ue.email, e.type, e.time_created, e.enabled
174
				FROM {$db_prefix}users_entity ue 
175
					LEFT JOIN {$db_prefix}entities e ON ue.guid = e.guid 
176
				WHERE e.enabled = 'yes'
177
				LIMIT 10 OFFSET {$offset}";
178
179
	$users = get_data($query);
180
181
	foreach ($users as $user) {
182
		$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...
183
			'guid' => $user->guid,
184
			'name' => array('en' => $user->name, 'fr' => $user->name),
185
			'username' => $user->username,
186
			'email' => $user->email,
187
			'type' => $user->type,
188
			'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created),
189
			'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created),
190
			'url' => "{$site_url}profile/{$user->username}"
191
		);
192
	}
193
    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...
194
}
195
196
197
function get_group_list($offset) {
198
	// @TODO use SQL query syntax instead of using function elgg_get_entities()
199
	$groups = elgg_get_entities(array(
200
		'type' => 'group',
201
		'limit' => 10,
202
		'offset' => $offset
203
	));
204
205
	foreach ($groups as $group) {
206
207 View Code Duplication
		if (is_Json($group->name)) {
208
			$name_array = json_decode($group->name, true);
209
			$name_array['en'] = str_replace('"', '\"', $name_array['en']);
210
			$name_array['fr'] = str_replace('"', '\"', $name_array['fr']);
211
		} else {
212
			$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...
213
			$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...
214
		}
215
216 View Code Duplication
		if (is_Json($group->description)) {
217
			$description_array = json_decode($group->description, true);
218
			$description_array['en'] = str_replace('"', '\"', $description_array['en']);
219
			$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
220
		} else {
221
			$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...
222
			$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...
223
		}
224
225
		$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...
226
			'guid' => $group->getGUID(),
227
			'name' => $name_array,
228
			'description' => $description_array,
229
			'type' => $group->getType(),
230
			'access_id' => $group->access_id,
231
			'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created),
232
			'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated),
233
			'url' => $group->getURL()
234
		);
235
	}
236
    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...
237
}
238
239
240
function get_entity_list($type, $subtype, $offset) {
241
	$entities = elgg_get_entities(array(
242
		'type' => $type,
243
		'subtype' => $subtype,
244
		'limit' => 10,
245
		'offset' => $offset
246
	));
247
248
	foreach ($entities as $entity) {
249
250 View Code Duplication
		if (is_Json($entity->title)) {
251
			$title_array = json_decode($entity->title, true);
252
			if (!isset($title_array['en']) || !isset($title_array['en'])) {
253
				$title_array['en'] = str_replace('"', '\"', $title_array);
254
				$title_array['fr'] = str_replace('"', '\"', $title_array);
255
			} else {
256
				$title_array['en'] = str_replace('"', '\"', $title_array['en']);
257
				$title_array['fr'] = str_replace('"', '\"', $title_array['fr']);
258
			}
259
		} else {
260
			$title_array['en'] = $entity->title;
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'] = $entity->title;
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
		}
263
264 View Code Duplication
		if (is_Json($entity->description)) {
265
			$description_array = json_decode($entity->description, true);
266
			if (!isset($description_array['en']) || !isset($description_array['en'])) {
267
				$description_array['en'] = str_replace('"', '\"', $description_array);
268
				$description_array['fr'] = str_replace('"', '\"', $description_array);
269
			} else {
270
				$description_array['en'] = str_replace('"', '\"', $description_array['en']);
271
				$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
272
			}
273
		} else {
274
			$description_array['en'] = $entity->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...
275
			$description_array['fr'] = $entity->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...
276
		}
277
278
		$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...
279
			'guid' => $entity->getGUID(), 
280
			'title' => $title_array,
281
			'description' => $description_array,
282
			'type' => $entity->getType(),
283
			'subtype' => $entity->getSubtype(),
284
			'access_id' => $entity->access_id,
285
			'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created),
286
			'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated),
287
			'url' => $entity->getURL()
288
		);
289
	}
290
291
	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...
292
}
293
294
295
// @TODO check if this function already exist in other modules
296
function is_Json($string) {
297
	json_decode($string);
298
	return (json_last_error() == JSON_ERROR_NONE);
299
}
300
301