Test Setup Failed
Push — master ( ec638a...cb9435 )
by Julito
51:10
created

WSCMCourse   C

Complexity

Total Complexity 72

Size/Duplication

Total Lines 588
Duplicated Lines 36.39 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 214
loc 588
rs 5.5667
c 0
b 0
f 0
wmc 72
lcom 2
cbo 7

17 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteCourseHelper() 10 19 2
A DeleteCourse() 0 18 3
B DeleteCourses() 21 22 4
C createCourseHelper() 29 40 7
B CreateCourse() 0 26 3
B CreateCourses() 27 28 4
F editCourseHelper() 0 62 15
B EditCourse() 0 25 3
B ListCourses() 27 39 4
B changeUserSubscription() 25 25 5
A SubscribeUserToCourse() 0 11 3
A UnsubscribeUserFromCourse() 0 11 3
A GetCourseDescriptions() 21 21 4
B EditCourseDescription() 38 38 6
A unreadMessage() 16 16 2
A get_message_data() 0 9 2
A nada() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WSCMCourse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WSCMCourse, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * @package chamilo.webservices
6
 */
7
require_once __DIR__.'/../inc/global.inc.php';
8
$libpath = api_get_path(LIBRARY_PATH);
9
require_once __DIR__.'/cm_webservice.php';
10
11
/**
12
 * Web services available for the Course module. This class extends the WS class
13
 */
14
class WSCMCourse extends WSCM
15
{
16
    /**
17
     * Deletes a course (helper method)
18
     *
19
     * @param string Course id field name
20
     * @param string Course id value
21
     * @return mixed True if the course was successfully deleted, WSError otherwise
22
     */
23 View Code Duplication
    protected function deleteCourseHelper(
24
        $course_id_field_name,
25
        $course_id_value
26
    ) {
27
        $course_id = $this->getCourseId(
28
            $course_id_field_name,
29
            $course_id_value
30
        );
31
        if ($course_id instanceof WSCMError) {
32
            return $course_id;
33
        } else {
34
            $course_code = CourseManager::get_course_code_from_course_id(
35
                $course_id
36
            );
37
            CourseManager::delete_course($course_code);
38
39
            return true;
40
        }
41
    }
42
43
    /**
44
     * Deletes a course
45
     *
46
     * @param string API secret key
47
     * @param string Course id field name
48
     * @param string Course id value
49
     */
50
    public function DeleteCourse(
51
        $secret_key,
52
        $course_id_field_name,
53
        $course_id_value
54
    ) {
55
        $verifKey = $this->verifyKey($secret_key);
56
        if ($verifKey instanceof WSError) {
57
            $this->handleError($verifKey);
58
        } else {
59
            $result = $this->deleteCourseHelper(
60
                $course_id_field_name,
61
                $course_id_value
62
            );
63
            if ($result instanceof WSError) {
64
                $this->handleError($result);
65
            }
66
        }
67
    }
68
69
    /**
70
     * Deletes multiple courses
71
     *
72
     * @param string API secret key
73
     * @param array Array of courses with elements of the form array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value')
74
     * @return array Array with elements like array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
75
     * than 0, an error occured
76
     */
77 View Code Duplication
    public function DeleteCourses($secret_key, $courses)
78
    {
79
        $verifKey = $this->verifyKey($secret_key);
80
        if ($verifKey instanceof WSError) {
81
            $this->handleError($verifKey);
82
        } else {
83
            $results = array();
84
            foreach ($courses as $course) {
85
                $result_tmp = array();
86
                $result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']);
87
                $result_tmp['course_id_value'] = $course['course_id_value'];
88
                if ($result_op instanceof WSCMError) {
89
                    // Return the error in the results
90
                    $result_tmp['result'] = $result_op->toArray();
91
                } else {
92
                    $result_tmp['result'] = $this->getSuccessfulResult();
93
                }
94
                $results[] = $result_tmp;
95
            }
96
            return $results;
97
        }
98
    }
99
100
    /**
101
     * Creates a course (helper method)
102
     *
103
     * @param string Title
104
     * @param string Category code
105
     * @param string Wanted code. If it's not defined, it will be generated automatically
106
     * @param string Tutor name
107
     * @param string Course admin user id field name
108
     * @param string Course admin user id value
109
     * @param string Course language
110
     * @param string Course id field name
111
     * @param string Course id value
112
     * @param array Course extra fields
113
     * @return mixed Generated id if creation was successful, WSError otherwise
114
     */
115 View Code Duplication
    protected function createCourseHelper(
116
        $title,
117
        $category_code,
118
        $wanted_code,
119
        $tutor_name,
120
        $course_admin_user_id_field_name,
121
        $course_admin_user_id_value,
122
        $language,
123
        $course_id_field_name,
124
        $course_id_value,
125
        $extras
126
    ) {
127
        // Add the original course id field name and value to the extra fields if needed
128
        $extras_associative = array();
129
        if ($course_id_field_name != "chamilo_course_id") {
130
            $extras_associative[$course_id_field_name] = $course_id_value;
131
        }
132
        foreach ($extras as $extra) {
133
            $extras_associative[$extra['field_name']] = $extra['field_value'];
134
        }
135
        $course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value);
136
        if ($course_admin_id instanceof WSError) {
137
            return $course_admin_id;
138
        }
139
        if ($wanted_code == '') {
140
            $wanted_code = CourseManager::generate_course_code($title);
141
        }
142
        $result = create_course($wanted_code, $title, $tutor_name, $category_code, $language, $course_admin_id, $this->_configuration['db_prefix'], 0);
143
        if (!$result) {
144
            return new WSError(202, 'There was an error creating the course');
145
        } else {
146
            // Update extra fields
147
            foreach ($extras_associative as $fname => $fvalue) {
148
                CourseManager::update_course_extra_field_value($result, $fname, $fvalue);
149
            }
150
            // Get course id
151
            $course_info = CourseManager::get_course_information($result);
0 ignored issues
show
Deprecated Code introduced by
The method CourseManager::get_course_information() has been deprecated with message: Use api_get_course_info() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
152
            return $course_info['real_id'];
153
        }
154
    }
