Completed
Push — master ( 74f5ec...2784cb )
by Ryan
06:52
created

FormCriteria   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 208
Duplicated Lines 13.94 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B get() 0 28 1
A builder() 0 6 1
A getBuilder() 0 4 1
A setBuilder() 0 15 1
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->fire('ready', ['criteria' => $this]);
90
91
        array_set($this->parameters, 'key', md5(json_encode($this->parameters)));
92
93
        array_set(
94
            $this->parameters,
95
            'options.url',
96
            array_get(
97
                $this->parameters,
98
                'options.url',
99
                $this->builder->getOption('url', 'form/handle/' . array_get($this->parameters, 'key'))
100
            )
101
        );
102
103
        $this->cache->remember(
104
            'form::' . array_get($this->parameters, 'key'),
105
            30,
106
            function () {
107
                return $this->parameters;
108
            }
109
        );
110
111
        $this->hydrator->hydrate($this->builder, $this->parameters);
112
113
        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...
114
    }
115
116
    /**
117
     * Return the hydrated builder.
118
     *
119
     * @return FormBuilder
120
     */
121
    public function builder()
122
    {
123
        $this->hydrator->hydrate($this->builder, $this->parameters);
124
125
        return $this->builder;
126
    }
127
128
    /**
129
     * Get the builder.
130
     *
131
     * @return FormBuilder
132
     */
133
    public function getBuilder()
134
    {
135
        return $this->builder;
136
    }
137
138
    /**
139
     * Set the form builder.
140
     *
141
     * @param FormBuilder $builder
142
     * @return $this
143
     */
144
    protected function setBuilder(FormBuilder $builder)
145
    {
146
        array_set($this->parameters, 'builder', get_class($builder));
147
148
        array_set(
149
            $this->parameters,
150
            'options',
151
            array_merge(
152
                $builder->getOptions(),
153
                array_get($this->parameters, 'options', [])
154
            )
155
        );
156
157
        return $this;
158
    }
159
160
    /**
161
     * Route through __get
162
     *
163
     * @param $name
164
     * @return $this
165
     */
166
    public function __get($name)
167
    {
168
        return $this->__call($name, []);
169
    }
170
171
    /**
172
     * @param $name
173
     * @param $arguments
174
     * @return $this
175
     */
176
    public function __call($name, $arguments)
177
    {
178
179 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...
180
181
            array_set($this->parameters, $name, array_shift($arguments));
182
183
            return $this;
184
        }
185
186 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...
187
188
            array_set($this->parameters, $name, array_shift($arguments));
189
190
            return $this;
191
        }
192
193 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...
194
195
            $key = snake_case($name);
196
197
            array_set($this->parameters, "options.{$key}", array_shift($arguments));
198
199
            return $this;
200
        }
201
202 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...
203
204
            $key = snake_case($name);
205
206
            // 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...
207
            array_set($this->parameters, "options.{$key}", true);
208
209
            return $this;
210
        }
211
212
        return $this;
213
    }
214
215
    /**
216
     * Return the form.
217
     *
218
     * @return string
219
     */
220
    public function __toString()
221
    {
222
        return $this->get()->__toString();
223
    }
224
}
225