CommentController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 14.44 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
c 4
b 1
f 0
lcom 1
cbo 7
dl 13
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getComments() 0 6 1
A getComment() 0 4 1
A putComment() 13 13 2
A deleteComment() 0 6 1
A postComments() 0 16 2

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
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Controllers\Api;
13
14
use Gitamin\Commands\Comment\AddCommentCommand;
15
use Gitamin\Commands\Comment\RemoveCommentCommand;
16
use Gitamin\Commands\Comment\UpdateCommentCommand;
17
use Gitamin\Models\Comment;
18
use Illuminate\Contracts\Auth\Guard;
19
use Illuminate\Database\QueryException;
20
use Illuminate\Foundation\Bus\DispatchesJobs;
21
use Illuminate\Http\Request;
22
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
23
24
class CommentController extends AbstractApiController
25
{
26
    use DispatchesJobs;
27
28
    /**
29
     * Get all comments.
30
     *
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     * @param \Illuminate\Contracts\Auth\Guard          $auth
33
     *
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36
    public function getComments(Request $request, Guard $auth)
0 ignored issues
show
Unused Code introduced by
The parameter $auth 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...
37
    {
38
        $comments = Comment::paginate($request->input('per_page', 20));
39
40
        return $this->paginator($comments, $request);
41
    }
42
43
    /**
44
     * Get a single comment.
45
     *
46
     * @param \Gitamin\Models\Comment $comment
47
     *
48
     * @return \Illuminate\Http\JsonResponse
49
     */
50
    public function getComment(Comment $comment)
51
    {
52
        return $this->item($comment);
53
    }
54
55
    /**
56
     * Create a new comment.
57
     *
58
     * @param \Illuminate\Contracts\Auth\Guard $auth
59
     *
60
     * @return \Illuminate\Http\JsonResponse
61
     */
62
    public function postComments(Request $request, Guard $auth)
0 ignored issues
show
Unused Code introduced by
The parameter $auth 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...
63
    {
64
        try {
65
            $comment = $this->dispatch(new AddCommentCommand(
66
                $request->input('message'),
0 ignored issues
show
Bug introduced by
It seems like $request->input('message') targeting Illuminate\Http\Request::input() can also be of type array; however, Gitamin\Commands\Comment...tCommand::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
                $request->input('target_type'),
0 ignored issues
show
Bug introduced by
It seems like $request->input('target_type') targeting Illuminate\Http\Request::input() can also be of type array; however, Gitamin\Commands\Comment...tCommand::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
68
                $request->input('target_id'),
0 ignored issues
show
Documentation introduced by
$request->input('target_id') is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
                $request->input('author_id'),
0 ignored issues
show
Documentation introduced by
$request->input('author_id') is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
                $request->input('project_id')
0 ignored issues
show
Documentation introduced by
$request->input('project_id') is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
            ));
72
        } catch (QueryException $e) {
73
            throw new BadRequestHttpException();
74
        }
75
76
        return $this->item($comment);
77
    }
78
79
    /**
80
     * Update an existing comment.
81
     *
82
     * @param \Gitamin\Models\Inicdent $comment
83
     *
84
     * @return \Illuminate\Http\JsonResponse
85
     */
86 View Code Duplication
    public function putComment(Request $request, Comment $comment)
0 ignored issues
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...
87
    {
88
        try {
89
            $comment = $this->dispatch(new UpdateCommentCommand(
90
                $comment,
91
                $request->input('message')
0 ignored issues
show
Bug introduced by
It seems like $request->input('message') targeting Illuminate\Http\Request::input() can also be of type array; however, Gitamin\Commands\Comment...tCommand::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
            ));
93
        } catch (QueryException $e) {
94
            throw new BadRequestHttpException();
95
        }
96
97
        return $this->item($comment);
98
    }
99
100
    /**
101
     * Delete an existing comment.
102
     *
103
     * @param \Gitamin\Models\Comment $comment
104
     *
105
     * @return \Illuminate\Http\JsonResponse
106
     */
107
    public function deleteComment(Comment $comment)
108
    {
109
        $this->dispatch(new RemoveCommentCommand($comment));
110
111
        return $this->noContent();
112
    }
113
}
114