Completed
Pull Request — master (#3089)
by Robin
30:27 queued 16:11
created

OCIExpressionBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 138
Duplicated Lines 5.8 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 138
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareColumn() 0 8 5
A comparison() 0 6 1
A eq() 0 6 1
A neq() 0 6 1
A lt() 0 6 1
A lte() 0 6 1
A gt() 0 6 1
A gte() 0 6 1
A in() 0 6 1
A notIn() 0 6 1
A castColumn() 8 8 2
A like() 0 3 1
A iLike() 0 5 1

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
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\DB\QueryBuilder\ExpressionBuilder;
26
27
28
use OC\DB\QueryBuilder\QueryFunction;
29
use OCP\DB\QueryBuilder\ILiteral;
30
use OCP\DB\QueryBuilder\IParameter;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
use OCP\DB\QueryBuilder\IQueryFunction;
33
34
class OCIExpressionBuilder extends ExpressionBuilder {
35
36
	/**
37
	 * @param mixed $column
38
	 * @param mixed|null $type
39
	 * @return array|IQueryFunction|string
40
	 */
41
	protected function prepareColumn($column, $type) {
42
		if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) {
43
			$column = $this->castColumn($column, $type);
44
		} else {
45
			$column = $this->helper->quoteColumnNames($column);
46
		}
47
		return $column;
48
	}
49
50
	/**
51
	 * @inheritdoc
52
	 */
53
	public function comparison($x, $operator, $y, $type = null) {
54
		$x = $this->prepareColumn($x, $type);
55
		$y = $this->prepareColumn($y, $type);
56
57
		return $this->expressionBuilder->comparison($x, $operator, $y);
58
	}
59
60
	/**
61
	 * @inheritdoc
62
	 */
63
	public function eq($x, $y, $type = null) {
64
		$x = $this->prepareColumn($x, $type);
65
		$y = $this->prepareColumn($y, $type);
66
67
		return $this->expressionBuilder->eq($x, $y);
68
	}
69
70
	/**
71
	 * @inheritdoc
72
	 */
73
	public function neq($x, $y, $type = null) {
74
		$x = $this->prepareColumn($x, $type);
75
		$y = $this->prepareColumn($y, $type);
76
77
		return $this->expressionBuilder->neq($x, $y);
78
	}
79
80
	/**
81
	 * @inheritdoc
82
	 */
83
	public function lt($x, $y, $type = null) {
84
		$x = $this->prepareColumn($x, $type);
85
		$y = $this->prepareColumn($y, $type);
86
87
		return $this->expressionBuilder->lt($x, $y);
88
	}
89
90
	/**
91
	 * @inheritdoc
92
	 */
93
	public function lte($x, $y, $type = null) {
94
		$x = $this->prepareColumn($x, $type);
95
		$y = $this->prepareColumn($y, $type);
96
97
		return $this->expressionBuilder->lte($x, $y);
98
	}
99
100
	/**
101
	 * @inheritdoc
102
	 */
103
	public function gt($x, $y, $type = null) {
104
		$x = $this->prepareColumn($x, $type);
105
		$y = $this->prepareColumn($y, $type);
106
107
		return $this->expressionBuilder->gt($x, $y);
108
	}
109
110
	/**
111
	 * @inheritdoc
112
	 */
113
	public function gte($x, $y, $type = null) {
114
		$x = $this->prepareColumn($x, $type);
115
		$y = $this->prepareColumn($y, $type);
116
117
		return $this->expressionBuilder->gte($x, $y);
118
	}
119
120
	/**
121
	 * @inheritdoc
122
	 */
123
	public function in($x, $y, $type = null) {
124
		$x = $this->prepareColumn($x, $type);
125
		$y = $this->prepareColumn($y, $type);
126
127
		return $this->expressionBuilder->in($x, $y);
128
	}
129
130
	/**
131
	 * @inheritdoc
132
	 */
133
	public function notIn($x, $y, $type = null) {
134
		$x = $this->prepareColumn($x, $type);
135
		$y = $this->prepareColumn($y, $type);
136
137
		return $this->expressionBuilder->notIn($x, $y);
138
	}
139
140
	/**
141
	 * Returns a IQueryFunction that casts the column to the given type
142
	 *
143
	 * @param string $column
144
	 * @param mixed $type One of IQueryBuilder::PARAM_*
145
	 * @return IQueryFunction
146
	 */
147 View Code Duplication
	public function castColumn($column, $type) {
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...
148
		if ($type === IQueryBuilder::PARAM_STR) {
149
			$column = $this->helper->quoteColumnName($column);
150
			return new QueryFunction('to_char(' . $column . ')');
151
		}
152
153
		return parent::castColumn($column, $type);
154
	}
155
156
	/**
157
	 * @inheritdoc
158
	 */
159
	public function like($x, $y, $type = null) {
160
		return parent::like($x, $y, $type) . " ESCAPE '\\'";
161
	}
162
163
	/**
164
	 * @inheritdoc
165
	 */
166
	public function iLike($x, $y, $type = null) {
167
		$x = $this->helper->quoteColumnName($x);
168
		$y = $this->helper->quoteColumnName($y);
169
		return new QueryFunction('REGEXP_LIKE('.$x.', \'^\' || REPLACE('.$y.', \'%\', \'.*\') || \'$\', \'i\')');
170
	}
171
}
172