ConfigController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 19.15 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 9
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 23 5
A getPermissions() 8 8 5

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 Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Event;
7
8
use Fabrica\Http\Requests;
9
use Fabrica\Http\Api\Controller;
10
11
use DB;
12
use Fabrica\Project\Provider;
13
use Fabrica\Acl\Eloquent\RolePermissions;
14
15
class ConfigController extends Controller
16
{
17
    /**
18
     * Display a listing of the resource.
19
     *
20
     * @return \Illuminate\Http\Response
21
     */
22
    public function index($project_key)
23
    {
24
        $new_types = [];
25
        $types = Provider::getTypeList($project_key); 
26
        foreach ($types as $type)
27
        {
28
            if (isset($type->disabled) && $type->disabled) {
29
                continue;
30
            }
31
            $type->screen = $type->screen;
32
            $type->workflow = $type->workflow;
33
            $new_types[] = $type;
34
        }
35
36
        $priorities = Provider::getPriorityList($project_key);
37
        $roles = Provider::getRoleList($project_key);
38
        foreach($roles as $role)
39
        {
40
            $role->permissions = $this->getPermissions($project_key, $role->id);
41
        }
42
43
        return response()->json([ 'ecode' => 0, 'data' => [ 'types' => $new_types, 'roles' => $roles, 'priorities' => $priorities ] ]);
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...
44
    }
45
46
    /**
47
     * get permissions by project_key and roleid.
48
     *
49
     * @param  string $project_key
50
     * @param  string $role_id
51
     * @return array
52
     */
53 View Code Duplication
    public function getPermissions($project_key, $role_id)
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...
54
    {
55
        $rp = RolePermissions::where([ 'project_key' => $project_key, 'role_id' => $role_id ])->first();
56
        if (!$rp && $project_key !== '$_sys_$') {
57
            $rp = RolePermissions::where([ 'project_key' => '$_sys_$', 'role_id' => $role_id ])->first();
58
        }
59
        return $rp && isset($rp->permissions) ? $rp->permissions : [];
60
    }
61
}
62