Passed
Push — master ( 4866cc...749535 )
by smiley
09:39
created

SQLite::fieldspec()   C

Complexity

Conditions 15
Paths 192

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 27
nc 192
nop 9
dl 0
loc 47
rs 5.15
c 0
b 0
f 0

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Class SQLite
4
 *
5
 * @filesource   SQLite.php
6
 * @created      11.01.2018
7
 * @package      chillerlan\Database\Dialects
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Dialects;
14
15
class SQLite extends DialectAbstract{
16
17
	/** @inheritdoc */
18
	public function fieldspec(string $name, string $type, $length = null, string $attribute = null, string $collation = null, bool $isNull = null, string $defaultType = null, $defaultValue = null, string $extra = null):string{
19
		$type      = strtoupper(trim($type));
20
		$collation = strtoupper($collation);
0 ignored issues
show
Bug introduced by
It seems like $collation can also be of type null; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
		$collation = strtoupper(/** @scrutinizer ignore-type */ $collation);
Loading history...
21
22
		$field = [$this->quote(trim($name))];
23
24
		$type_translation = [
25
			'MEDIUMTEXT' => 'TEXT',
26
			'LONGTEXT'   => 'TEXT',
27
		][$type] ?? $type;
28
29
		if($length !== null && (in_array($type, ['CHAR', 'NCHAR', 'VARCHAR', 'NVARCHAR', 'CHARACTER'], true) || (is_string($length) && count(explode(',', $length)) === 2 && $type === 'DECIMAL'))){
30
			$field[] = $type_translation.'('.$length.')';
31
		}
32
		else{
33
			$field[] = $type_translation;
34
		}
35
36
		if(is_bool($isNull)){
37
			$field[] = $isNull ? 'NULL' : 'NOT NULL';
38
		}
39
40
		if(in_array($collation, ['BINARY', 'NOCASE', 'RTRIM'], true)){
41
			$field[] = 'COLLATE '.$collation;
42
		}
43
44
		$defaultType = strtoupper($defaultType);
45
46
		if($defaultType === 'USER_DEFINED'){
47
			$field[] = 'DEFAULT \''.$defaultValue.'\'';
48
		}
49
		elseif(in_array($defaultType, ['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'], true)){
50
			$field[] = 'DEFAULT '.$defaultType;
51
		}
52
		elseif($defaultType === 'NULL' && $isNull === true){
53
			$field[] = 'DEFAULT NULL';
54
		}
55
56
		if($attribute){
57
			$field[] = $attribute;
58
		}
59
60
		if($extra){
61
			$field[] = $extra;
62
		}
63
64
		return implode(' ', $field);
65
	}
66
67
	/** @inheritdoc */
68
	public function createTable(string $table, array $cols, string $primaryKey = null, bool $ifNotExists = null, bool $temp = null, string $dir = null):array{
69
		$sql = ['CREATE'];
70
71
		if($temp){
72
			$sql[] = 'TEMPORARY';
73
		}
74
75
		$sql[] = 'TABLE';
76
77
		if($ifNotExists){
78
			$sql[] = 'IF NOT EXISTS';
79
		}
80
81
		$n = explode('.', $table);
82
83
		$sql[] = $this->quote($n[count($n) - 1]);
84
85
		if(!empty($cols)){
86
			$sql[] = '(';
87
			$sql[] = implode(', ', $cols);
88
89
			if($primaryKey){
90
				$sql[] = ', PRIMARY KEY';
91
				$sql[] = '(';
92
				$sql[] = $this->quote($primaryKey);
93
94
				if(in_array($dir, ['ASC', 'DESC'], true)){
95
					$sql[] = ($dir);
96
				}
97
98
				$sql[] = ')';
99
			}
100
101
			$sql[] = ')';
102
		}
103
104
		return $sql;
105
	}
106
107
	/** @inheritdoc */
108
	public function truncate(string $table):array{
109
		$sql = ['DELETE FROM'];// ??? sqlite
110
		$sql[] = $this->quote($table);
111
112
		return $sql;
113
	}
114
115
	/** @inheritdoc */
116
	public function showTables(string $database = null, string $pattern = null, string $where = null):array{
117
		/** @noinspection SqlResolve */
118
		return ['SELECT "name" AS "tablename" FROM sqlite_master'];
119
	}
120
121
	/** @inheritdoc */
122
	public function showCreateTable(string $table):array{
123
		/** @noinspection SqlResolve */
124
		$sql = ['SELECT "name" AS "Table", "sql" AS "Create Table" FROM sqlite_master WHERE name ='];
125
		$sql[] = $this->quote($table);
126
127
		return $sql;
128
	}
129
}
130