TeamMemberController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 140
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A catalogue() 0 6 1
A assign() 0 14 1
A remove() 0 4 1
A search() 0 19 1
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Http\Requests\Team\AssignTeamMember;
6
use App\Http\Requests\Team\RemoveTeamMember;
7
use App\Member;
8
use App\TeamMember;
9
use App\Transformers\TeamMemberSearchTransformer;
10
use App\Transformers\TeamMemberTransformer;
11
use Illuminate\Support\Facades\Input;
12
13
/**
14
 * Class TeamMemberController
15
 * @package App\Http\Controllers\API
16
 */
17
class TeamMemberController extends Controller
18
{
19
    /**
20
     * @SWG\Get(
21
     *     tags={"Team"},
22
     *     path="/api/v1/teamMembers",
23
     *     description="Returns all members of specified team from tournament",
24
     *     operationId="catalogue",
25
     *     produces={"application/json"},
26
     *     @SWG\Parameter(
27
     *         description="Tournament-team id",
28
     *         in="query",
29
     *         name="tournamentTeamId",
30
     *         required=true,
31
     *         type="integer"
32
     *     ),
33
     *     @SWG\Response(
34
     *     response="200",
35
     *     description="Successfully get list of team members"
36
     *     )
37
     * )
38
     */
39
    public function catalogue()
40
    {
41
        $collection = TeamMember::with('Member')->where(['tournamentTeamId' => Input::get('tournamentTeamId')]);
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
42
43
        return $this->response->collection($collection->get(), new TeamMemberTransformer(), 'teamMembers');
44
    }
45
46
    /**
47
     * @SWG\Post(
48
     *     tags={"Team"},
49
     *     path="/api/v1/teamMembers",
50
     *     description="Create new member to specified team",
51
     *     operationId="assign",
52
     *     produces={"application/json"},
53
     *     @SWG\Parameter(
54
     *         description="Tournament-team id",
55
     *         in="query",
56
     *         name="teamMember[teamId]",
57
     *         required=true,
58
     *         type="integer"
59
     *     ),
60
     *     @SWG\Parameter(
61
     *         description="Member id",
62
     *         in="query",
63
     *         name="teamMember[memberId]",
64
     *         required=true,
65
     *         type="integer"
66
     *     ),
67
     *     @SWG\Response(
68
     *     response="200",
69
     *     description="Successfully add member to team"
70
     *     )
71
     * )
72
     * @param AssignTeamMember $request
73
     * @return array
74
     */
75
    public function assign(AssignTeamMember $request)
76
    {
77
        $input = $request->input('teamMember');
78
79
        $attributes = [
80
            'memberId' => array_get($input, 'memberId'),
0 ignored issues
show
Bug introduced by
It seems like $input defined by $request->input('teamMember') on line 77 can also be of type string; however, array_get() does only seem to accept object<ArrayAccess>|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
            'tournamentTeamId' => array_get($input, 'teamId'),
0 ignored issues
show
Bug introduced by
It seems like $input defined by $request->input('teamMember') on line 77 can also be of type string; however, array_get() does only seem to accept object<ArrayAccess>|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82
        ];
83
84
        TeamMember::create($attributes);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\TeamMember. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
        $teamMember = TeamMember::where($attributes)->get();
86
87
        return $this->response->collection($teamMember, new TeamMemberTransformer(), 'teamMembers');
88
    }
89
90
    /**
91
     * @SWG\Delete(
92
     *     tags={"Team"},
93
     *     path="/api/v1/teamMembers/{teamMemberId}",
94
     *     description="Remove member from specified team",
95
     *     operationId="remove",
96
     *     produces={"application/json"},
97
     *     @SWG\Parameter(
98
     *         description="Member id",
99
     *         in="path",
100
     *         name="teamMemberId",
101
     *         required=true,
102
     *         type="integer"
103
     *     ),
104
     *     @SWG\Response(
105
     *     response="200",
106
     *     description="Successfully remove member from team"
107
     *     )
108
     * )
109
     * @param $teamMemberId
110
     * @return
111
     */
112
    public function remove($teamMemberId)
113
    {
114
        return TeamMember::where(['memberId' => $teamMemberId])->delete();
115
    }
116
117
    /**
118
     * @SWG\Get(
119
     *     tags={"Team"},
120
     *     path="/api/v1/teamMembers/search",
121
     *     description="Returns members we search from database",
122
     *     operationId="search",
123
     *     produces={"application/json"},
124
     *     @SWG\Parameter(
125
     *         description="Tournament id",
126
     *         in="query",
127
     *         name="tournamentId",
128
     *         required=true,
129
     *         type="integer"
130
     *     ),
131
     *     @SWG\Response(
132
     *     response="200",
133
     *     description="Successfully get members we search"
134
     *     )
135
     * )
136
     */
137
    public function search()
138
    {
139
        $tournamentId = Input::get('tournamentId');
140
        $collection = Member::with(['teamMembers', 'tournamentTeams'])->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
141
142
        $collection = $collection->filter(function ($member) use ($tournamentId) {
143
            $team = $member->tournamentTeams->first(function ($tournamentTeam) use ($tournamentId) {
144
                return $tournamentTeam->tournamentId == $tournamentId;
145
            });
146
147
            return null === $team;
148
        });
149
150
        return $this->response->collection(
151
            $collection,
152
            new TeamMemberSearchTransformer(),
153
            'members'
154
        );
155
    }
156
}
157