Completed
Push — master ( ab60f2...c901a1 )
by Ricardo
07:00
created

ActivityController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 104
Duplicated Lines 20.19 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
lcom 0
cbo 2
dl 21
loc 104
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F index() 21 96 23

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 Fabrica\Http\Api;
4
5
use DB;
6
7
use Fabrica\Http\Api\Controller;
8
use Fabrica\Http\Requests;
9
use Fabrica\Project\Provider;
10
use Illuminate\Http\Request;
11
12
use Sentinel;
13
14
class ActivityController extends Controller
15
{
16
    /**
17
     * Display a listing of the resource.
18
     *
19
     * @return \Illuminate\Http\Response
20
     */
21
    public function index(Request $request, $project_key)
22
    {
23
        $cache_issues = [];
24
25
        $query = DB::collection('activity_' . $project_key);
26
27
        $category = $request->input('category');
28
        if (isset($category) && $category != 'all')
29
        {
30
            $query->where('event_key', 'like', '%' . $category);
31
        }
32
33
        $offset_id = $request->input('offset_id');
34
        if (isset($offset_id))
35
        {
36
            $query = $query->where('_id', '<', $offset_id);
37
        }
38
39
        $query->whereRaw([ 'issue_id' => [ '$exists' => 1 ] ]);
40
41
        $query->orderBy('_id', 'desc');
42
43
        $limit = $request->input('limit');
44
        if (!isset($limit))
45
        {
46
            $limit = 30;
47
        }
48
        $query->take(intval($limit));
49
50
        $avatars = [];
51
        $activities = $query->get();
52
        foreach ($activities as $key => $activity)
53
        {
54 View Code Duplication
            if (!array_key_exists($activity['user']['id'], $avatars))
0 ignored issues
show
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...
55
            {
56
                $user = Sentinel::findById($activity['user']['id']);
57
                $avatars[$activity['user']['id']] = isset($user->avatar) ? $user->avatar : '';
58
            }
59
            $activities[$key]['user']['avatar'] = $avatars[$activity['user']['id']];
60
61
            if ($activity['event_key'] == 'create_link' || $activity['event_key'] == 'del_link')
62
            {
63
                $activities[$key]['issue_link'] = [];
64 View Code Duplication
                if (isset($cache_issues[$activity['issue_id']]))
0 ignored issues
show
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...
65
                {
66
                    $issue = $cache_issues[$activity['issue_id']]; 
67
                }
68
                else
69
                {
70
                    $issue = DB::collection('issue_' . $project_key)->where('_id', $activity['issue_id'])->first();
71
                }
72
                $activities[$key]['issue_link'][ 'src'] = [ 
73
                    'id' => $activity['issue_id'], 
74
                    'no' => $issue['no'], 
75
                    'title' => isset($issue['title']) ? $issue['title'] : '', 
76
                    'state' => isset($issue['state']) ? $issue['state'] : '', 
77
                    'del_flg' => isset($issue['del_flg']) ? $issue['del_flg'] : 0 ];
78
79
                $activities[$key]['issue_link']['relation'] = $activity['data']['relation'];
80
81
                if (isset($cache_issues[$activity['data']['dest']]))
82
                {
83
                    $issue = $cache_issues[$activity['data']['dest']]; 
84
                }
85
                else
86
                {
87
                    $issue = DB::collection('issue_' . $project_key)->where('_id', $activity['data']['dest'])->first();
88
                }
89
                $activities[$key]['issue_link']['dest'] = [ 
90
                    'id' => $activity['data']['dest'], 
91
                    'no' => $issue['no'], 
92
                    'title' => isset($issue['title']) ? $issue['title'] : '', 
93
                    'state' => isset($issue['state']) ? $issue['state'] : '', 
94
                    'del_flg' => isset($issue['del_flg']) ? $issue['del_flg'] : 0 ];
95
            }
96
            else if (isset($activity['issue_id']))
97
            {
98 View Code Duplication
                if (isset($cache_issues[$activity['issue_id']]))
0 ignored issues
show
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...
99
                {
100
                    $issue = $cache_issues[$activity['issue_id']]; 
101
                }
102
                else
103
                {
104
                    $issue = DB::collection('issue_' . $project_key)->where('_id', $activity['issue_id'])->first();
105
                }
106
                $activities[$key]['issue'] = [ 
107
                    'id' => $activity['issue_id'], 
108
                    'no' => $issue['no'], 
109
                    'title' => isset($issue['title']) ? $issue['title'] : '', 
110
                    'state' => isset($issue['state']) ? $issue['state'] : '', 
111
                    'del_flg' => isset($issue['del_flg']) ? $issue['del_flg'] : 0 ];
112
                $cache_issues[$activity['issue_id']] = $issue;
113
            }
114
        }
115
        return Response()->json([ 'ecode' => 0, 'data' => parent::arrange($activities), 'options' => [ 'current_time' => time() ] ]);
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...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of index()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
116
    }
117
}
118