Completed
Push — master ( 621e91...dd0af9 )
by Ryan
07:19
created

FormCriteria   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 227
Duplicated Lines 12.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 17
c 2
b 1
f 0
lcom 1
cbo 5
dl 29
loc 227
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A get() 0 6 1
A builder() 0 6 1
B build() 0 26 1
A setParameter() 0 6 1
A getBuilder() 0 4 1
A setBuilder() 0 12 2
A __get() 0 4 1
C __call() 29 38 7
A __toString() 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 namespace Anomaly\Streams\Platform\Ui\Form;
2
3
use Anomaly\Streams\Platform\Support\Decorator;
4
use Anomaly\Streams\Platform\Support\Hydrator;
5
use Anomaly\Streams\Platform\Traits\FiresCallbacks;
6
use Illuminate\Contracts\Cache\Repository;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class FormCriteria
11
 *
12
 * @link          http://anomaly.is/streams-platform
13
 * @author        AnomalyLabs, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 * @package       Anomaly\Streams\Platform\Ui\Form
16
 */
17
class FormCriteria
18
{
19
20
    use FiresCallbacks;
21
22
    /**
23
     * The cache repository.
24
     *
25
     * @var Repository
26
     */
27
    protected $cache;
28
29
    /**
30
     * The form builder.
31
     *
32
     * @var FormBuilder
33
     */
34
    protected $builder;
35
36
    /**
37
     * The request object.
38
     *
39
     * @var Request
40
     */
41
    protected $request;
42
43
    /**
44
     * The hydrator utility.
45
     *
46
     * @var Hydrator
47
     */
48
    protected $hydrator;
49
50
    /**
51
     * The parameters.
52
     *
53
     * @var array
54
     */
55
    protected $parameters = [];
56
57
    /**
58
     * Create a new FormCriteria instance.
59
     *
60
     * @param Repository  $cache
61
     * @param Request     $request
62
     * @param Hydrator    $hydrator
63
     * @param FormBuilder $builder
64
     * @param array       $parameters
65
     */
66
    public function __construct(
67
        Repository $cache,
68
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
69
        Hydrator $hydrator,
70
        FormBuilder $builder,
71
        array $parameters = []
72
    ) {
73
        $this->cache      = $cache;
74
        $this->builder    = $builder;
75
        $this->request    = $request;
76
        $this->hydrator   = $hydrator;
77
        $this->parameters = $parameters;
78
79
        $this->setBuilder($builder);
80
    }
81
82
    /**
83
     * Get the form.
84
     *
85
     * @return FormPresenter
86
     */
87
    public function get()
88
    {
89
        $this->builder();
90
91
        return (new Decorator())->decorate($this->builder->make()->getForm());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \Anomaly\Str...er->make()->getForm()); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform\Ui\Form\FormCriteria::get of type Anomaly\Streams\Platform...Form\FormPresenter|null.

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...
92
    }
93
94
    /**
95
     * Return the hydrated builder.
96
     *
97
     * @return FormBuilder
98
     */
99
    public function builder()
100
    {
101
        $this->build();
102
103
        return $this->builder->make();
104
    }
105
106
    /**
107
     * Build the builder.
108
     */
109
    protected function build()
110
    {
111
        $this->fire('ready', ['criteria' => $this]);
112
113
        array_set($this->parameters, 'key', md5(json_encode($this->parameters)));
114
115
        array_set(
116
            $this->parameters,
117
            'options.url',
118
            array_get(
119
                $this->parameters,
120
                'options.url',
121
                $this->builder->getOption('url', 'form/handle/' . array_get($this->parameters, 'key'))
122
            )
123
        );
124
125
        $this->cache->remember(
126
            'form::' . array_get($this->parameters, 'key'),
127
            30,
128
            function () {
129
                return $this->parameters;
130
            }
131
        );
132
133
        $this->hydrator->hydrate($this->builder, $this->parameters);
134
    }
135
136
    /**
137
     * Set a parameter.
138
     *
139
     * @param $key
140
     * @param $value
141
     * @return $this
142
     */
143
    public function setParameter($key, $value)
144
    {
145
        $this->parameters[$key] = $value;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Get the builder.
152
     *
153
     * @return FormBuilder
154
     */
155
    public function getBuilder()
156
    {
157
        return $this->builder;
158
    }
159
160
    /**
161
     * Set the form builder.
162
     *
163
     * @param FormBuilder $builder
164
     * @return $this
165
     */
166
    public function setBuilder($builder)
167
    {
168
        if (!is_string($builder)) {
169
            $builder = get_class($builder);
170
        }
171
172
        array_set($this->parameters, 'builder', $builder);
173
174
        $this->builder = app($builder);
175
176
        return $this;
177
    }
178
179
    /**
180
     * Route through __get
181
     *
182
     * @param $name
183
     * @return $this
184
     */
185
    public function __get($name)
186
    {
187
        return $this->__call($name, []);
188
    }
189
190
    /**
191
     * @param $name
192
     * @param $arguments
193
     * @return $this
194
     */
195
    public function __call($name, $arguments)
196
    {
197
198 View Code Duplication
        if (method_exists($this->builder, camel_case('set_' . $name))) {
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...
199
200
            array_set($this->parameters, $name, array_shift($arguments));
201
202
            return $this;
203
        }
204
205 View Code Duplication
        if (method_exists($this->builder, camel_case('add_' . $name))) {
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...
206
207
            array_set($this->parameters, $name, array_shift($arguments));
208
209
            return $this;
210
        }
211
212 View Code Duplication
        if (!method_exists($this->builder, camel_case($name)) && count($arguments) === 1) {
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...
213
214
            $key = snake_case($name);
215
216
            array_set($this->parameters, "options.{$key}", array_shift($arguments));
217
218
            return $this;
219
        }
220
221 View Code Duplication
        if (!method_exists($this->builder, camel_case($name)) && count($arguments) === 0) {
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...
222
223
            $key = snake_case($name);
224
225
            // Helpful for form.disableLabels().disableFoo() ...
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
226
            array_set($this->parameters, "options.{$key}", true);
227
228
            return $this;
229
        }
230
231
        return $this;
232
    }
233
234
    /**
235
     * Return the form.
236
     *
237
     * @return string
238
     */
239
    public function __toString()
240
    {
241
        return $this->get()->__toString();
242
    }
243
}
244