155
156
    /**
157
     * Creates a course
158
     *
159
     * @param string API secret key
160
     * @param string Title
161
     * @param string Category code
162
     * @param string Wanted code. If it's not defined, it will be generated automatically
163
     * @param string Tutor name
164
     * @param string Course admin user id field name
165
     * @param string Course admin user id value
166
     * @param string Course language
167
     * @param string Course id field name
168
     * @param string Course id value
169
     * @param array Course extra fields
170
     * @return int Course id generated
171
     */
172
    public function CreateCourse(
173
        $secret_key,
174
        $title,
175
        $category_code,
176
        $wanted_code,
177
        $tutor_name,
178
        $course_admin_user_id_field_name,
179
        $course_admin_user_id_value,
180
        $language,
181
        $course_id_field_name,
182
        $course_id_value,
183
        $extras
184
    ) {
185
        // First, verify the secret key
186
        $verifKey = $this->verifyKey($secret_key);
187
        if ($verifKey instanceof WSError) {
188
            $this->handleError($verifKey);
189
        } else {
190
            $result = $this->createCourseHelper($title, $category_code, $wanted_code, $tutor_name, $course_admin_user_id_field_name, $course_admin_user_id_value, $language, $course_id_field_name, $course_id_value, $extras);
191
            if ($result instanceof WSError) {
192
                $this->handleError($result);
193
            } else {
194
                return $result;
195
            }
196
        }
197
    }
198
199
    /**
200
     * Create multiple courses
201
     *
202
     * @param string API secret key
203
     * @param array Courses to be created, with elements following the structure presented in CreateCourse
204
     * @return array Array with elements of the form array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
205
     */
206 View Code Duplication
    public function CreateCourses($secret_key, $courses)
207
    {
208
        // First, verify the secret key
209
        $verifKey = $this->verifyKey($secret_key);
210
        if ($verifKey instanceof WSCMError) {
211
            $this->handleError($verifKey);
212
        } else {
213
            $results = array();
214
            foreach ($courses as $course) {
215
                $result_tmp = array();
216
                //reinitialize variables just in case
217
                $title = $category_code = $wanted_code = $tutor_name = $course_admin_user_id_field_name = $course_admin_user_id_value = $language = $course_id_field_name = $course_id_value = $extras = null;
218
                extract($course);
219
                $result = $this->createCourseHelper($title, $category_code, $wanted_code, $tutor_name, $course_admin_user_id_field_name, $course_admin_user_id_value, $language, $course_id_field_name, $course_id_value, $extras);
220
                if ($result instanceof WSCMError) {
221
                    $result_tmp['result'] = $result->toArray();
222
                    $result_tmp['course_id_value'] = $course_id_value;
223
                    $result_tmp['course_id_generated'] = 0;
224
                } else {
225
                    $result_tmp['result'] = $this->getSuccessfulResult();
226
                    $result_tmp['course_id_value'] = $course_id_value;
227
                    $result_tmp['course_id_generated'] = $result;
228
                }
229
                $results[] = $result_tmp;
230
            }
231
            return $results;
232
        }
233
    }
