Completed
Push — master ( 466d49...06d08c )
by smiley
05:19
created

CreateTableAbstract   B

Complexity

Total Complexity 35

Size/Duplication

Total Lines 251
Duplicated Lines 3.59 %

Coupling/Cohesion

Components 6
Dependencies 1

Importance

Changes 0
Metric Value
wmc 35
lcom 6
cbo 1
dl 9
loc 251
rs 8.1818
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A temp() 0 5 1
A ifNotExists() 0 5 1
A name() 0 9 2
A primaryKey() 0 5 1
fieldspec() 0 11 ?
A field() 0 16 1
A charset() 9 9 2
A tinyint() 0 3 3
A int() 0 3 3
A varchar() 0 3 3
A decimal() 0 3 3
A tinytext() 0 3 3
A text() 0 3 3
B enum() 0 20 8
A timestamp() 0 4 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
 * Class CreateTableAbstract
4
 *
5
 * @filesource   CreateTableAbstract.php
6
 * @created      04.06.2017
7
 * @package      chillerlan\Database\Query\Dialects
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query;
14
15
abstract class CreateTableAbstract extends StatementAbstract implements CreateTableInterface{
16
17
	/**
18
	 * @var string
19
	 */
20
	protected $name;
21
22
	/**
23
	 * @var bool
24
	 */
25
	protected $ifNotExists = false;
26
27
	/**
28
	 * @var bool
29
	 */
30
	protected $temp = false;
31
32
	/**
33
	 * @var string
34
	 */
35
	protected $primaryKey;
36
37
	/**
38
	 * @var array
39
	 */
40
	protected $cols = [];
41
42
	/**
43
	 * @var string
44
	 */
45
	protected $collate;
46
47
	/**
48
	 * @param string $collation
49
	 *
50
	 * @return \chillerlan\Database\Query\CreateTableInterface
51
	 */
52 View Code Duplication
	public function charset(string $collation):CreateTableInterface{
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...
53
		$collation = trim($collation);
54
55
		if(!empty($collation)){
56
			$this->collate = $collation;
57
		}
58
59
		return $this;
60
	}
61
62
	/**
63
	 * @return \chillerlan\Database\Query\CreateTableInterface
64
	 */
65
	public function temp():CreateTableInterface{
66
		$this->temp = true;
67
68
		return $this;
69
	}
70
71
	/**
72
	 * @return \chillerlan\Database\Query\CreateTableInterface
73
	 */
74
	public function ifNotExists():CreateTableInterface{
75
		$this->ifNotExists = true;
76
77
		return $this;
78
	}
79
80
	/**
81
	 * @param string|null $tablename
82
	 *
83
	 * @return \chillerlan\Database\Query\CreateTableInterface
84
	 */
85
	public function name(string $tablename = null):CreateTableInterface{
86
		$name = trim($tablename);
87
88
		if(!empty($name)){
89
			$this->name = $tablename;
90
		}
91
92
		return $this;
93
	}
94
95
	/**
96
	 * @param string $field
97
	 *
98
	 * @return \chillerlan\Database\Query\CreateTableInterface
99
	 */
100
	public function primaryKey(string $field):CreateTableInterface{
101
		$this->primaryKey = $field;
102
103
		return $this;
104
	}
105
106
	/**
107
	 * @param string      $name
108
	 * @param string      $type
109
	 * @param null        $length
110
	 * @param string|null $attribute
111
	 * @param string|null $collation
112
	 * @param bool|null   $isNull
113
	 * @param string|null $defaultType
114
	 * @param null        $defaultValue
115
	 * @param string|null $extra
116
	 *
117
	 * @return mixed
118
	 */
119
	abstract protected function fieldspec(
120
		string $name,
121
		string $type,
122
		$length = null,
123
		string $attribute = null,
124
		string $collation = null,
125
		bool $isNull = null,
126
		string $defaultType = null,
127
		$defaultValue = null,
128
		string $extra = null
129
	);
130
131
	/**
132
	 * @param string      $name
133
	 * @param string      $type
134
	 * @param null        $length
135
	 * @param string|null $attribute
136
	 * @param string|null $collation
137
	 * @param bool|null   $isNull
138
	 * @param string|null $defaultType
139
	 * @param null        $defaultValue
140
	 * @param string|null $extra
141
	 *
142
	 * @return \chillerlan\Database\Query\CreateTableInterface
143
	 */
144
	public function field(
145
		string $name,
146
		string $type,
147
		$length = null,
148
		string $attribute = null,
149
		string $collation = null,
150
		bool $isNull = null,
151
		string $defaultType = null,
152
		$defaultValue = null,
153
		string $extra = null
154
	):CreateTableInterface {
155
156
		$this->cols[$name] = $this->fieldspec($name, $type, $length, $attribute, $collation, $isNull, $defaultType, $defaultValue, $extra);
157
158
		return $this;
159
	}
160
161
	/**
162
	 * @param $name
163
	 *
164
	 * @return \chillerlan\Database\Query\CreateTableInterface
165
	 */
166
#	public function index($name):CreateTableInterface{}
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
167
168
	// @todo: clean up this mess...
169
170
	/**
171
	 * @param string      $name
172
	 * @param int|null    $length
173
	 * @param bool|null   $isNull
174
	 * @param int|null    $defaultValue
175
	 * @param string|null $attribute
176
	 *
177
	 * @return \chillerlan\Database\Query\CreateTableInterface
178
	 */
179
	public function tinyint(string $name, int $length = null, int $defaultValue = null , bool $isNull = null, string $attribute = null):CreateTableInterface{
180
		return $this->field($name, 'TINYINT', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $length defined by parameter $length on line 179 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
Bug introduced by
It seems like $defaultValue defined by parameter $defaultValue on line 179 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
181
	}
182
183
184
	public function int(string $name, int $length = null, int $defaultValue = null , bool $isNull = null, string $attribute = null):CreateTableInterface{
185
		return $this->field($name, 'INT', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $length defined by parameter $length on line 184 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
Bug introduced by
It seems like $defaultValue defined by parameter $defaultValue on line 184 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
186
	}
187
188
189
	/**
190
	 * @param string    $name
191
	 * @param int|null  $length
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $length a bit more specific; maybe use integer.
Loading history...
192
	 * @param bool|null $isNull
193
	 * @param int|null  $defaultValue
194
	 *
195
	 * @return \chillerlan\Database\Query\CreateTableInterface
196
	 */
197
	public function varchar(string $name, int $length, int $defaultValue = null , bool $isNull = null):CreateTableInterface{
198
		return $this->field($name, 'VARCHAR', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $defaultValue defined by parameter $defaultValue on line 197 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
199
	}
200
201
	/**
202
	 * @param string    $name
203
	 * @param string    $length
204
	 * @param bool|null $isNull
205
	 * @param int|null  $defaultValue
206
	 *
207
	 * @return \chillerlan\Database\Query\CreateTableInterface
208
	 */
209
	public function decimal(string $name, string $length, int $defaultValue = null , bool $isNull = null):CreateTableInterface{
210
		return $this->field($name, 'DECIMAL', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $defaultValue defined by parameter $defaultValue on line 209 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
211
	}
212
213
	/**
214
	 * @param string   $name
215
	 * @param bool     $isNull
216
	 * @param int|null $defaultValue
217
	 *
218
	 * @return \chillerlan\Database\Query\CreateTableInterface
219
	 */
220
	public function tinytext(string $name, int $defaultValue = null , bool $isNull = true):CreateTableInterface{
221
		return $this->field($name, 'TINYTEXT', null, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $defaultValue defined by parameter $defaultValue on line 220 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
222
	}
223
224
225
	public function text(string $name, int $defaultValue = null , bool $isNull = true):CreateTableInterface{
226
		return $this->field($name, 'TEXT', null, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $defaultValue defined by parameter $defaultValue on line 225 can also be of type integer; however, chillerlan\Database\Quer...eTableAbstract::field() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
227
	}
228
229
230
	/**
231
	 * @param string    $name
232
	 * @param array     $values
233
	 * @param null      $defaultValue
234
	 * @param bool|null $isNull
235
	 *
236
	 * @return \chillerlan\Database\Query\CreateTableInterface
237
	 */
238
	public function enum(string $name, array $values, $defaultValue = null , bool $isNull = null):CreateTableInterface{
239
240
		$field = $this->quote($name);
241
		$field .= 'ENUM (\''.implode('\', \'', $values).'\')';
242
243
		if(is_bool($isNull)){
244
			$field .= $isNull ? 'NULL' : 'NOT NULL';
245
		}
246
247
		if(in_array($defaultValue, $values, true)){
248
			$field .= 'DEFAULT '.(is_int($defaultValue) || is_float($defaultValue) ? $defaultValue : '\''.$defaultValue.'\'') ;
249
		}
250
		elseif($isNull && strtolower($defaultValue) === 'null'){
251
			$field .= 'DEFAULT NULL';
252
		}
253
254
		$this->cols[$name] = $field;
255
256
		return $this;
257
	}
258
259
260
	public function timestamp():CreateTableInterface{
261
262
		return $this;
263
	}
264
265
}
266