Completed
Push — master ( 906f5a...709e36 )
by Phecho
03:29
created

ProjectController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 226
Duplicated Lines 19.47 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 11
c 7
b 2
f 0
lcom 1
cbo 6
dl 44
loc 226
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 42 1
A index() 11 11 1
A starred() 11 12 1
A showProject() 11 11 1
A showEditProject() 11 11 1
B updateProjectAction() 0 28 2
A showAddProject() 0 9 1
B createProjectAction() 0 27 2
A deleteProjectAction() 0 7 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\Dashboard;
13
14
use AltThree\Validator\ValidationException;
15
use Gitamin\Commands\Project\AddProjectCommand;
16
use Gitamin\Commands\Project\RemoveProjectCommand;
17
use Gitamin\Commands\Project\UpdateProjectCommand;
18
use Gitamin\Models\Project;
19
use Gitamin\Models\ProjectTeam;
20
use Gitamin\Models\Tag;
21
use GrahamCampbell\Binput\Facades\Binput;
22
use Illuminate\Foundation\Bus\DispatchesJobs;
23
use Illuminate\Routing\Controller;
24
use Illuminate\Support\Facades\Redirect;
25
use Illuminate\Support\Facades\View;
26
27
class ProjectController extends Controller
28
{
29
    use DispatchesJobs;
30
31
    /**
32
     * Array of sub-menu items.
33
     *
34
     * @var array
35
     */
36
    protected $subMenu = [];
37
38
    /**
39
     * Creates a new project controller instance.
40
     *
41
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
42
     */
43
    public function __construct()
44
    {
45
        $this->subMenu = [
46
            'yours' => [
47
                'title'  => trans('dashboard.projects.yours'),
48
                'url'    => route('dashboard.projects.index'),
49
                'icon'   => 'fa fa-edit',
50
                'active' => false,
51
            ],
52
            'starred' => [
53
                'title'  => trans('dashboard.projects.starred'),
54
                'url'    => route('dashboard.projects.starred'),
55
                'icon'   => 'fa fa-umbrella',
56
                'active' => false,
57
            ],
58
            'explore' => [
59
                'title'  => trans('dashboard.projects.explore'),
60
                'url'    => route('explore.index'),
61
                'icon'   => 'fa fa-eye',
62
                'active' => false,
63
            ],
64
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
            '<hr>'    => [],
66
            'teams'   => [
67
                'title'  => trans_choice('dashboard.teams.teams', 2),
68
                'url'    => route('dashboard.teams.index'),
69
                'icon'   => 'fa fa-folder',
70
                'active' => false,
71
            ],
72
            'labels' => [
73
                'title'  => trans_choice('dashboard.projects.labels.labels', 2),
74
                'url'    => route('dashboard.projects.index'),
75
                'icon'   => 'fa fa-tags',
76
                'active' => false,
77
            ],*/
78
        ];
79
80
        View::share([
81
            'sub_menu'  => $this->subMenu,
82
            'sub_title' => trans_choice('dashboard.projects.projects', 2),
83
        ]);
84
    }
85
86
    /**
87
     * Shows the projects view.
88
     *
89
     * @return \Illuminate\View\View
90
     */
91 View Code Duplication
    public function index()
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...
92
    {
93
        $projects = Project::orderBy('order')->orderBy('created_at')->get();
94
95
        $this->subMenu['yours']['active'] = true;
96
97
        return View::make('dashboard.projects.index')
98
            ->withPageTitle(trans_choice('dashboard.projects.projects', 2).' - '.trans('dashboard.dashboard'))
99
            ->withProjects($projects)
100
            ->withSubMenu($this->subMenu);
101
    }
102
103 View Code Duplication
    public function starred()
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...
104
    {
105
106
        $projects = Project::orderBy('order')->orderBy('created_at')->get();
107
108
        $this->subMenu['starred']['active'] = true;
109
110
        return View::make('dashboard.projects.index')
111
            ->withPageTitle(trans_choice('dashboard.projects.projects', 2).' - '.trans('dashboard.dashboard'))
112
            ->withProjects($projects)
113
            ->withSubMenu($this->subMenu);
114
    }
115
116
    /**
117
     * Shows the project view.
118
     *
119
     * @param \Gitamin\Models\Project $project
120
     *
121
     * @return \Illuminate\View\View
122
     */
123 View Code Duplication
    public function showProject(Project $project)
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...
124
    {
125
        $teams = ProjectTeam::all();
126
127
        $pageTitle = sprintf('"%s" - %s - %s', $project->name, trans('dashboard.projects.edit.title'), trans('dashboard.dashboard'));
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Gitamin\Models\Project>. 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...
128
129
        return View::make('dashboard.projects.show')
130
            ->withPageTitle($pageTitle)
131
            ->withProject($project)
132
            ->withTeams($teams);
133
    }
134
135
    /**
136
     * Shows the edit project view.
137
     *
138
     * @param \Gitamin\Models\Project $project
139
     *
140
     * @return \Illuminate\View\View
141
     */
142 View Code Duplication
    public function showEditProject(Project $project)
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...
143
    {
144
        $teams = ProjectTeam::all();
145
146
        $pageTitle = sprintf('"%s" - %s - %s', $project->name, trans('dashboard.projects.edit.title'), trans('dashboard.dashboard'));
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Gitamin\Models\Project>. 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...
147
148
        return View::make('dashboard.projects.edit')
149
            ->withPageTitle($pageTitle)
150
            ->withProject($project)
151
            ->withTeams($teams);
152
    }
153
154
    /**
155
     * Updates a project.
156
     *
157
     * @param \Gitamin\Models\Project $projects
0 ignored issues
show
Documentation introduced by
There is no parameter named $projects. Did you maybe mean $project?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
158
     *
159
     * @return \Illuminate\Http\RedirectResponse
160
     */
161
    public function updateProjectAction(Project $project)
162
    {
163
        $projectData = Binput::get('project');
164
        $tags = array_pull($projectData, 'tags');
165
166
        try {
167
            $projectData['project'] = $project;
168
            $project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData);
169
        } catch (ValidationException $e) {
170
            return Redirect::route('dashboard.projects.edit', ['id' => $project->id])
171
                ->withInput(Binput::all())
172
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure')))
173
                ->withErrors($e->getMessageBag());
174
        }
175
176
        // The project was added successfully, so now let's deal with the tags.
177
        $tags = preg_split('/ ?, ?/', $tags);
178
179
        // For every tag, do we need to create it?
180
        $projectTags = array_map(function ($taggable) use ($project) {
181
            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...
182
        }, $tags);
183
184
        $project->tags()->sync($projectTags);
185
186
        return Redirect::route('dashboard.projects.edit', ['id' => $project->id])
187
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success')));
188
    }
