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

Passed
Push — development ( 4b4ae2...928b46 )
by José
04:02
created

CampaignController::store()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 12
nc 1
nop 1
1
<?php
2
3
namespace DoeSangue\Http\Controllers\API;
4
5
use DoeSangue\Http\Requests\UpdateCampaignRequest;
6
use DoeSangue\Http\Requests\CreateCampaignRequest;
7
use DoeSangue\Http\Controllers\Controller;
8
use Illuminate\Support\Facades\Mail;
9
use DoeSangue\Models\Donor;
10
use DoeSangue\Models\Campaign;
11
use DoeSangue\Models\User;
12
use Carbon\Carbon;
13
use Tymon\JWTAuth\Facades\JWTAuth;
14
15
class CampaignController extends Controller
16
{
17
    public function __construct()
18
    {
19
        $this->middleware('jwt.auth', [ 'except' => [ 'index', 'show' ] ]);
20
    }
21
22
    public function index()
23
    {
24
        $campaigns = Campaign::all();
25
26
        return response()->json($campaigns, 200);
0 ignored issues
show
Bug introduced by
It seems like $campaigns defined by \DoeSangue\Models\Campaign::all() on line 24 can also be of type object<Illuminate\Database\Eloquent\Collection>; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
27
    }
28
29
    public function store(CreateCampaignRequest $request)
30
    {
31
32
        $user = JWTAuth::parseToken()->authenticate();
33
34
        $campaign = new Campaign();
35
        $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...
36
        $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...
37
//        $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...
38
        // use auth guard instead of $request['user_id'].
39
        $campaign->user_id = $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...
40
        $campaign->created_at = Carbon::now();
0 ignored issues
show
Documentation introduced by
The property created_at 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...
41
        $campaign->save();
42
43
        // Send mail to users about the new campaign.
44
 //       Mail::to($campaign->owner->email)->send(new CampaignPublished($campaign));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
45
46
        return response()->json(
47
            [
48
            'status_code' => 201,
49
            'message' => 'Campaign added!'
50
            ], 201
51
        );
52
53
    }
54
55
    public function show($id)
56
    {
57
        $campaign = Campaign::find($id);
58
59
        if (!$campaign) {
60
            return response()->json(
61
                [
62
                    'error_code' => 404,
63
                    'error_message' => 'Campaign not found!'
64
                ], 404
65
            );
66
        }
67
68
        return response()->json(
69
            [
70
            'title' => $campaign->title,
71
            'owner' => [
72
              'first_name' => $campaign->owner->first_name,
73
              'last_name' => $campaign->owner->last_name,
74
              'username' => $campaign->owner->username
75
            ],
76
            'dates' => [
77
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
78
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
79
            ]
80
            ], 200
81
        );
82
83
    }
84
85
    public function update(UpdateCampaignRequest $request, $id)
86
    {
87
        $campaign = Campaign::find($id);
88
89
        $user = JWTAuth::parseToken()->authenticate();
90
91
        if ($user->id !== $campaign->user_id) {
92
            return response()->json(
93
                [
94
                'message' => 'You haven\'t permission to update this entry'
95
                ], 401
96
            );
97
        }
98
99
        $campaign->title = $request[ 'title' ];
100
        $campaign->expires = $request[ 'expires' ];
101
        $campaign->updated_at = Carbon::now();
102
103
        // Notify error in not found
104
        if (!$campaign) {
105
            return response()->json(
106
                [
107
                  'error_code' => '404',
108
                  'message' => 'Campaign not found!'
109
                ], 404
110
            );
111
        }
112
113
        // If validation pass: update the entry.
114
        $campaign->save();
115
116
        return response()->json(
117
            [
118
            'title' => $campaign->title,
119
            'owner' => [
120
              'first_name' => $campaign->owner->first_name,
121
              'last_name' => $campaign->owner->last_name,
122
              'email' => $campaign->owner->email,
123
              'username' => $campaign->owner->username
124
            ],
125
            'dates' => [
126
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
127
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
128
            ]
129
            ], 200
130
        );
131
    }
132
133
    public function destroy($id)
134
    {
135
        $campaign = Campaign::find($id);
136
137
        $user = JWTAuth::parseToken()->authenticate();
138
139
        if ($user->id !== $campaign->user_id) {
140
            return response()->json(
141
                [
142
                'message' => 'You haven\'t permission to delete this entry'
143
                ], 401
144
            );
145
        }
146
147
        // Notify error in not found
148
        if (!$campaign) {
149
            return response()->json(
150
                [
151
                  'error_code' => '404',
152
                  'message' => 'Campaign not found!'
153
                ], 404
154
            );
155
        }
156
157
        $campaign->delete();
158
159
        return response()->json(
160
            [
161
            'message' => 'Campaign deleted'
162
            ], 200
163
        );
164
    }
165
166
    /**
167
     * Get campaigns by specific donor/user
168
     *
169
     * @return Response
170
     */
171
    public function campaigns(Donor $id)
172
    {
173
174
        $donor = Donor::find($id);
175
        $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...
176
    }
177
}
178