Total Complexity | 32 |
Total Lines | 162 |
Duplicated Lines | 3.7 % |
Changes | 0 |
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 GeneaLabs\LaravelModelCaching; |
||
7 | class CacheKey |
||
8 | { |
||
9 | protected $eagerLoad; |
||
10 | protected $model; |
||
11 | protected $query; |
||
12 | |||
13 | public function __construct(array $eagerLoad, Model $model, Builder $query) |
||
14 | { |
||
15 | $this->eagerLoad = $eagerLoad; |
||
16 | $this->model = $model; |
||
17 | $this->query = $query; |
||
18 | } |
||
19 | |||
20 | public function make(array $columns = ['*'], $idColumn = null) : string |
||
21 | { |
||
22 | $key = $this->getModelSlug(); |
||
23 | $key .= $this->getIdColumn($idColumn ?: ''); |
||
24 | $key .= $this->getQueryColumns($columns); |
||
25 | $key .= $this->getWhereClauses(); |
||
26 | $key .= $this->getWithModels(); |
||
27 | $key .= $this->getOrderByClauses(); |
||
28 | $key .= $this->getOffsetClause(); |
||
29 | $key .= $this->getLimitClause(); |
||
30 | |||
31 | return $key; |
||
32 | } |
||
33 | |||
34 | protected function getIdColumn(string $idColumn) : string |
||
35 | { |
||
36 | return $idColumn ? "_{$idColumn}" : ''; |
||
37 | } |
||
38 | |||
39 | protected function getLimitClause() : string |
||
46 | } |
||
47 | |||
48 | protected function getModelSlug() : string |
||
51 | } |
||
52 | |||
53 | protected function getOffsetClause() : string |
||
54 | { |
||
60 | } |
||
61 | |||
62 | protected function getOrderByClauses() : string |
||
70 | } |
||
71 | |||
72 | protected function getQueryColumns(array $columns) : string |
||
73 | { |
||
74 | if ($columns === ['*'] || $columns === []) { |
||
75 | return ''; |
||
76 | } |
||
77 | |||
78 | return '_' . implode('_', $columns); |
||
79 | } |
||
80 | |||
81 | protected function getTypeClause($where) : string |
||
86 | } |
||
87 | |||
88 | protected function getValuesClause(array $where = null) : string |
||
93 | } |
||
94 | |||
95 | protected function getWhereClauses(array $wheres = []) : string |
||
107 | } |
||
108 | |||
109 | protected function getNestedClauses(array $where) : string |
||
110 | { |
||
111 | View Code Duplication | if (! in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) { |
|
1 ignored issue
–
show
|
|||
112 | return ''; |
||
113 | } |
||
114 | |||
115 | return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres); |
||
116 | } |
||
117 | |||
118 | protected function getColumnClauses(array $where) : string |
||
119 | { |
||
120 | if ($where['type'] !== 'Column') { |
||
121 | return ''; |
||
122 | } |
||
123 | |||
124 | return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; |
||
125 | } |
||
126 | |||
127 | protected function getRawClauses(array $where) : string |
||
128 | { |
||
129 | if ($where['type'] !== 'raw') { |
||
130 | return ''; |
||
131 | } |
||
132 | |||
133 | return "_{$where['boolean']}_" . str_slug($where['sql']); |
||
134 | } |
||
135 | |||
136 | protected function getOtherClauses(array $where, string $carry = null) : string |
||
147 | } |
||
148 | |||
149 | protected function getWheres(array $wheres) : Collection |
||
158 | } |
||
159 | |||
160 | protected function getWithModels() : string |
||
171 |
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.