Completed
Push — settings ( bea8d4...96499a )
by Tony
05:40
created

NotificationController::update()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 13

Duplication

Lines 6
Ratio 27.27 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 6
loc 22
rs 8.6737
cc 5
eloc 13
nc 12
nop 3
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\Api\Transformers\NotificationTransformer;
6
use App\Models\Notification;
7
use Dingo\Api\Http;
8
use Dingo\Api\Routing\Helpers;
9
use Illuminate\Http\Request;
10
11
class NotificationController extends Controller
12
{
13
14
    use Helpers;
15
16
    /**
17
     * Display a listing of all notifications
18
     *
19
     * @param Request $request
20
     * @param null $type archive or null (all)
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function index(Request $request, $type = null)
24
    {
25
        if ($type === 'archive')
26
        {
27
            $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...
28
        }
29
        else {
30
            $notifications = Notification::isUnread()->get();
31
        }
32
        return $this->response->collection($notifications, NotificationTransformer::class);
33
    }
34
35
    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...
36
    {
37
        $notification = Notification::find($id);
38
        $enable = strpos($action, 'un') === false;
39
        if (!$enable) {
40
            $action = substr($action, 2);
41
        }
42
43
        if ($action == 'read') {
44
            $result = $notification->markRead($enable);
45
        }
46
        elseif ($action == 'sticky') {
47
            $result = $notification->markSticky(false);
48
        }
49
50 View Code Duplication
        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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            return $this->response->errorInternal();
52
        }
53
        else {
54
            return $this->response->array(array('statusText' => 'OK'));
55
        }
56
    }
57
58
    /**
59
     * Create a new notification
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function create(Request $request)
64
    {
65
        $notification = new Notification;
66
        $notification->title = $request->title;
1 ignored issue
show
Bug introduced by
The property title does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67
        $notification->body = $request->body;
1 ignored issue
show
Bug introduced by
The property body does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68
        $notification->checksum = hash('sha512', $request->user()->user_id.'.LOCAL.'.$request->title);
69
        $notification->source = $request->user()->user_id;
70 View Code Duplication
        if ($notification->save()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            return $this->response->array(array('statusText' => 'OK'));
72
        }
73
        else {
74
            return $this->response->errorInternal();
75
        }
76
    }
77
78
}
79