NotificationController::update()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 12
nop 3
dl 0
loc 20
ccs 0
cts 12
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\Api\Transformers\NotificationTransformer;
6
use App\Models\Notification;
7
use Dingo\Api\Routing\Helpers;
8
use Illuminate\Http\Request;
9
10
class NotificationController extends Controller
11
{
12
13
    use Helpers;
14
15
    /**
16
     * Display a listing of all notifications
17
     *
18
     * @param Request $request
19
     * @param null $type archive or null (all)
20
     * @return \Illuminate\Http\Response
21
     */
22 1
    public function index(Request $request, $type = null)
23
    {
24 1
        if ($type === 'archive') {
25 1
            $notifications = Notification::isArchived($request->user())->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Query\Builder, but not in App\Models\Notification.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
26
        } else {
27 1
            $notifications = Notification::isUnread()->get();
28
        }
29 1
        return $this->response->collection($notifications, NotificationTransformer::class);
30
    }
31
32
    public function update(Request $request, $id, $action)
33
    {
34
        $notification = Notification::find($id);
35
        $enable = strpos($action, 'un') === false;
36
        if (!$enable) {
37
            $action = substr($action, 2);
38
        }
39
40
        if ($action == 'read') {
41
            $result = $notification->markRead($enable);
42
        } elseif ($action == 'sticky') {
43
            $result = $notification->markSticky(false);
44
        }
45
46
        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...
47
            return $this->response->errorInternal();
48
        } else {
49
            return $this->response->array(array('statusText' => 'OK'));
50
        }
51
    }
52
53
    /**
54
     * Create a new notification
55
     *
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function create(Request $request)
59
    {
60
        $notification = new Notification;
61
        $notification->title = $request->title;
62
        $notification->body = $request->body;
63
        $notification->checksum = hash('sha512', $request->user()->user_id.'.LOCAL.'.$request->title);
64
        $notification->source = $request->user()->user_id;
65
        if ($notification->save()) {
66
            return $this->response->array(array('statusText' => 'OK'));
67
        } else {
68
            return $this->response->errorInternal();
69
        }
70
    }
71
}
72