234
235
    /**
236
     * Edits a course (helper method)
237
     *
238
     * @param string Course id field name
239
     * @param string Course id value
240
     * @param string Title
241
     * @param string Category code
242
     * @param string Department name
243
     * @param string Department url
244
     * @param string Course language
245
     * @param int Visibility
246
     * @param int Subscribe (0 = denied, 1 = allowed)
247
     * @param int Unsubscribe (0 = denied, 1 = allowed)
248
     * @param string Visual code
249
     * @param array Course extra fields
250
     * @return mixed True in case of success, WSError otherwise
251
     */
252
    protected function editCourseHelper(
253
        $course_id_field_name,
254
        $course_id_value,
255
        $title,
256
        $category_code,
257
        $department_name,
258
        $department_url,
259
        $language,
260
        $visibility,
261
        $subscribe,
262
        $unsubscribe,
263
        $visual_code,
264
        $extras
265
    ) {
266
        $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
267
        if ($course_id instanceof WSCMError) {
268
            return $course_id;
269
        } else {
270
            $attributes = array();
271
            if (!empty($title)) {
272
                $attributes['title'] = $title;
273
            }
274
            if (!empty($category_code)) {
275
                $attributes['category_code'] = $category_code;
276
            }
277
            if (!empty($department_name)) {
278
                $attributes['department_name'] = $department_name;
279
            }
280
            if (!empty($department_url)) {
281
                $attributes['department_url'] = $department_url;
282
            }
283
            if (!empty($language)) {
284
                $attributes['course_language'] = $language;
285
            }
286
            if ($visibility != '') {
287
                $attributes['visibility'] = (int) $visibility;
288
            }
289
            if ($subscribe != '') {
290
                $attributes['subscribe'] = (int) $subscribe;
291
            }
292
            if ($unsubscribe != '') {
293
                $attributes['unsubscribe'] = (int) $unsubscribe;
294
            }
295
            if (!empty($visual_code)) {
296
                $attributes['visual_code'] = $visual_code;
297
            }
298
            if (!empty($attributes)) {
299
                CourseManager::update_attributes($course_id, $attributes);
300
            }
301
            if (!empty($extras)) {
302
                $course_code = CourseManager::get_course_code_from_course_id($course_id);
303
                $extras_associative = array();
304
                foreach ($extras as $extra) {
305
                    $extras_associative[$extra['field_name']] = $extra['field_value'];
306
                }
307
                foreach ($extras_associative as $fname => $fvalue) {
308
                    CourseManager::update_extra_field_value($course_code, $fname, $fvalue);
309
                }
310
            }
311
            return true;
312
        }
313
    }
314
315
    /**
316
     * Edits a course
317
     *
318
     * @param string API secret key
319
     * @param string Course id field name
320
     * @param string Course id value
321
     * @param string Title
322
     * @param string Category code
323
     * @param string Department name
324
     * @param string Department url
325
     * @param string Course language
326
     * @param int Visibility
327
     * @param int Subscribe (0 = denied, 1 = allowed)
328
     * @param int Unsubscribe (0 = denied, 1 = allowed)
329
     * @param string Visual code
330
     * @param array Course extra fields
331
     */
332
    public function EditCourse(
333
        $secret_key,
334
        $course_id_field_name,
335
        $course_id_value,
336
        $title,
337
        $category_code,
338
        $department_name,
339
        $department_url,
340
        $language,
341
        $visibility,
342
        $subscribe,
343
        $unsubscribe,
344
        $visual_code,
345
        $extras
346
    ) {
347
        $verifKey = $this->verifyKey($secret_key);
348
        if ($verifKey instanceof WSCMError) {
349
            $this->handleError($verifKey);
350
        } else {
351
            $result = $this->editCourseHelper($course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras);
352
            if ($result instanceof WSCMError) {
353
                $this->handleError($result);
354
            }
355
        }
356
    }
357
358
    /**
359
     * List courses
360
     *
361
     * @param string API secret key
362
     * @param string Course id field name. Use "chamilo_course_id" to use internal id
363
     * @return array An array with elements of the form ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility',
364
     * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id')
365
     */
366
    public function ListCourses($secret_key, $course_id_field_name)
