Completed
Push — api-test ( c6440b )
by
unknown
19:23
created

start.php ➔ get_entity_list()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 43
Code Lines 31

Duplication

Lines 32
Ratio 74.42 %

Importance

Changes 0
Metric Value
cc 4
eloc 31
nc 5
nop 2
dl 32
loc 43
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
elgg_register_event_handler('init','system','solr_api_init');
4
5
6
function solr_api_init() {
7
8
9
10
	elgg_register_event_handler('delete', 'object', 'intercept_object_deletion'); 
11
	//elgg_register_event_handler('create','object','cp_create_notification',900);
12
	// @TODO limit access to only the specified user agent string for more strict security
13
	// @TODO list all subtypes and types
14
15
	// http://192.168.245.130/gcconnex/services/api/rest/json/?method=get.entity_list&type=object&subtype=blog
16
	// http://192.168.245.130/gcconnex/services/api/rest/json/?method=get.user_list
17
	// http://192.168.245.130/gcconnex/services/api/rest/json/?method=get.group_list
18
 
19
20
	// @TODO will need authentication mechanism for this
21
	elgg_ws_expose_function(
22
		'delete.updated_index_list',
23
		'delete_updated_index_list',
24
		[
25
			'guids' => [
26
				'type' => 'array',
27
				'required' => false,
28
				'description' => 'deletes records based on collection of entity guids'
29
			]
30
		],
31
		'deletes record that already updated the solr index',
32
		'POST',
33
		false,
34
		false
35
	);
36
37
38
	// api expose function calls
39
	// parameters: method, function, array of parameters, description, call method/get or post, require api authentication, require user authentication
40
	elgg_ws_expose_function(
41
        'get.entity_list',
42
        'get_entity_list',
43
        [
44
            'type' => [
45
                    'type' => 'string',
46
                    'required' => true,
47
                    'description' => 'the type of entity in string format',
48
            ],
49
            'subtype' => [
50
                    'type' => 'string',
51
                    'required' => false,
52
                    'description' => 'the subtype of entity in string format, not required',
53
            ],
54
55
        ],
56
        'retrieves all entities filtered by type [and subtype]',
57
        'GET',
58
        false,
59
        false
60
	);
61
62
63
	elgg_ws_expose_function(
64
        'get.user_list',
65
        'get_user_list',
66
        null,
67
        'retrieves a user list',
68
        'GET',
69
        false,
70
        false
71
	);
72
73
	elgg_ws_expose_function(
74
        'get.group_list',
75
        'get_group_list',
76
        null,
77
        'retrieves a group list',
78
        'GET',
79
        false,
80
        false
81
	);
82
83
	elgg_ws_expose_function(
84
		'get.list_of_deleted_records',
85
		'get_list_of_deleted_records',
86
		null,
87
		'retrieves a list of deleted content',
88
		'GET',
89
		false,
90
		false
91
	);
92
}
93
 
94
95
function delete_updated_index_list($guids) {
96
97
	$ids = array();
98
	foreach ($guids as $guid) {
99
100
		error_log(".... guid array: " . $guid);
101
102
		if (is_numeric($guid)) {
103
			error_log("guid is an int");
104
			$ids[] = "id = {$guid}";
105
		}
106
107
	}
108
	error_log("the size is.. " . sizeof($ids));
109
	if (sizeof($ids) > 0) {
110
		$ids = implode(" OR ",$ids);
111
		$query = "DELETE FROM deleted_object_tracker WHERE {$ids}";
112
		error_log("query: " . $query);
113
		$result = delete_data($query);
114
	}
115
116
	return "ehhh";
117
}
118
119
120
function intercept_object_deletion($event, $type, $object) {
121
	$unixtime = time();
122
	error_log("deleting ... id: {$object->guid}  //  title: {$object->title} on " . time());
123
124
125
	$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})";
126
	
127
	try {
128
		$insert_record = insert_data($query);
129
	} catch (Exception $e) {
130
		error_log("error occurred: {$e}");
131
	}
132
133
	$query = "SELECT * FROM deleted_object_tracker";
134
	$deleted_records = get_data($query);
