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

CommentsController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 62.73 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 69
loc 110
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 6 1
A create() 0 24 1
A update() 35 35 3
A destroy() 34 34 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
24
    }
25
26
    public function create(Request $request, $campaign)
27
    {
28
29
        $user = JWTAuth::parseToken()->authenticate();
30
31
        //$campaign = Campaign::find($id);
32
        $camp = Campaign::find($campaign);
33
        $comment = new Comment();
34
        $comment->id = str_random();
35
        $comment->comment = $request[ 'comment' ];
36
        $comment->user_id = $user->id;
37
        $comment->campaign_id = $camp->id;
38
        // $comment->created_at = Carbon::now();
39
        $comment->save();
40
41
        //  return response()->json($comment);
42
        return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
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)
52
    {
53
        $comment = Comment::find($id);
54
55
        $user = JWTAuth::parseToken()->authenticate();
56
57
        if ($user->id !== $comment->user_id) {
58
            return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
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)
88
    {
89
        $comment = Comment::find($id);
90
91
        $user = JWTAuth::parseToken()->authenticate();
92
93
        if ($user->id !== $comment->user_id) {
94
            return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
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