189
190
    /**
191
     * Shows the add project view.
192
     *
193
     * @return \Illuminate\View\View
194
     */
195
    public function showAddProject()
196
    {
197
        $teamId = (int) Binput::get('team_id');
198
199
        return View::make('dashboard.projects.add')
200
            ->withPageTitle(trans('dashboard.projects.add.title').' - '.trans('dashboard.dashboard'))
201
            ->withTeamId($teamId)
202
            ->withTeams(ProjectTeam::all());
203
    }
204
205
    /**
206
     * Creates a new project.
207
     *
208
     * @return \Illuminate\Http\RedirectResponse
209
     */
210
    public function createProjectAction()
211
    {
212
        $projectData = Binput::get('project');
213
        $tags = array_pull($projectData, 'tags');
214
215
        try {
216
            $project = $this->dispatchFromArray(AddProjectCommand::class, $projectData);
217
        } catch (ValidationException $e) {
218
            return Redirect::route('dashboard.projects.add')
219
                ->withInput(Binput::all())
220
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.add.failure')))
221
                ->withErrors($e->getMessageBag());
222
        }
223
224
        // The project was added successfully, so now let's deal with the tags.
225
        $tags = preg_split('/ ?, ?/', $tags);
226
227
        // For every tag, do we need to create it?
228
        $projectTags = array_map(function ($taggable) use ($project) {
229
            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...
230
        }, $tags);
231
232
        $project->tags()->sync($projectTags);
233
234
        return Redirect::route('dashboard.projects.index')
235
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.add.success')));
236
    }
237
238
    /**
239
     * Deletes a given project.
240
     *
241
     * @param \Gitamin\Models\Project $project
242
     *
243
     * @return \Illuminate\Http\RedirectResponse
244
     */
245
    public function deleteProjectAction(Project $project)
246
    {
247
        $this->dispatch(new RemoveProjectCommand($project));
248
249
        return Redirect::route('dashboard.projects.index')
250
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.delete.success')));
251
    }
252
}
253