Completed
Push — api-test ( 6486f1...2894cc )
by
unknown
27:43
created

start.php ➔ api_hook_handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
// REFERENCE: http://learn.elgg.org/en/stable/guides/web-services.html#api-authentication
4
// Admin > Web Services > Create API Keys
5
6
elgg_register_event_handler('init','system','solr_api_init');
7
8
9
function your_plugin_auth_handler($credentials) {
10
   error_log("credentials...");
11
}
12
13
function api_hook_handler() {
14
	return "hey hey";
15
}
16
17
function solr_api_init() {
18
19
	elgg_register_event_handler('delete', 'object', 'intercept_object_deletion'); 
20
21
	// Register the authentication handler
22
	//register_pam_handler('your_plugin_auth_handler');
23
	//elgg_register_plugin_hook_handler('rest:output', 'get_entity_list', 'api_hook_handler');
24
25
	// @TODO limit access to only the specified user agent string for more strict security
26
	// @TODO will need authentication mechanism for this
27
28
29
	//$method, $function, array $parameters = NULL, $description = "",
30
	//	$call_method = "GET", $require_api_auth = false, $require_user_auth = false
31
32
	elgg_ws_expose_function(
33
		'delete.updated_index_list',
34
		'delete_updated_index_list',
35
		[
36
			'guids' => [
37
				'type' => 'array',
38
				'required' => false,
39
				'description' => 'deletes records based on collection of entity guids'
40
			]
41
		],
42
		'deletes record that already updated the solr index',
43
		'POST',
44
		true,
45
		false
46
	);
47
48
49
	elgg_ws_expose_function(
50
        'get.entity_list',
51
        'get_entity_list',
52
        [
53
            'type' => [
54
                'type' => 'string',
55
                'required' => true,
56
                'description' => 'the type of entity in string format',
57
            ],
58
            'subtype' => [
59
                'type' => 'string',
60
                'required' => false,
61
                'description' => 'the subtype of entity in string format, not required',
62
            ],
63
			'offset' => [
64
                'type' => 'int',
65
                'required' => true,
66
                'description' => 'the subtype of entity in string format, not required',
67
            ],
68
69
        ],
70
        'retrieves all entities filtered by type [and subtype]',
71
        'GET',
72
        true,
73
        false
74
	);
75
76
77
	elgg_ws_expose_function(
78
        'get.user_list',
79
        'get_user_list',
80
        [
81
        	'offset' => [
82
        		'type' => 'int',
83
        		'required' => true,
84
        		'description' => 'paging mechanism'
85
        	]
86
        ],
87
        'retrieves a user list',
88
        'GET',
89
        true,
90
        false
91
	);
92
93
	//$method, $function, array $parameters = NULL, $description = "",
94
	//	$call_method = "GET", $require_api_auth = false, $require_user_auth = false
95
96
	elgg_ws_expose_function(
97
        'get.group_list',
98
        'get_group_list',
99
        [
100
        	'offset' => [
101
        		'type' => 'int',
102
        		'required' => true,
103
        		'description' => 'api loads 20 groups at a time'
104
        	]
105
        ],
106
        'retrieves a group list',
107
        'GET',
108
        true,
109
        false
110
	);
111
112
113
	elgg_ws_expose_function(
114
		'get.list_of_deleted_records',
115
		'get_list_of_deleted_records',
116
		null,
117
		'retrieves a list of deleted content',
118
		'GET',
119
		true,
120
		false
121
	);
122
}
123
124
125
function delete_updated_index_list($guids) {
126
127
	$ids = array();
128
	foreach ($guids as $guid) {
129
		// guid must be an integer
130
		if (is_numeric($guid)) 
131
			$ids[] = $guid;
132
		else
133
			return "there is an issue deleting a record in the database, please see function delete_updated_index_list()";
134
	}
135
136
	if (sizeof($ids) > 0) {
137
		$ids = implode(",", $ids);
138
		$query = "DELETE FROM deleted_object_tracker WHERE id IN ({$ids})";
139
		$result = delete_data($query);
140
	}
141
142
	$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...
143
	$arr[] = $ids;
144
145
	return $arr;
146
}
147
148
// this is not an api call, this is a hook that will initiate once user attempts deletion
149
function intercept_object_deletion($event, $type, $object) {
150
	$unixtime = time();
151
	error_log("deleting ... id: {$object->guid}  //  title: {$object->title} on " . time());
152
153
	
154
	$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})";
155
156
	// just in case for some reason, the user is able to delete the same thing twice
157
	try {
158
		$insert_record = insert_data($query);
159
	} catch (Exception $e) {
160
		error_log("error occurred: {$e}");
161
	}
162
163
	$query = "SELECT * FROM deleted_object_tracker";
164
	$deleted_records = get_data($query);
165
166
	foreach ($deleted_records as $deleted_record) {
167
		error_log("the id: " . $deleted_record->id);
168
	}
