Completed
Push — master ( 7e212d...676b8c )
by wen
12:32
created

Table   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 10.34 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 9
loc 87
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A orderByFromRequest() 0 9 3
A setColumns() 0 8 2
A getColumns() 0 4 1
A get() 9 10 2
A toArray() 0 6 1
A parseRows() 0 20 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\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Sco\Admin\Contracts\Display\ColumnInterface;
9
use Sco\Admin\Display\Concerns\WithPagination;
10
11
class Table extends Display
12
{
13
    use WithPagination;
14
15
    protected $columns;
16
17
    protected $type = 'table';
18
19
    public function __construct()
20
    {
21
        parent::__construct();
22
23
        $this->columns = new Collection();
24
        $this->orderByFromRequest();
25
    }
26
27
    protected function orderByFromRequest()
28
    {
29
        $orderBy = request()->query('orderBy', '');
30
        $sortedBy = request()->query('sortedBy', '');
31
32
        if (! empty($orderBy) && ! empty($sortedBy)) {
33
            $this->orderBy($orderBy, $sortedBy);
34
        }
35
    }
36
37
    /**
38
     * @param array $columns
39
     *
40
     * @return $this
41
     */
42
    public function setColumns(array $columns)
43
    {
44
        foreach ($columns as $column) {
45
            $this->columns->push($column);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return \Illuminate\Support\Collection
53
     */
54
    public function getColumns()
55
    {
56
        return $this->columns;
57
    }
58
59 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...
60
    {
61
        if ($this->isPagination()) {
62
            $data = $this->paginate();
63
64
            return $data->setCollection($this->parseRows($data->getCollection()));
65
        }
66
67
        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\Table::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...
68
    }
69
70
    public function toArray()
71
    {
72
        return parent::toArray() + [
73
                'columns' => $this->getColumns(),
74
            ];
75
    }
76
77
    protected function parseRows(Collection $rows)
78
    {
79
        return $rows->map(function (Model $row) {
80
            $newRow = $this->getColumns()->mapWithKeys(function (
81
                ColumnInterface $column
82
            ) use ($row) {
83
                return [
84
                    $column->getName() => $column->setModel($row)->getValue(),
85
                ];
86
            });
87
88
            // whether this row has been soft deleted
89
            if ($this->getRepository()->isRestorable()) {
90
                $newRow->put('_deleted', $row->trashed() ? 1 : 0);
91
            }
92
            $newRow->put('_primary', $row->getKey());
93
94
            return $newRow;
95
        });
96
    }
97
}
98