| Conditions | 16 |
| Paths | 8 |
| Total Lines | 111 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 258 | public function sendPendingMessages($urlId = 0) |
||
| 259 | { |
||
| 260 | if (!$this->allowed()) { |
||
| 261 | return 0; |
||
| 262 | } |
||
| 263 | |||
| 264 | $messagesSent = 0; |
||
| 265 | $now = api_get_utc_datetime(); |
||
| 266 | $result = $this->get_all(); |
||
| 267 | |||
| 268 | foreach ($result as $result) { |
||
| 269 | if (empty($result['sent'])) { |
||
| 270 | if (!empty($result['date']) && $result['date'] < $now) { |
||
| 271 | $sessionId = $result['session_id']; |
||
| 272 | $sessionInfo = api_get_session_info($sessionId); |
||
| 273 | if (empty($sessionInfo)) { |
||
| 274 | continue; |
||
| 275 | } |
||
| 276 | $users = SessionManager::get_users_by_session( |
||
| 277 | $sessionId, |
||
| 278 | '0', |
||
| 279 | false, |
||
| 280 | $urlId |
||
| 281 | ); |
||
| 282 | |||
| 283 | if (empty($users)) { |
||
| 284 | continue; |
||
| 285 | } |
||
| 286 | |||
| 287 | self::update(['id' => $result['id'], 'sent' => 1]); |
||
| 288 | |||
| 289 | $subject = $result['subject']; |
||
| 290 | $message = $result['message']; |
||
| 291 | |||
| 292 | if ($users) { |
||
| 293 | foreach ($users as $user) { |
||
|
|
|||
| 294 | $userInfo = api_get_user_info($user['user_id']); |
||
| 295 | $courseList = SessionManager::getCoursesInSession($sessionId); |
||
| 296 | $courseInfo = []; |
||
| 297 | if (!empty($courseList)) { |
||
| 298 | $courseId = current($courseList); |
||
| 299 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 300 | } |
||
| 301 | |||
| 302 | $progress = ''; |
||
| 303 | if (!empty($sessionInfo) && !empty($courseInfo)) { |
||
| 304 | $progress = Tracking::get_avg_student_progress( |
||
| 305 | $user['user_id'], |
||
| 306 | $courseInfo['code'], |
||
| 307 | null, |
||
| 308 | $sessionId |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | |||
| 312 | if (is_numeric($progress)) { |
||
| 313 | $progress = $progress.'%'; |
||
| 314 | } else { |
||
| 315 | $progress = '0%'; |
||
| 316 | } |
||
| 317 | |||
| 318 | $startTime = api_get_local_time( |
||
| 319 | $sessionInfo['access_start_date'], |
||
| 320 | null, |
||
| 321 | null, |
||
| 322 | true |
||
| 323 | ); |
||
| 324 | $endTime = api_get_local_time( |
||
| 325 | $sessionInfo['access_end_date'], |
||
| 326 | null, |
||
| 327 | null, |
||
| 328 | true |
||
| 329 | ); |
||
| 330 | |||
| 331 | $generalCoach = ''; |
||
| 332 | $generalCoachEmail = ''; |
||
| 333 | if (!empty($sessionInfo['coach_id'])) { |
||
| 334 | $coachInfo = api_get_user_info($sessionInfo['coach_id']); |
||
| 335 | if (!empty($coachInfo)) { |
||
| 336 | $generalCoach = $coachInfo['complete_name']; |
||
| 337 | $generalCoachEmail = $coachInfo['email']; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | $tags = [ |
||
| 341 | '((session_name))' => $sessionInfo['name'], |
||
| 342 | '((session_start_date))' => $startTime, |
||
| 343 | '((general_coach))' => $generalCoach, |
||
| 344 | '((general_coach_email))' => $generalCoachEmail, |
||
| 345 | '((session_end_date))' => $endTime, |
||
| 346 | '((user_complete_name))' => $userInfo['complete_name'], |
||
| 347 | '((user_first_name))' => $userInfo['firstname'], |
||
| 348 | '((user_last_name))' => $userInfo['lastname'], |
||
| 349 | '((lp_progress))' => $progress, |
||
| 350 | ]; |
||
| 351 | |||
| 352 | $message = str_replace(array_keys($tags), $tags, $message); |
||
| 353 | |||
| 354 | MessageManager::send_message( |
||
| 355 | $userInfo['user_id'], |
||
| 356 | $subject, |
||
| 357 | $message |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | $messagesSent++; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | return $messagesSent; |
||
| 368 | } |
||
| 369 | |||
| 398 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.