Completed
Push — develop ( f06541...810a8c )
by Mohamed
11:09 queued 05:53
created

Project   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 10.48 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.14%

Importance

Changes 8
Bugs 2 Features 2
Metric Value
wmc 9
c 8
b 2
f 2
lcom 1
cbo 3
dl 13
loc 124
ccs 61
cts 70
cp 0.8714
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A actions() 0 19 2
B fields() 13 70 4
A rules() 0 9 1
A getRedirectUrl() 0 8 2

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 the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
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 Tinyissue\Form;
13
14
use Tinyissue\Model\Project as ProjectModel;
15
use Tinyissue\Model\Tag as TagModel;
16
17
/**
18
 * Project is a class to defines fields & rules for add/edit project form.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class Project extends FormAbstract
23
{
24
    /**
25
     * @return array
26
     */
27 3
    public function actions()
28
    {
29 3
        if ($this->isEditing()) {
30
            return [
31 2
                'submit' => 'update',
32
                'delete' => [
33 2
                    'type'         => 'danger_submit',
34 2
                    'label'        => trans('tinyissue.delete_something', ['name' => $this->getModel()->name]),
35 2
                    'class'        => 'delete-project',
36 2
                    'name'         => 'delete-project',
37 2
                    'data-message' => trans('tinyissue.delete_project_confirm'),
38 2
                ],
39 2
            ];
40
        }
41
42
        return [
43 1
            'submit' => 'create_project',
44 1
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50 3
    public function fields()
51
    {
52
        $fields = [
53
            'name' => [
54 3
                'type'  => 'text',
55 3
                'label' => 'name',
56 3
            ],
57
            'private' => [
58 3
                'type'    => 'select',
59 3
                'label'   => 'visibility',
60 3
                'options' => [ProjectModel::PRIVATE_YES => trans('tinyissue.private'), ProjectModel::PRIVATE_NO => trans('tinyissue.public')],
61 3
            ],
62
            'default_assignee' => [
63 3
                'type' => 'hidden',
64 3
                'id'   => 'default_assignee-id',
65 3
            ],
66 3
        ];
67
68
        // On create project can assign users
69
        // On edit project can change status or default assignee
70 3
        if (!$this->isEditing()) {
71 1
            $fields['user'] = [
72 1
                'type'        => 'selectUser',
73 1
                'label'       => 'assign_users',
74 1
                'id'          => 'add-user-project',
75 1
                'placeholder' => trans('tinyissue.assign_a_user'),
76
            ];
77 1
        } else {
78 2
            $fields['status'] = [
79 2
                'type'    => 'select',
80 2
                'label'   => 'status',
81 2
                'options' => [ProjectModel::STATUS_OPEN => trans('tinyissue.open'), ProjectModel::STATUS_ARCHIVED => trans('tinyissue.archived')],
82
            ];
83 2
            $fields['default_assignee'] = [
84 2
                'type'    => 'select',
85 2
                'label'   => 'default_assignee',
86 2
                'options' => [0 => ''] + $this->getModel()->users()->get()->lists('fullname', 'id')->all(),
87
            ];
88
        }
89
90 3
        $fields['kanban_board'] = [
91 3
            'type' => 'legend',
92
        ];
93
94 3
        $statusTags = (new TagModel())->getStatusTags()->tags()->get()->implode('fullname', ', ');
95 3 View Code Duplication
        if ($this->isEditing()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            $selectTags = $this->getModel()->kanbanTags()->get()->filter(function (TagModel $tag) {
97
                return !($tag->name == TagModel::STATUS_OPEN || $tag->name == TagModel::STATUS_CLOSED);
98 2
            })->map(function (TagModel $tag) {
99
                return [
100
                    'value'   => $tag->id,
101
                    'label'   => ($tag->fullname),
102
                    'bgcolor' => $tag->bgcolor,
103
                ];
104 2
            })->toJson();
105 2
        } else {
106 1
            $selectTags = (new TagModel())->tagsToJson(\Request::input('tags'));
107
        }
108 3
        $fields['columns'] = [
109 3
            'type'        => 'text',
110 3
            'label'       => 'columns',
111 3
            'placeholder' => trans('tinyissue.tags'),
112 3
            'multiple'    => true,
113 3
            'class'       => 'tagit',
114 3
            'help'        => trans('tinyissue.columns_help', ['status' => $statusTags]),
115 3
            'data_tokens' => htmlentities($selectTags, ENT_QUOTES),
116
        ];
117
118 3
        return $fields;
119
    }
120
121
    /**
122
     * @return array
123
     */
124 3
    public function rules()
125
    {
126
        $rules = [
127 3
            'name' => 'required|max:250',
128 3
            'user' => 'array|min:1',
129 3
        ];
130
131 3
        return $rules;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getRedirectUrl()
138
    {
139
        if ($this->isEditing()) {
140
            return $this->getModel()->to('edit');
141
        }
142
143
        return 'projects/new';
144
    }
145
}
146