Completed
Pull Request — master (#33)
by Phecho
03:26
created

ProjectNamespaceController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 6
dl 29
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTeams() 0 6 1
A getTeam() 0 4 1
A postTeams() 14 14 2
A putTeam() 15 15 2
A deleteTeam() 0 6 1

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\ProjectNamespace\AddProjectNamespaceCommand;
15
use Gitamin\Commands\ProjectNamespace\RemoveProjectNamespaceCommand;
16
use Gitamin\Commands\ProjectNamespace\UpdateProjectNamespaceCommand;
17
use Gitamin\Models\ProjectNamespace;
18
use GrahamCampbell\Binput\Facades\Binput;
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 ProjectNamespaceController extends AbstractApiController
25
{
26
    use DispatchesJobs;
27
28
    /**
29
     * Get all teams.
30
     *
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     *
33
     * @return \Illuminate\Http\JsonResponse
34
     */
35
    public function getTeams(Request $request)
36
    {
37
        $teams = ProjectNamespace::paginate(Binput::get('per_page', 20));
38
39
        return $this->paginator($teams, $request);
40
    }
41
42
    /**
43
     * Get a single team.
44
     *
45
     * @param \Gitamin\Models\ProjectNamespace $team
46
     *
47
     * @return \Illuminate\Http\JsonResponse
48
     */
49
    public function getTeam(ProjectNamespace $team)
50
    {
51
        return $this->item($team);
52
    }
53
54
    /**
55
     * Create a new project team.
56
     *
57
     * @return \Illuminate\Http\JsonResponse
58
     */
59 View Code Duplication
    public function postTeams()
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...
60
    {
61
        try {
62
            $team = $this->dispatch(new AddProjectNamespaceCommand(
0 ignored issues
show
Bug introduced by
The call to AddProjectNamespaceCommand::__construct() misses some required arguments starting with $description.
Loading history...
63
                Binput::get('name'),
64
                Binput::get('path'),
65
                Binput::get('order', 0)
66
            ));
67
        } catch (QueryException $e) {
68
            throw new BadRequestHttpException();
69
        }
70
71
        return $this->item($team);
72
    }
73
74
    /**
75
     * Update an existing team.
76
     *
77
     * @param \Gitamin\Models\ProjectNamespace $team
78
     *
79
     * @return \Illuminate\Http\JsonResponse
80
     */
81 View Code Duplication
    public function putTeam(ProjectNamespace $team)
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...
82
    {
83
        try {
84
            $team = $this->dispatch(new UpdateProjectNamespaceCommand(
0 ignored issues
show
Bug introduced by
The call to UpdateProjectNamespaceCommand::__construct() misses a required argument $description.

This check looks for function calls that miss required arguments.

Loading history...
85
                $team,
0 ignored issues
show
Documentation introduced by
$team is of type object<Gitamin\Models\ProjectNamespace>, but the function expects a object<Gitamin\Models\ProjectNamepsace>.

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...
86
                Binput::get('name'),
87
                Binput::get('path'),
88
                Binput::get('order', 0)
89
            ));
90
        } catch (QueryException $e) {
91
            throw new BadRequestHttpException();
92
        }
93
94
        return $this->item($team);
95
    }
96
97
    /**
98
     * Delete an existing team.
99
     *
100
     * @param \Gitamin\Models\ProjectNamespace $team
101
     *
102
     * @return \Illuminate\Http\JsonResponse
103
     */
104
    public function deleteTeam(ProjectNamespace $team)
105
    {
106
        $this->dispatch(new RemoveProjectNamespaceCommand($team));
107
108
        return $this->noContent();
109
    }
110
}
111