Completed
Push — master ( 024746...0b49f1 )
by Song
04:00
created

Field   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 294
Duplicated Lines 11.9 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 35
loc 294
rs 10
c 0
b 0
f 0
wmc 25
lcom 2
cbo 5

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setParent() 0 6 1
A getName() 0 4 1
A formatLabel() 0 6 2
A getLabel() 0 4 1
A as() 0 6 1
A values() 0 6 1
A image() 11 14 3
A link() 0 8 2
A label() 12 12 2
A badge() 12 12 2
A setValue() 0 14 3
A setRelation() 0 6 1
A __call() 0 9 2
A render() 0 16 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 Encore\Admin\Show;
4
5
use Encore\Admin\Show;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\Storage;
11
12
class Field implements Renderable
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $view = 'admin::show.field';
18
19
    /**
20
     * Name of column.
21
     *
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * Label of column.
28
     *
29
     * @var string
30
     */
31
    protected $label;
32
33
    /**
34
     * Field value.
35
     *
36
     * @var mixed
37
     */
38
    protected $value;
39
40
    /**
41
     * @var Collection
42
     */
43
    protected $showAs = [];
44
45
    /**
46
     * Parent show instance.
47
     *
48
     * @var Show
49
     */
50
    protected $parent;
51
52
    /**
53
     * Relation name.
54
     *
55
     * @var string
56
     */
57
    protected $relation;
58
59
    /**
60
     * Field constructor.
61
     *
62
     * @param string $name
63
     * @param string $label
64
     */
65
    public function __construct($name = '', $label = '')
66
    {
67
        $this->name = $name;
68
69
        $this->label = $this->formatLabel($label);
70
71
        $this->showAs = new Collection();
72
    }
73
74
    /**
75
     * Set parent show instance.
76
     *
77
     * @param Show $show
78
     *
79
     * @return $this
80
     */
81
    public function setParent(Show $show)
82
    {
83
        $this->parent = $show;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Get name of this column.
90
     *
91
     * @return mixed
92
     */
93
    public function getName()
94
    {
95
        return $this->name;
96
    }
97
98
    /**
99
     * Format label.
100
     *
101
     * @param $label
102
     *
103
     * @return mixed
104
     */
105
    protected function formatLabel($label)
106
    {
107
        $label = $label ?: ucfirst($this->name);
108
109
        return str_replace(['.', '_'], ' ', $label);
110
    }
111
112
    /**
113
     * Get label of the column.
114
     *
115
     * @return mixed
116
     */
117
    public function getLabel()
118
    {
119
        return $this->label;
120
    }
121
122
    /**
123
     * Field display callback.
124
     *
125
     * @param callable $callable
126
     *
127
     * @return $this
128
     */
129
    public function as(callable $callable)
130
    {
131
        $this->showAs->push($callable);
132
133
        return $this;
134
    }
135
136
    /**
137
     * Set value mapping.
138
     *
139
     * @param array $values
140
     * @param null  $default
141
     *
142
     * @return $this
143
     */
144
    public function values(array $values, $default = null)
145
    {
146
        return $this->as(function ($value) use ($values, $default) {
147
            return array_get($values, $value, $default);
148
        });
149
    }
150
151
    /**
152
     * Show field as a image.
153
     *
154
     * @param string $server
155
     * @param int    $width
156
     * @param int    $height
157
     *
158
     * @return $this
159
     */
160
    public function image($server = '', $width = 200, $height = 200)
161
    {
162 View Code Duplication
        return $this->as(function ($path) use ($server, $width, $height) {
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...
163
            if (url()->isValidUrl($path)) {
164
                $src = $path;
165
            } elseif ($server) {
166
                $src = $server.$path;
167
            } else {
168
                $src = Storage::disk(config('admin.upload.disk'))->url($path);
169
            }
170
171
            return "<img src='$src' style='max-width:{$width}px;max-height:{$height}px' class='img' />";
172
        });
173
    }
174
175
    /**
176
     * Show field as a link.
177
     *
178
     * @param string $href
179
     * @param string $target
180
     *
181
     * @return Field
182
     */
183
    public function link($href = '', $target = '_blank')
184
    {
185
        return $this->as(function ($link) use ($href, $target) {
186
            $href = $href ?: $link;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $href, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
187
188
            return "<a href='$href' target='{$target}'>{$link}</a>";
189
        });
190
    }
191
192
    /**
193
     * Show field as labels.
194
     *
195
     * @param string $style
196
     *
197
     * @return Field
198
     */
199 View Code Duplication
    public function label($style = 'success')
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...
200
    {
201
        return $this->as(function ($value) use ($style) {
202
            if ($value instanceof Arrayable) {
203
                $value = $value->toArray();
204
            }
205
206
            return collect((array) $value)->map(function ($name) use ($style) {
207
                return "<span class='label label-{$style}'>$name</span>";
208
            })->implode('&nbsp;');
209
        });
210
    }
211
212
    /**
213
     * Show field as badges.
214
     *
215
     * @param string $style
216
     *
217
     * @return Field
218
     */
219 View Code Duplication
    public function badge($style = 'blue')
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...
220
    {
221
        return $this->as(function ($value) use ($style) {
222
            if ($value instanceof Arrayable) {
223
                $value = $value->toArray();
224
            }
225
226
            return collect((array) $value)->map(function ($name) use ($style) {
227
                return "<span class='badge bg-{$style}'>$name</span>";
228
            })->implode('&nbsp;');
229
        });
230
    }
231
232
    /**
233
     * Set value for this field.
234
     *
235
     * @param Model $model
236
     *
237
     * @return $this
238
     */
239
    public function setValue(Model $model)
240
    {
241
        if ($this->relation) {
242
            if (!$model->{$this->relation}) {
243
                return $this;
244
            }
245
246
            $this->value = $model->{$this->relation}->getAttribute($this->name);
247
        } else {
248
            $this->value = $model->getAttribute($this->name);
249
        }
250
251
        return $this;
252
    }
253
254
    /**
255
     * Set relation name for this field.
256
     *
257
     * @param string $relation
258
     *
259
     * @return $this
260
     */
261
    public function setRelation($relation)
262
    {
263
        $this->relation = $relation;
264
265
        return $this;
266
    }
267
268
    /**
269
     * @param string $method
270
     * @param array  $arguments
271
     *
272
     * @return $this
273
     */
274
    public function __call($method, $arguments = [])
275
    {
276
        if ($this->relation) {
277
            $this->name  = $method;
278
            $this->label = $this->formatLabel(array_get($arguments, 0));
279
        }
280
281
        return $this;
282
    }
283
284
    /**
285
     * Render this field.
286
     *
287
     * @return string
288
     */
289
    public function render()
290
    {
291
        if ($this->showAs->isNotEmpty()) {
292
            $this->showAs->each(function ($callable) {
293
                $this->value = $callable->call(
294
                    $this->parent->getModel(),
295
                    $this->value
296
                );
297
            });
298
        }
299
300
        $content = $this->value;
301
        $label   = $this->getLabel();
302
303
        return view($this->view, compact('content', 'label'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view($this->view, compact('content', 'label')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 303 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
304
    }
305
}
306