Completed
Pull Request — develop (#38)
by Neil
08:28
created

NotificationController::update()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 13
nc 12
nop 3
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use Dingo\Api\Http;
6
use Dingo\Api\Routing\Helpers;
7
use App\Notification;
8
use App\NotificationAttrib;
9
use Illuminate\Http\Request;
10
11
class NotificationController extends Controller
12
{
13
14
    use Helpers;
15
16
    public function __construct() {
17
    }
18
19
    /**
20
     * Display a listing of all notifications
21
     *
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index(Request $request, $type = null)
25
    {
26
        if ($type === 'archive')
27
        {
28
            $notifications = Notification::IsArchived($request)->get();
0 ignored issues
show
Bug introduced by
The method IsArchived() does not exist on App\Notification. Did you maybe mean scopeIsArchived()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
29
        }
30
        else {
31
            $notifications = Notification::IsUnread()->get();
0 ignored issues
show
Bug introduced by
The method IsUnread() does not exist on App\Notification. Did you maybe mean scopeIsUnread()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
        }
33
        return $notifications;
34
    }
35
36
    public function update(Request $request, $id, $action)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
39
        $notification = Notification::find($id);
40
        $enable = strpos($action, 'un') === false;
41
        if(!$enable) {
42
            $action = substr($action, 2);
43
        }
44
45
        if ($action == 'read') {
46
           $result = $notification->markRead($enable);
47
        }
48
        elseif ($action == 'sticky') {
49
           $result = $notification->markSticky(false);
50
        }
51
52
        if ($result === false) {
0 ignored issues
show
Bug introduced by
The variable $result 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...
53
            return $this->response->errorInternal();
54
        }
55
        else {
56
            return $this->response->array(array('statusText'=>'OK'));
57
        }
58
    }
59
60
}
61