Completed
Push — master ( 188ffb...31684b )
by Maxime
02:48
created

UserForm   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 78
Duplicated Lines 8.97 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 4
dl 7
loc 78
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 50 2
A getUpdateRules() 7 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 namespace Distilleries\Expendable\Http\Forms\User;
2
3
use Distilleries\Expendable\Helpers\StaticLabel;
4
use Distilleries\Expendable\Models\Role;
5
use Distilleries\FormBuilder\FormValidator;
6
7
class UserForm extends FormValidator {
8
9
    public static $rules = [
10
        'email'    => 'required|email|unique:users',
11
        'password' => 'required|min:8',
12
        'status'   => 'required|integer',
13
        'role_id'  => 'required|integer',
14
    ];
15
16
    public static $rules_update = [
17
        'id'      => 'required',
18
        'email'   => 'required|email|unique:users,email',
19
        'status'  => 'required|integer',
20
        'role_id' => 'required|integer',
21
    ];
22
23
    // ------------------------------------------------------------------------------------------------
24
25
26
    public function buildForm()
27
    {
28
29
        $this
30
            ->add($this->model->getKeyName(), 'hidden')
31
            ->add('email', 'email',
32
                [
33
                    'label'      => trans('expendable::form.email'),
34
                    'validation' => 'required,custom[email]',
35
                ]);
36
37
        $id = $this->model->getKey();
38
39
        if (!empty($id))
40
        {
41
            $this->add('change_password', 'checkbox', [
42
                'default_value' => 1,
43
                'label'         => trans('expendable::form.change_password_help'),
44
                'checked'       => false,
45
                'noInEditView'  => true
46
            ]);
47
        }
48
49
        $this->add('password', 'password',
50
            [
51
                'label'        => trans('expendable::form.password'),
52
                'attr'         => ['id' => 'password'],
53
                'validation'   => 'required',
54
                'noInEditView' => true
55
            ])
56
            ->add('password_match', 'password',
57
                [
58
                    'label'        => trans('expendable::form.repeat_password'),
59
                    'validation'   => 'required,equals[password]',
60
                    'noInEditView' => true
61
                ])
62
            ->add('status', 'choice', [
63
                'choices'     => StaticLabel::status(),
64
                'empty_value' => '-',
65
                'validation'  => 'required',
66
                'label'       => trans('expendable::form.status')
67
            ])
68
            ->add('role_id', 'choice', [
69
                'choices'     => Role::getChoice(),
70
                'empty_value' => '-',
71
                'validation'  => 'required',
72
                'label'       => trans('expendable::form.role')
73
            ])
74
            ->addDefaultActions();
75
    }
76
77 View Code Duplication
    protected function getUpdateRules()
78
    {
79
        $key                           = \Request::get($this->model->getKeyName());
80
        static::$rules_update['email'] = 'required|email|unique:users,email,' . $key;
81
82
        return parent::getUpdateRules();
83
    }
84
}