367
    {
368
        $verifKey = $this->verifyKey($secret_key);
369
        if ($verifKey instanceof WSError) {
370
            $this->handleError($verifKey);
371
        } else {
372
            $courses_result = array();
373
            $category_names = array();
374
375
            $courses = CourseManager::get_courses_list();
376 View Code Duplication
            foreach ($courses as $course) {
377
                $course_tmp = array();
378
                $course_tmp['id'] = $course['id'];
379
                $course_tmp['code'] = $course['code'];
380
                $course_tmp['title'] = $course['title'];
381
                $course_tmp['language'] = $course['course_language'];
382
                $course_tmp['visibility'] = $course['visibility'];
383
384
                // Determining category name
385
                if ($category_names[$course['category_code']]) {
386
                    $course_tmp['category_name'] = $category_names[$course['category_code']];
387
                } else {
388
                    $category = CourseManager::get_course_category($course['category_code']);
389
                    $category_names[$course['category_code']] = $category['name'];
390
                    $course_tmp['category_name'] = $category['name'];
391
                }
392
393
                // Determining number of students registered in course
394
                $user_list = CourseManager::get_user_list_from_course_code($course['code']);
395
                $course_tmp['number_students'] = count($user_list);
396
397
                // Determining external course id
398
                $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_id_field_name, $course['code']);
399
                $courses_result[] = $course_tmp;
400
            }
401
402
            return $courses_result;
403
        }
404
    }
405
406
    /**
407
     * Subscribe or unsubscribe user to a course (helper method)
408
     *
409
     * @param string Course id field name. Use "chamilo_course_id" to use internal id
410
     * @param string Course id value.
411
     * @param string User id field name. Use "chamilo_user_id" to use internal id
412
     * @param string User id value
413
     * @param int Set to 1 to subscribe, 0 to unsubscribe
414
     * @param int Status (STUDENT or TEACHER) Used for subscription only
415
     * @return mixed True if subscription or unsubscription was successful, false otherwise
416
     */
417 View Code Duplication
    protected function changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $state, $status = STUDENT) {
418
        $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
419
        if ($course_id instanceof WSError) {
420
            return $course_id;
421
        } else {
422
            $user_id = $this->getUserId($user_id_field_name, $user_id_value);
423
            if ($user_id instanceof WSError) {
424
                return $user_id;
425
            } else {
426
                $course_code = CourseManager::get_course_code_from_course_id($course_id);
0 ignored issues
show
Bug introduced by
It seems like $course_id defined by $this->getCourseId($cour...name, $course_id_value) on line 418 can also be of type object<WSCMError>; however, CourseManager::get_course_code_from_course_id() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
427
                if ($state == 0) {
428
                    // Unsubscribe user
429
                    CourseManager::unsubscribe_user($user_id, $course_code);
430
                    return true;
431
                } else {
432
                    // Subscribe user
433
                    if (CourseManager::subscribe_user($user_id, $course_code, $status)) {
0 ignored issues
show
Bug introduced by
It seems like $user_id defined by $this->getUserId($user_i...d_name, $user_id_value) on line 422 can also be of type object<WSCMError>; however, CourseManager::subscribe_user() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
434
                        return true;
435
                    } else {
436
                        return new WSError(203, 'An error occured subscribing to this course');
437
                    }
438
                }
439
            }
440
        }
441
    }
442
443
    /**
444
     * Subscribe user to a course
445
     *
446
     * @param string API secret key
447
     * @param string Course id field name. Use "chamilo_course_id" to use internal id
448
     * @param string Course id value.
449
     * @param string User id field name. Use "chamilo_user_id" to use internal id
450
     * @param string User id value
451
     * @param int Status (1 = Teacher, 5 = Student)
452
     */
453
    public function SubscribeUserToCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $status) {
454
        $verifKey = $this->verifyKey($secret_key);
455
        if ($verifKey instanceof WSError) {
456
            $this->handleError($verifKey);
457
        } else {
458
            $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 1, $status);
459
            if ($result instanceof WSError) {
460
                $this->handleError($result);
461
            }
462
        }
463
    }
464
465
    /**
466
     * Unsusbscribe user from course
467
     *
468
     * @param string API secret key
469
     * @param string Course id field name. Use "chamilo_course_id" to use internal id
470
     * @param string Course id value.
471
     * @param string User id field name. Use "chamilo_user_id" to use internal id
472
     * @param string User id value
473
     */
474
    public function UnsubscribeUserFromCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value) {
475
        $verifKey = $this->verifyKey($secret_key);
476
        if ($verifKey instanceof WSError) {
477
            $this->handleError($verifKey);
478
        } else {
479
            $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 0);
480
            if ($result instanceof WSError) {
481
                $this->handleError($result);
482
            }
483
        }
484
    }
