Completed
Push — master ( a0e7e0...d1cff9 )
by Costin
01:48 queued 12s
created

Form   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 117
Duplicated Lines 16.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 19
loc 117
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 10 1
A delete() 0 8 1
A setModel() 0 6 3
A setAction() 3 6 3
B setMethod() 3 25 7
A setFiles() 4 7 3
A setAutocomplete() 9 9 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
namespace SoareCostin\BladeFormComponents;
4
5
use SoareCostin\BladeFormComponents\Traits\GluesAttributes;
6
7
class Form
8
{
9
    use GluesAttributes;
10
11
    /** @var array */
12
    protected $params;
13
14
    /** @var string */
15
    public $action;
16
17
    /** @var string */
18
    public $method;
19
20
    /** @var string */
21
    public $httpMethod;
22
    
23
    /** @var bool */
24
    public $files;
25
26
    /** @var string */
27
    public $enctype;
28
29
    /** @var Illuminate\Database\Eloquent\Model */
30
    public $model = null;
31
32
    /** @var bool */
33
    public $autocomplete;
34
35
    /** @var array */
36
    public $attributesList = [
37
        'action', 'method', 'enctype', 'autocomplete'
38
    ];
39
    
40
    /**
41
     * Will be called when the form is opened.
42
     */
43
    public function setup(array $params)
44
    {
45
        $this->params = $params;
46
47
        $this->setModel();
48
        $this->setAction();
49
        $this->setMethod();
50
        $this->setFiles();
51
        $this->setAutocomplete();
52
    }
53
54
    /**
55
     * Will be called when the form is closed.
56
     */
57
    public function delete()
58
    {
59
        $this->parmas = null;
0 ignored issues
show
Bug introduced by
The property parmas does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
        $this->model = null;
61
        $this->action = null;
62
        $this->method = null;
63
        $this->files = null;
64
    }
65
66
    protected function setModel()
67
    {
68
        if (isset($this->params['model']) && ! empty($this->params['model'])) {
69
            $this->model = $this->params['model'];
70
        }
71
    }
72
73
    protected function setAction()
74
    {
75 View Code Duplication
        if (isset($this->params['url']) && ! empty($this->params['url'])) {
1 ignored issue
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...
76
            $this->action = $this->params['url'];
77
        }
78
    }
79
80
    protected function setMethod()
81
    {
82
        // Set default
83
        $this->method = $this->httpMethod = 'post';
84
85
        // Check method in params
86
87 View Code Duplication
        if (isset($this->params['method']) && ! empty($this->params['method'])) {
1 ignored issue
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...
88
            $this->method = $this->httpMethod = $this->params['method'];
89
        }
90
91
        if (in_array(strtoupper($this->method), ['GET', 'POST', 'PUT', 'DELETE'])) {
92
            $this->method = $this->httpMethod = strtoupper($this->method);
93
94
            // If the form type is POST but we have sent an existing model make it a PUT request
95
            if ($this->httpMethod == 'POST' && ! is_null($this->model)) {
96
                $this->httpMethod = 'PUT';
97
            }
98
99
            // The 'method' attribute is used to be used on the <form> element and can only by GET or POST
100
            if ($this->method !== 'GET') {
101
                $this->method = 'POST';
102
            }
103
        }
104
    }
105
106
    protected function setFiles()
107
    {
108 View Code Duplication
        if (isset($this->params['files']) && ! empty($this->params['files'])) {
1 ignored issue
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...
109
            $this->files = $this->params['files'];
110
            $this->enctype = 'multipart/form-data';
111
        }
112
    }
113
114 View Code Duplication
    protected function setAutocomplete()
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...
115
    {
116
        // Set default autocomplete option (true/false) from cofing file
117
        $this->autocomplete = config('blade-form-components.autocomplete');
118
119
        if (isset($this->params['autocomplete'])) {
120
            $this->autocomplete = $this->params['autocomplete'];
121
        }
122
    }
123
}
124