Image   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 6
dl 12
loc 56
ccs 0
cts 38
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getImagePathAttribute() 0 4 1
A setImagePathAttribute() 0 6 1
A get() 9 9 2
A parseRows() 0 16 3
A getImageUrl() 3 8 3

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 Sco\Admin\Display;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Sco\Admin\Display\Concerns\WithPagination;
8
use Sco\Admin\Traits\UploadStorageTrait;
9
10
class Image extends Display
11
{
12
    use UploadStorageTrait, WithPagination;
13
14
    protected $type = 'image';
15
16
    protected $imagePathAttribute;
17
18
    public function getImagePathAttribute()
19
    {
20
        return $this->imagePathAttribute;
21
    }
22
23
    public function setImagePathAttribute($value)
24
    {
25
        $this->imagePathAttribute = $value;
26
27
        return $this;
28
    }
29
30 View Code Duplication
    public function get()
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...
31
    {
32
        if ($this->isPagination()) {
33
            $data = $this->paginate();
34
35
            return $data->setCollection($this->parseRows($data->getCollection()));
36
        }
37
        return $this->parseRows($this->getQuery()->get());
0 ignored issues
show
Bug introduced by
It seems like $this->getQuery()->get() targeting Illuminate\Database\Eloquent\Builder::get() can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Sco\Admin\Display\Image::parseRows() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
38
    }
39
40
    protected function parseRows(Collection $rows)
41
    {
42
        if (is_null($pathKey = $this->getImagePathAttribute())) {
43
            throw new \InvalidArgumentException('Must set image attribute');
44
        }
45
46
        return $rows->map(function (Model $row) use ($pathKey) {
47
            if (! isset($row->$pathKey)) {
48
                throw new \InvalidArgumentException("Not Found '{$pathKey}' attribute");
49
            }
50
            $row->setAttribute('_primary', $row->getKey());
51
            $row->setAttribute('_url', $this->getImageUrl($row->$pathKey));
52
53
            return $row;
54
        });
55
    }
56
57
    protected function getImageUrl($value)
58
    {
59 View Code Duplication
        if (! empty($value) && (strpos($value, '://') === false)) {
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...
60
            $value = $this->getFileUrl($value);
61
        }
62
63
        return $value;
64
    }
65
}
66