Completed
Push — master ( 8e8eee...0073f8 )
by wen
12:40
created

View   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 127
Duplicated Lines 8.66 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 11
loc 127
c 2
b 0
f 2
wmc 18
lcom 3
cbo 5
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A initialize() 0 4 1
A extend() 0 4 1
A setRepository() 0 7 1
A getRepository() 0 4 1
A getWith() 0 4 1
A with() 0 6 1
A getQuery() 0 12 2
A orderBy() 0 8 1
A toArray() 0 6 1
C __call() 11 23 7

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\View;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Database\Eloquent\Builder;
7
use Sco\Admin\Contracts\RepositoryInterface;
8
use Sco\Admin\Contracts\View\Extensions\ExtensionInterface;
9
use Sco\Admin\Contracts\View\ViewInterface;
10
use Sco\Admin\View\Extensions\Applies;
11
use Sco\Admin\View\Extensions\Filters;
12
use Sco\Admin\View\Extensions\Scopes;
13
use Sco\Admin\Contracts\View\Filters\FilterInterface;
14
15
/**
16
 * @method Scopes getScopes() get query scopes
17
 * @method $this setScopes($scope, ...$scopes) set query scopes
18
 * @method $this addScope($scope, $parameter, ...$parameters) add query scope
19
 *
20
 * @method Applies getApplies() get query applies
21
 * @method $this setApplies(\Closure $apply, ...$applies) set query applies
22
 * @method $this addApply(\Closure $apply) add query apply
23
 *
24
 * @method Filters getFilters()
25
 * @method $this setFilters(FilterInterface $filter, ...$filters)
26
 * @method $this addFilter(FilterInterface $filter)
27
 *
28
 */
29
abstract class View implements ViewInterface, Arrayable
30
{
31
    /**
32
     * @var array
33
     */
34
    protected $with = [];
35
36
    /**
37
     * @var RepositoryInterface
38
     */
39
    protected $repository;
40
41
    protected $type;
42
43
    protected $extensions;
44
45
    public function __construct()
46
    {
47
        $this->extensions = new Extensions();
48
49
        $this->extend('scopes', new Scopes());
50
        $this->extend('applies', new Applies());
51
        $this->extend('filters', new Filters());
52
    }
53
54
    public function initialize()
55
    {
56
        $this->extensions->initialize();
57
    }
58
59
    public function extend($name, ExtensionInterface $extension)
60
    {
61
        $this->extensions->put($name, $extension);
62
    }
63
64
    public function setRepository(RepositoryInterface $repository)
65
    {
66
        $this->repository = $repository;
67
        $this->repository->with($this->getWith());
68
69
        return $this;
70
    }
71
72
    public function getRepository()
73
    {
74
        return $this->repository;
75
    }
76
77
    /**
78
     * @return string[]
79
     */
80
    public function getWith()
81
    {
82
        return $this->with;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function with($relations)
89
    {
90
        $this->with = array_flatten(func_get_args());
91
92
        return $this;
93
    }
94
95
    public function getQuery()
96
    {
97
        $repository = $this->getRepository();
98
99
        $builder = $repository->getQuery();
100
101
        if ($repository->isRestorable()) {
102
            $builder->withTrashed();
103
        }
104
105
        return $builder;
106
    }
107
108
    /**
109
     * Add an "order by" clause to the query.
110
     *
111
     * @param  string $column
112
     * @param  string $direction
113
     *
114
     * @return $this
115
     */
116
    public function orderBy($column, $direction = 'asc')
117
    {
118
        $this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) {
0 ignored issues
show
Bug introduced by
The property scopes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
119
            $builder->orderBy($column, $direction);
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
120
        };
121
122
        return $this;
123
    }
124
125
    public function toArray()
126
    {
127
        return [
128
            'type' => $this->type,
129
        ];
130
    }
131
132
    public function __call($name, $parameters)
133
    {
134
        $method = snake_case(substr($name, 3));
135
136
        if (starts_with($name, 'get') && $this->extensions->has($method)) {
137
            return $this->extensions->get($method);
138
        }
139
140 View Code Duplication
        if (starts_with($name, 'set') && $this->extensions->has($method)) {
1 ignored issue
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...
141
            $extension = $this->extensions->get($method);
142
            call_user_func_array([$extension, 'set'], $parameters);
143
144
            return $this;
145
        }
146
147 View Code Duplication
        if (starts_with($name, 'add') && $this->extensions->has(str_plural($method))) {
1 ignored issue
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...
148
            $extension = $this->extensions->get(str_plural($method));
149
            call_user_func_array([$extension, 'add'], $parameters);
150
            return $this;
151
        }
152
153
        throw new \BadMethodCallException("Call to undefined method [{$name}]");
154
    }
155
}
156