Field   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 210
Duplicated Lines 5.71 %

Coupling/Cohesion

Components 4
Dependencies 2

Test Coverage

Coverage 28.42%

Importance

Changes 0
Metric Value
dl 12
loc 210
ccs 27
cts 95
cp 0.2842
rs 9.3999
c 0
b 0
f 0
wmc 33
lcom 4
cbo 2

20 Methods

Rating   Name   Duplication   Size   Complexity  
A hideValue() 0 8 2
A __construct() 0 13 1
A __clone() 0 8 1
A getName() 0 4 1
A setName() 0 4 1
A presentation() 12 12 2
A type() 0 4 1
A setValidationRules() 0 4 1
A getValidationRules() 0 10 4
A getValidationRulesArray() 0 4 1
A setFunctions() 0 8 2
A applyFunctions() 0 12 3
A setValue() 0 8 2
A getValue() 0 4 1
A setOptions() 0 6 1
A getOptions() 0 4 1
A getFormField() 0 10 3
A saveIfEmpty() 0 8 2
A noValidate() 0 8 2
A setFormElementAttributes() 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
namespace Anavel\Crud\Abstractor\Eloquent;
4
5
use Anavel\Crud\Contracts\Abstractor\Field as FieldAbstractorContract;
6
use Doctrine\DBAL\Schema\Column;
7
use FormManager\Fields\Field as FormManagerField;
8
use Request;
9
10
class Field implements FieldAbstractorContract
11
{
12
    /**
13
     * @var Column
14
     */
15
    protected $dbal;
16
    /**
17
     * @var FormManagerField
18
     */
19
    protected $formField;
20
    protected $name;
21
    protected $value;
22
    protected $presentation;
23
    protected $validationRules;
24
    protected $functions;
25
    protected $options;
26
    protected $hideValue;
27
    protected $saveIfEmpty;
28
    protected $noValidate;
29
30 5
    public function __construct(Column $column, FormManagerField $formField, $name, $presentation = null)
31
    {
32 5
        $this->dbal = $column;
33 5
        $this->formField = $formField;
34 5
        $this->name = $name;
35 5
        $this->presentation = $presentation;
36 5
        $this->validationRules = [];
37 5
        $this->functions = [];
38 5
        $this->options = [];
39 5
        $this->hideValue = false;
40 5
        $this->saveIfEmpty = true;
41 5
        $this->noValidate = false;
42 5
    }
43
44
    public function __clone()
45
    {
46
        $formField = clone $this->formField;
47
        $this->formField = $formField;
48
        $field = new self($this->dbal, $formField, $this->name, $this->presentation);
49
50
        return $field;
51
    }
52
53
    public function getName()
54
    {
55
        return $this->name;
56
    }
57
58
    /**
59
     * @param string $name
60
     */
61
    public function setName($name)
62
    {
63
        $this->name = $name;
64
    }
65
66 1 View Code Duplication
    public function presentation()
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...
67
    {
68 1
        if ($this->presentation) {
69 1
            return transcrud($this->presentation);
70
        }
71
72
        $nameWithSpaces = str_replace('_', ' ', $this->name);
73
        $namePieces = explode(' ', $nameWithSpaces);
74
        $namePieces = array_filter(array_map('trim', $namePieces));
75
76
        return transcrud(ucfirst(implode(' ', $namePieces)));
77
    }
78
79 1
    public function type()
80
    {
81 1
        return $this->dbal->getType();
82
    }
83
84
    public function setValidationRules($rules)
85
    {
86
        $this->validationRules = explode('|', $rules);
87
    }
88
89 1
    public function getValidationRules()
90
    {
91 1
        if (count($this->validationRules) === 0 && $this->noValidate() === false) {
92 1
            if ($this->dbal->getNotnull()) {
93 1
                $this->validationRules[] = 'required';
94 1
            }
95 1
        }
96
97 1
        return implode('|', $this->validationRules);
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getValidationRulesArray()
104
    {
105
        return explode('|', $this->getValidationRules());
106
    }
107
108
    public function setFunctions($functions)
109
    {
110
        if (!is_array($functions)) {
111
            $functions = [$functions];
112
        }
113
114
        $this->functions = $functions;
115
    }
116
117
    public function applyFunctions($value)
118
    {
119
        foreach ($this->functions as $function) {
120
            if (!function_exists($function)) {
121
                throw new \Exception('Function '.$function.' does not exist');
122
            }
123
124
            $value = call_user_func($function, $value);
125
        }
126
127
        return $value;
128
    }
129
130
    /**
131
     * @param string $value
132
     *
133
     * @return void
134
     */
135
    public function setValue($value)
136
    {
137
        $this->value = $value;
138
139
        if (!$this->hideValue()) {
140
            $this->formField->val($this->value);
141
        }
142
    }
143
144
    /**
145
     * @return mixed
146
     */
147
    public function getValue()
148
    {
149
        return $this->value;
150
    }
151
152
    /**
153
     * @param array $options
154
     *
155
     * @return void
156
     */
157
    public function setOptions(array $options)
158
    {
159
        $this->options = $options;
160
161
        $this->formField->options($this->options);
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function getOptions()
168
    {
169
        return $this->options;
170
    }
171
172
    public function getFormField()
173
    {
174
        if (!$this->hideValue()) {
175
            if (Request::old($this->name)) {
176
                $this->formField->val(Request::old($this->name));
177
            }
178
        }
179
180
        return $this->formField;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->formField; (FormManager\Fields\Field) is incompatible with the return type declared by the interface Anavel\Crud\Contracts\Ab...tor\Field::getFormField of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
181
    }
182
183
    public function hideValue($value = null)
184
    {
185
        if (!is_null($value)) {
186
            $this->hideValue = $value;
187
        }
188
189
        return $this->hideValue;
190
    }
191
192
    public function saveIfEmpty($value = null)
193
    {
194
        if (!is_null($value)) {
195
            $this->saveIfEmpty = $value;
196
        }
197
198
        return $this->saveIfEmpty;
199
    }
200
201 1
    public function noValidate($value = null)
202
    {
203 1
        if (!is_null($value)) {
204
            $this->noValidate = $value;
205
        }
206
207 1
        return $this->noValidate;
208
    }
209
210
    /**
211
     * @param array $attributes
212
     *
213
     * @return void
214
     */
215
    public function setFormElementAttributes(array $attributes)
216
    {
217
        $this->formField->attr($attributes);
218
    }
219
}
220