Completed
Push — master ( 47c098...6c95d6 )
by smiley
02:23
created

CreateTable::fieldspec()   F

Complexity

Conditions 23
Paths 1280

Size

Total Lines 71
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 2.5828
c 0
b 0
f 0
cc 23
eloc 50
nc 1280
nop 9

How to fix   Long Method    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 CreateTable
4
 *
5
 * @filesource   CreateTable.php
6
 * @created      03.06.2017
7
 * @package      chillerlan\Database\Query\Dialects\MySQL
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query\Dialects\MySQL;
14
15
use chillerlan\Database\Query\CreateTableAbstract;
16
use chillerlan\Database\Query\CreateTableInterface;
17
use chillerlan\Database\Query\Traits\CharsetTrait;
18
19
class CreateTable extends CreateTableAbstract{
20
	use CharsetTrait;
21
22
	public function collate(string $collation):CreateTableInterface{return $this->_collate($collation);}
23
24
	public function sql():string{
25
#		var_dump($this->cols);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
26
27
		$sql = 'CREATE ';
28
29
		$sql .= $this->temp ? 'TEMPORARY ' : '';
30
		$sql .= 'TABLE ';
31
		$sql .= $this->ifNotExists ? 'IF NOT EXISTS ' : '';
32
		$sql .= $this->quote($this->name);
33
		$sql .= !empty($this->cols)
34
			? '('.PHP_EOL."\t".implode(','.PHP_EOL."\t", $this->cols)
35
			  .($this->primaryKey ? ','.PHP_EOL."\t".'PRIMARY KEY ('.$this->quote($this->primaryKey).')' : '').PHP_EOL.')'
36
			: '';
37
38
		return $sql;
39
	}
40
41
	protected function fieldspec(
42
		string $name,
43
		string $type,
44
		$length = null,
45
		string $attribute = null,
46
		string $collation = null,
47
		bool $isNull = false,
48
		string $defaultType = null,
49
		$defaultValue = null,
50
		string $extra = null
51
	){
52
		$field = [$this->quote($name)];
53
54
		$type = strtoupper(trim($type));
55
		$nolengthtypes = ['DATE', 'TINYBLOB', 'TINYTEXT', 'BLOB', 'TEXT', 'MEDIUMBLOB',
56
		                  'MEDIUMTEXT', 'LONGBLOB', 'LONGTEXT', 'SERIAL', 'BOOLEAN', 'UUID'];
57
58
		$field[] = (is_int($length) || is_string($length) && count(explode(',', $length)) === 2) && !in_array($type, $nolengthtypes)
59
			? $type.'('. $length . ')'
60
			: $type;
61
62
		if($attribute){
0 ignored issues
show
Bug Best Practice introduced by
The expression $attribute of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63
			$field[] = strtoupper($attribute);
64
		}
65
66
		$collationtypes = ['TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT', 'VARCHAR', 'CHAR', 'ENUM', 'SET'];
67
		if($collation && in_array($type, $collationtypes)){
0 ignored issues
show
Bug Best Practice introduced by
The expression $collation of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68
			$field[] = $this->_charset($collation);
69
		}
70
71
		$field[] = $isNull ? 'NULL' : 'NOT NULL';
72
73
		$defaultType = strtoupper($defaultType);
74
75
		if($defaultType === 'USER_DEFINED'){
76
77
			switch(true){
78
				case $type === 'TIMESTAMP' && intval($defaultValue) === 0:
79
					$field[] = 'DEFAULT 0';
80
					break;
81
				case $type === 'BIT':
82
					$field[] = 'DEFAULT b\''.preg_replace('/[^01]/', '0', $defaultValue).'\'';
83
					break;
84
				case $type === 'BOOLEAN':
85
					$field[] = 'DEFAULT '.preg_match('/^1|T|TRUE|YES$/i', $defaultValue) ? 'TRUE' : 'FALSE';
86
					break;
87
				case $type === 'BINARY' || $type === 'VARBINARY':
88
					$field[] = 'DEFAULT 0x'.$defaultValue;
89
					break;
90
				case strtoupper($defaultValue) === 'NULL' && $isNull:
91
					$field[] = 'DEFAULT NULL';
92
					break;
93
				default:
94
					$field[] = 'DEFAULT \''.$defaultValue.'\'';
95
			}
96
97
		}
98
		else if($defaultType === 'CURRENT_TIMESTAMP'){
99
			$field[] = 'DEFAULT CURRENT_TIMESTAMP';
100
		}
101
		else if($defaultType === 'NULL' && $isNull){
102
			$field[] = 'DEFAULT NULL';
103
		}
104
105
106
		if($extra){
0 ignored issues
show
Bug Best Practice introduced by
The expression $extra of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
107
			$field[] = $extra;
108
		}
109
110
		return implode(' ', $field);
111
	}
112
113
114
}
115