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

Test Failed
Push — development ( 3add70...a51bef )
by José
06:20
created

CommentsController::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
3
namespace DoeSangue\Http\Controllers\API\V1;
4
5
use Illuminate\Http\Request;
6
use DoeSangue\Http\Controllers\Controller;
7
use DoeSangue\Models\Comment;
8
use DoeSangue\Models\Campaign;
9
use Tymon\JWTAuth\Facades\JWTAuth;
10
use Carbon\Carbon;
11
12
class CommentsController extends Controller
13
{
14
    public function __construct()
15
    {
16
        $this->middleware('jwt.auth', [ 'except' => [ 'index', 'show' ] ]);
17
    }
18
19
    public function index($campaign)
20
    {
21
        $comments = Comment::where('campaign_id', $campaign)->get();
22
23
        return response()->json($comments, 200);
24
    }
25
26
    public function create(Request $request, $campaign)
27
    {
28
29
        $user = JWTAuth::parseToken()->authenticate();
30
31
        //$campaign = Campaign::find($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
32
        $camp = Campaign::find($campaign);
33
        $comment = new Comment();
34
        $comment->id = str_random();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<DoeSangue\Models\Comment>. 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...
35
        $comment->comment = $request['comment'];
0 ignored issues
show
Documentation introduced by
The property comment does not exist on object<DoeSangue\Models\Comment>. 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
        $comment->user_id = $user->id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<DoeSangue\Models\Comment>. 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
        $comment->campaign_id = $camp->id;
0 ignored issues
show
Documentation introduced by
The property campaign_id does not exist on object<DoeSangue\Models\Comment>. 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...
38
        // $comment->created_at = Carbon::now();
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
39
        $comment->save();
40
41
        //  return response()->json($comment);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
42
        return response()->json(
43
            [
44
            'status_code' => 201,
45
            'message' => 'Comment added!'
46
            ], 201
47
        );
48
49
    }
50
51 View Code Duplication
    public function update($id, Request $request)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
52
    {
53
        $comment = Comment::find($id);
54
55
        $user = JWTAuth::parseToken()->authenticate();
56
57
        if ($user->id !== $comment->user_id) {
58
            return response()->json(
59
                [
60
                  'status_code' => 401,
61
                  'message' => 'You haven\'t permission to update this entry'
62
                ], 401
63
            );
64
        }
65
66
        $comment->comment = $request['comment'];
67
68
        if (! $comment) {
69
            return response()->json(
70
                [
71
                    'error_code' => 404,
72
                    'error_message' => 'Comment not found!'
73
                ], 404
74
            );
75
        }
76
77
        $comment->save();
78
79
        return response()->json(
80
            [
81
            'status_code' => 200,
82
            'message' => 'Comment updated!'
83
            ], 200
84
        );
85
    }
86
87 View Code Duplication
    public function destroy($id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89
        $comment = Comment::find($id);
90
91
        $user = JWTAuth::parseToken()->authenticate();
92
93
        if ($user->id !== $comment->user_id) {
94
            return response()->json(
95
                [
96
                  'status_code' => 401,
97
                  'message' => 'You haven\'t permission to update this entry'
98
                ], 401
99
            );
100
        }
101
102
         // Notify error in not found
103
        if (!$comment) {
104
            return response()->json(
105
                [
106
                  'error_code' => 404,
107
                  'message' => 'Comment not found!'
108
                ], 404
109
            );
110
        }
111
112
        $Comment->delete();
0 ignored issues
show
Bug introduced by
The variable $Comment does not exist. Did you mean $comment?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
113
114
        return response()->json(
115
            [
116
              'status_code' => 204,
117
              'message' => 'Comment deleted'
118
            ], 204
119
        );
120
    }
121
}
122