169
170
171
	return false;
172
}
173
174
175
// api calls with sample set of limit 15
176
function get_list_of_deleted_records() {
177
178
	$query = "SELECT * FROM deleted_object_tracker";
179
	$deleted_records = get_data($query);
180
181
	foreach ($deleted_records as $deleted_record) {
182
		error_log("the id: " . $deleted_record->id);
183
		//$datetime = new DateTime("@{$deleted_record->time_deleted}");
184
		//$datetime->setTimeZone(new DateTimeZone('America/New York'));
185
		$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...
186
			'guid' => $deleted_record->id,
187
			'time_deleted' => $deleted_record->time_deleted
188
		);
189
	}
190
	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...
191
}
192
193
194
function get_user_list($offset) {
195
196
	$users = elgg_get_entities(array(
197
		'type' => 'user',
198
		'limit' => 10,
199
		'offset' => $offset
200
	));
201
202
	foreach ($users as $user) {
203
204
		$name_array['en'] = $user->name;
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...
205
		$name_array['fr'] = $user->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...
206
207
		$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...
208
			'guid' => $user->getGUID(),
209
			'name' => $name_array,
210
			'username' => $user->username,
211
			'email' => $user->email,
212
			'type' => $user->getType(),
213
			'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created),
214
			'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created),
215
			'url' => $user->getURL()
216
		);
217
	}
218
    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...
219
}
220
221
function get_group_list($offset) {
222
223
	$groups = elgg_get_entities(array(
224
		'type' => 'group',
225
		'limit' => 10,
226
		'offset' => $offset
227
	));
228
229
	foreach ($groups as $group) {
230
231 View Code Duplication
		if (isJson($group->name)) {
232
			$name_array = json_decode($group->name, true);
233
			$name_array['en'] = str_replace('"', '\"', $name_array['en']);
234
			$name_array['fr'] = str_replace('"', '\"', $name_array['fr']);
235
		} else {
236
			$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...
237
			$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...
238
		}
239
240 View Code Duplication
		if (isJson($group->description)) {
241
			$description_array = json_decode($group->description, true);
242
			$description_array['en'] = str_replace('"', '\"', $description_array['en']);
243
			$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
244
		} else {
245
			$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...
246
			$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...
247
		}
248
249
		$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...
250
			'guid' => $group->getGUID(),
251
			'name' => $name_array,
252
			'description' => $description_array,
253
			'type' => $group->getType(),
254
			'access_id' => $group->access_id,
255
			'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created),
256
			'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated),
257
			'url' => $group->getURL()
258
		);
259
	}
260
    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...
261
}
262
263
264
function get_entity_list($type, $subtype, $offset) {
265
266
	$entities = elgg_get_entities(array(
267
		'type' => $type,
268
		'subtype' => $subtype,
269
		'limit' => 10,
270
		'offset' => $offset
271
	));
272
273
	foreach ($entities as $entity) {
274
275 View Code Duplication
		if (isJson($entity->title)) {
276
			$title_array = json_decode($entity->title, true);
277
			if (!isset($title_array['en']) || !isset($title_array['en'])) {
278
				$title_array['en'] = str_replace('"', '\"', $title_array);
279
				$title_array['fr'] = str_replace('"', '\"', $title_array);
280
			} else {
281
				$title_array['en'] = str_replace('"', '\"', $title_array['en']);
282
				$title_array['fr'] = str_replace('"', '\"', $title_array['fr']);
283
			}
284
		} else {
285
			$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...
286
			$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...
287
		}
288
289 View Code Duplication
		if (isJson($entity->description)) {
290
			$description_array = json_decode($entity->description, true);
291
			if (!isset($description_array['en']) || !isset($description_array['en'])) {
292
				$description_array['en'] = str_replace('"', '\"', $description_array);
293
				$description_array['fr'] = str_replace('"', '\"', $description_array);
294
			} else {
295
				$description_array['en'] = str_replace('"', '\"', $description_array['en']);
296
				$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
297
			}
298
		} else {
299
			$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...
300
			$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...
301
		}
302
303
		$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...
304
			'guid' => $entity->getGUID(), 
305
			'title' => $title_array,
306
			'description' => $description_array,
307
			'type' => $entity->getType(),
308
			'subtype' => $entity->getSubtype(),
309
			'access_id' => $entity->access_id,
310
			'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created),
311
			'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated),
312
			'url' => $entity->getURL()
313
		);
314
	}
315
316
	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...
317
}
318
319
 
320
321
function isJson($string) {
0 ignored issues
show
Best Practice introduced by
The function isJson() has been defined more than once; this definition is ignored, only the first definition in mod/cp_notifications/lib/functions.php (L148-151) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
322
	json_decode($string);
323
	return (json_last_error() == JSON_ERROR_NONE);
324
}
325
326