Completed
Push — master ( 1f8bb9...0c53f4 )
by Phecho
06:42 queued 03:18
created

ProjectsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 148
Duplicated Lines 19.59 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 9
c 2
b 1
f 1
lcom 0
cbo 5
dl 29
loc 148
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 5 1
A newAction() 0 7 1
B createAction() 29 29 2
A showAction() 0 17 1
A editAction() 0 12 1
A updateAction() 0 23 2
A destroy() 0 4 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;
13
14
use Illuminate\Http\Request;
15
use Illuminate\Foundation\Bus\DispatchesJobs;
16
use Illuminate\Support\Facades\Redirect;
17
use Illuminate\Support\Facades\Auth;
18
use Illuminate\Support\Facades\View;
19
use GrahamCampbell\Binput\Facades\Binput;
20
use AltThree\Validator\ValidationException;
21
use Gitamin\Commands\Project\AddProjectCommand;
22
use Gitamin\Commands\Project\RemoveProjectCommand;
23
use Gitamin\Commands\Project\UpdateProjectCommand;
24
use Gitamin\Models\Project;
25
use Gitamin\Models\Owner;
26
use Gitamin\Models\Group;
27
use Gitamin\Models\Tag;
28
29
use Gitamin\Http\Controllers\Controller;
30
31
class ProjectsController extends Controller
32
{
33
    use DispatchesJobs;
34
    /**
35
     * Display a listing of the resource.
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function indexAction()
40
    {
41
        //
42
        echo "In projects controller";
43
    }
44
45
    /**
46
    * Show the form or adding a new resource.
47
    *
48
    * return \Illuminate\Http\Response
49
    */
50
    public function newAction()
51
    {
52
        return View::make('projects.new')
53
            ->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard'))
54
            ->withGroupId('')
55
            ->withGroups(Owner::Mine()->get());
56
    }
57
58
    /**
59
     * Show the form for creating a new resource.
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63 View Code Duplication
    public function createAction()
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...
64
    {
65
        //
66
        $projectData = Binput::get('project');
67
        $tags = array_pull($projectData, 'tags');
68
69
        try {
70
            $projectData['creator_id'] = Auth::user()->id;
71
            $project = $this->dispatchFromArray(AddProjectCommand::class, $projectData);
72
        } catch (ValidationException $e) {
73
            return Redirect::route('projects.new')
74
                ->withInput(Binput::all())
75
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.new.failure')))
76
                ->withErrors($e->getMessageBag());
77
        }
78
79
        // The project was added successfully, so now let's deal with the tags.
80
        $tags = preg_split('/ ?, ?/', $tags);
81
82
        // For every tag, do we need to create it?
83
        $projectTags = array_map(function ($taggable) use ($project) {
84
            return Tag::firstOrCreate(['name' => $taggable])->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Gitamin\Models\Tag>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
        }, $tags);
86
87
        $project->tags()->sync($projectTags);
88
89
        return Redirect::route('dashboard.projects.index')
90
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.new.success')));
91
    }
92
93
    /**
94
     * Display the specified resource.
95
     *
96
     * @param  string  $owner
97
     * @param  string  $project_path
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function showAction($owner, $project_path)
101
    {
102
        $project = Project::leftJoin('owners', function($join) {
103
            $join->on('projects.owner_id', '=', 'owners.id');
104
        })->where('projects.path', '=', $project_path)->where('owners.path', '=', $owner)->first(['projects.*']);
105
106
        return View::make('projects.show')
107
            ->withPageTitle($project->name)
108
            ->withActiveItem('project_show')
109
            ->withProject($project)
110
            ->withRepo('')
111
            ->withRepository('')
112
            ->withCurrentBranch('master')
113
            ->withBranches([])
114
            ->withParentPath('')
115
            ->withFiles([]);
116
    }
117
118
    /**
119
     * Show the form for editing the specified resource.
120
     *
121
     * @param  int  $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function editAction($namespace, $project_path)
125
    {
126
        $project = Project::leftJoin('namespaces', function($join) {
127
            $join->on('projects.namespace_id', '=', 'namespaces.id');
128
        })->where('projects.path', '=', $project_path)->where('namespaces.path', '=', $namespace)->first(['projects.*']);
129
        
130
        return View::make('projects.edit')
131
            ->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard'))
132
            ->withProject($project)
133
            ->withGroupId('')
134
            ->withGroups(Group::all());
135
    }
136
137
    /**
138
     * Update the specified resource in storage.
139
     *
140
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
141
     * @param  int  $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
142
     * @return \Illuminate\Http\Response
143
     */
144
    public function updateAction($namespace, $project_path)
0 ignored issues
show
Unused Code introduced by
The parameter $namespace 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...
Unused Code introduced by
The parameter $project_path 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...
145
    {
146
        $projectData = Binput::get('project');
147
        $tags = array_pull($projectData, 'tags');
148
        $project = Project::find($projectData['id']);
149
        $projectData['namespace_id'] = $project->namespace_id;
150
151
        try {
152
            $projectData['project'] = $project;
153
            $projectData['creator_id'] = Auth::user()->id;
154
            $project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData);
155
        } catch (ValidationException $e) {
156
            return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path])
157
                ->withInput(Binput::all())
158
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure')))
159
                ->withErrors($e->getMessageBag());
160
        }
161
        // The project was updated successfully, so now let's deal with the tags.
162
        $tags = preg_split('/ ?, ?/', $tags);
0 ignored issues
show
Unused Code introduced by
$tags is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
164
        return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path])
165
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success')));
166
    }
167
168
    /**
169
     * Remove the specified resource from storage.
170
     *
171
     * @param  int  $id
172
     * @return \Illuminate\Http\Response
173
     */
174
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
175
    {
176
        //
177
    }
178
}
179