485
486
    /**
487
     * Returns the descriptions of a course, along with their id
488
     *
489
     * @param string API secret key
490
     * @param string Course id field name
491
     * @param string Course id value
492
     * @return array Returns an array with elements of the form ('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content')
493
     */
494 View Code Duplication
    public function GetCourseDescriptions($secret_key, $course_id_field_name, $course_id_value) {
495
        $verifKey = $this->verifyKey($secret_key);
496
        if ($verifKey instanceof WSError) {
497
            $this->handleError($verifKey);
498
        } else {
499
            $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
500
            if ($course_id instanceof WSError) {
501
                return $course_id;
502
            } else {
503
                // Course exists, get its descriptions
504
                $descriptions = CourseDescription::get_descriptions($course_id);
0 ignored issues
show
Bug introduced by
It seems like $course_id defined by $this->getCourseId($cour...name, $course_id_value) on line 499 can also be of type object<WSCMError>; however, CourseDescription::get_descriptions() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
505
                $results = array();
506
                foreach ($descriptions as $description) {
507
                    $results[] = array('course_desc_id' => $description->get_description_type(),
508
                        'course_desc_title' => $description->get_title(),
509
                        'course_desc_content' => $description->get_content());
510
                }
511
                return $results;
512
            }
513
        }
514
    }
515
516
517
    /**
518
     * Edit course description
519
     *
520
     * @param string API secret key
521
     * @param string Course id field name
522
     * @param string Course id value
523
     * @param int Category id from course description
524
     * @param string Description title
525
     * @param string Course description content
526
     */
527 View Code Duplication
    public function EditCourseDescription($secret_key, $course_id_field_name, $course_id_value, $course_desc_id, $course_desc_title, $course_desc_content) {
528
        $verifKey = $this->verifyKey($secret_key);
529
        if ($verifKey instanceof WSError) {
530
            $this->handleError($verifKey);
531
        } else {
532
            $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
533
            if ($course_id instanceof WSError) {
534
                return $course_id;
535
            } else {
536
                // Create the new course description
537
                $cd = new CourseDescription();
538
                $cd->set_description_type($course_desc_id);
539
                $cd->set_title($course_desc_title);
540
                $cd->set_content($course_desc_content);
541
                $cd->set_session_id(0);
542
543
                // Get course info
544
                $course_info = CourseManager::get_course_information(
0 ignored issues
show
Deprecated Code introduced by
The method CourseManager::get_course_information() has been deprecated with message: Use api_get_course_info() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
545
                    CourseManager::get_course_code_from_course_id($course_id)
0 ignored issues
show
Bug introduced by
It seems like $course_id defined by $this->getCourseId($cour...name, $course_id_value) on line 532 can also be of type object<WSCMError>; however, CourseManager::get_course_code_from_course_id() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
546
                );
547
                // Check if this course description exists
548
                $descriptions = CourseDescription::get_descriptions($course_id);
0 ignored issues
show
Bug introduced by
It seems like $course_id defined by $this->getCourseId($cour...name, $course_id_value) on line 532 can also be of type object<WSCMError>; however, CourseDescription::get_descriptions() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
549
                $exists = false;
550
                foreach ($descriptions as $description) {
551
                    if ($description->get_description_type() == $course_desc_id) {
552
                        $exists = true;
553
                    }
554
                }
555
                $cd->set_course_id($course_info['real_id']);
556
                if (!$exists) {
557
                    $cd->set_progress(0);
558
                    $cd->insert();
559
                } else {
560
                    $cd->update();
561
                }
562
            }
563
        }
564
    }
565 View Code Duplication
    public function unreadMessage($username, $password)
566
    {
567
        if ($this->verifyUserPass($username, $password) == "valid")
568
        {
569
            $table_message = Database::get_main_table(TABLE_MESSAGE);
570
            $user_id = UserManager::get_user_id_from_username($username);
571
            $condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
572
573
            $sql_query = "SELECT COUNT(*) as number_messages FROM $table_message WHERE $condition_msg_status AND user_receiver_id=".$user_id;
574
575
            $sql_result = Database::query($sql_query);
576
            $result = Database::fetch_array($sql_result);
577
            return $result['number_messages'];
578
        }
579
        return "0";
580
    }
581
582
    public function get_message_data($username, $password)
583
    {
584
        if ($this->verifyUserPass($username, $password) == "valid")
585
        {
586
            $user_id = get_user_id_from_username($username);
587
588
        }
589
590
    }
591
592
    public function nada($username, $password)
593
    {
594
        if ($this->verifyUserPass($username, $password) == "valid")
595
            return $username.$password;
596
        return $username;
597
    }
598
599
600
601
}
602
603