135
136
	foreach ($deleted_records as $deleted_record) {
137
		error_log("the id: " . $deleted_record->id);
138
	}
139
140
141
	return false;
142
}
143
144
function clear_deleted_object_tracker_table() {
145
146
}
147
148
// api calls with sample set of limit 15
149
function get_list_of_deleted_records() {
150
151
	$query = "SELECT * FROM deleted_object_tracker";
152
	$deleted_records = get_data($query);
153
154
	foreach ($deleted_records as $deleted_record) {
155
		error_log("the id: " . $deleted_record->id);
156
		//$datetime = new DateTime("@{$deleted_record->time_deleted}");
157
		//$datetime->setTimeZone(new DateTimeZone('America/New York'));
158
		$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...
159
			'guid' => $deleted_record->id,
160
			'time_deleted' => $deleted_record->time_deleted
161
		);
162
	}
163
164
	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...
165
}
166
167
function get_user_list() {
168
169
	$users = elgg_get_entities(array(
170
		'type' => 'user',
171
		'limit' => 15
172
	));
173
174
	foreach ($users as $user) {
175
176
		$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...
177
		$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...
178
179
		$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...
180
			'guid' => $user->getGUID(),
181
			'name' => $name_array,
182
			'username' => $user->username,
183
			'email' => $user->email,
184
			'type' => $user->getType(),
185
			'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created),
186
			'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created),
187
			'url' => $user->getURL()
188
		);
189
	}
190
	
191
    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...
192
}
193
194
function get_group_list() {
195
196
	$groups = elgg_get_entities(array(
197
		'type' => 'group',
198
		'limit' => 15
199
	));
200
201 View Code Duplication
	foreach ($groups as $group) {
202
203
		if (isJson($group->name)) {
204
			$name_array = json_decode($group->name, true);
205
			$name_array['en'] = str_replace('"', '\"', $name_array['en']);
206
			$name_array['fr'] = str_replace('"', '\"', $name_array['fr']);
207
		} else {
208
			$name_array['en'] = $group->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...
209
			$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...
210
		}
211
212
		if (isJson($group->description)) {
213
			$description_array = json_decode($group->description, true);
214
			$description_array['en'] = str_replace('"', '\"', $description_array['en']);
215
			$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
216
		} else {
217
			$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...
218
			$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...
219
		}
220
221
		$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...
222
			'guid' => $group->getGUID(),
223
			'name' => $name_array,
224
			'description' => $description_array,
225
			'type' => $group->getType(),
226
			'access_id' => $group->access_id,
227
			'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created),
228
			'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated),
229
			'url' => $group->getURL()
230
		);
231
	}
232
	
233
    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...
234
}
235
236
237
function get_entity_list($type, $subtype) {
238
239
	$entities = elgg_get_entities(array(
240
		'type' => $type,
241
		'subtype' => $subtype,
242
		'limit' => 15
243
	));
244
245 View Code Duplication
	foreach ($entities as $entity) {
246
247
		if (isJson($entity->title)) {
248
			$title_array = json_decode($entity->title, true);
249
			$title_array['en'] = str_replace('"', '\"', $title_array['en']);
250
			$title_array['fr'] = str_replace('"', '\"', $title_array['fr']);
251
		} else {
252
			$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...
253
			$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...
254
		}
255
256
		if (isJson($entity->description)) {
257
			$description_array = json_decode($entity->description, true);
258
			$description_array['en'] = str_replace('"', '\"', $description_array['en']);
259
			$description_array['fr'] = str_replace('"', '\"', $description_array['fr']);
260
		} else {
261
			$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...
262
			$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...
263
		}
264
265
		$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...
266
			'guid' => $entity->getGUID(), 
267
			'title' => $title_array,
268
			'description' => $description_array,
269
			'type' => $entity->getType(),
270
			'subtype' => $entity->getSubtype(),
271
			'access_id' => $entity->access_id,
272
			'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created),
273
			'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated),
274
			'url' => $entity->getURL()
275
		);
276
	}
277
	
278
    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...
279
}
280
281
282
283
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...
284
	json_decode($string);
285
	return (json_last_error() == JSON_ERROR_NONE);
286
}
287
288