Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — development ( 3f66c8...290203 )
by José
04:42
created

CampaignController::show()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 1
1
<?php
2
3
namespace DoeSangue\Http\Controllers\API;
4
5
use Illuminate\Http\Request;
6
use DoeSangue\Http\Requests\UpdateCampaignRequest;
7
use DoeSangue\Http\Requests\CreateCampaignRequest;
8
use DoeSangue\Http\Controllers\Controller;
9
use DoeSangue\Models\Donor;
10
use DoeSangue\Models\Campaign;
11
12
class CampaignController extends Controller
13
{
14
    public function __construct()
15
    {
16
        $this->middleware('jwt.auth', [ 'except' => [ 'index', 'show' ] ]);
17
    }
18
19
    public function index()
20
    {
21
        $campaigns = Campaign::all();
1 ignored issue
show
Unused Code introduced by
$campaigns is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
23
        return response()->json($data, 200);
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
24
    }
25
26
    public function store(CreateCampaignRequest $request)
27
    {
28
29
        $campaign = new Campaign();
30
        $campaign->title = $request[ 'title' ];
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31
        $campaign->expires = $request[ 'expires' ];
0 ignored issues
show
Documentation introduced by
The property expires does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
32
//        $campaign->user_id = $request[ 'user_id' ];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
        // use auth guard instead of $request['user_id'].
34
        $campaign->user_id = auth()->user()->id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
35
        $campaign->save();
36
37
        return response()->json(
38
            [
39
            'status_code' => 201,
40
            'message' => 'Campaign added!'
41
            ], 201
42
        );
43
    }
44
45
    public function show($id)
46
    {
47
        $campaign = Campaign::find($id);
48
49
        if (!$campaign) {
50
            return response()->json(
51
                ['data' => [
52
                'error_code' => '404',
53
                'error_message' => 'Campaign not found!'
54
                ]], 404
55
            );
56
        }
57
58
        return response()->json(
59
            [
60
            'title' => $campaign->title,
61
            'owner' => [
62
              'first_name' => $campaign->owner->first_name,
63
              'last_name' => $campaign->owner->last_name,
64
              'email' => $campaign->owner->email,
65
              'username' => $campaign->owner->username
66
            ],
67
            'dates' => [
68
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
69
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
70
            ]
71
            ], 200
72
        );
73
74
    }
75
76
    public function update(UpdateCampaignRequest $request, $id)
77
    {
78
        $campaign = Campaign::find($id);
79
        $campaign->title = $request[ 'title' ];
80
        $campaign->expires = $request[ 'expires' ];
81
82
        // Notify error in not found
83
        if (!$campaign) {
84
            return response()->json(
85
                [
86
                  'error_code' => '404',
87
                  'message' => 'Campaign not found!'
88
                ], 404
89
            );
90
        }
91
92 View Code Duplication
        if (auth()->user()->id != $campaign->user_id) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
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...
93
            return response()->json(
94
                [
95
                'message' => 'You haven\'t permission to update this entry'
96
                ]
97
            );
98
        }
99
100
        // If validation pass: update the entry.
101
        $campaign->save();
102
103
        return response()->json(
104
            [
105
            'title' => $campaign->title,
106
            'owner' => [
107
              'first_name' => $campaign->owner->first_name,
108
              'last_name' => $campaign->owner->last_name,
109
              'email' => $campaign->owner->email,
110
              'username' => $campaign->owner->username
111
            ],
112
            'dates' => [
113
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
114
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
115
            ]
116
            ], 200
117
        );
118
    }
119
120
    public function destroy($id)
121
    {
122
        $campaign = Campaign::find($id);
123
        // Notify error in not found
124
        if (!$campaign) {
125
            return response()->json(
126
                [
127
                  'error_code' => '404',
128
                  'message' => 'Campaign not found!'
129
                ], 404
130
            );
131
        }
132
133 View Code Duplication
        if (auth()->user()->id != $campaign->user_id) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
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...
134
            return response()->json(
135
                [
136
                'message' => 'You haven\'t permission to delete this entry'
137
                ]
138
            );
139
        }
140
141
        $campaign->delete();
142
143
        return response()->json(
144
            [
145
            'message' => 'Campaign deleted'
146
            ], 204
147
        );
148
    }
149
150
    /**
151
     * Get campaigns by specific donor/user
152
     *
153
     * @return Response
154
     */
155
    public function campaigns(Donor $id)
156
    {
157
158
        $donor = Donor::find($id);
159
        $campaigns = Campaigns::where('id_user', $donor->user_id)->get();
0 ignored issues
show
Unused Code introduced by
$campaigns is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
160
    }
161
}
162