Completed
Push — master ( ec27e4...948d8b )
by smiley
14:17
created

CreateTable::fieldspec()   D

Complexity

Conditions 17
Paths 384

Size

Total Lines 62
Code Lines 40

Duplication

Lines 6
Ratio 9.68 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 6
loc 62
rs 4.5408
cc 17
eloc 40
nc 384
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\Firebird
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query\Dialects\Firebird;
14
15
use chillerlan\Database\Query\CreateTableAbstract;
16
use chillerlan\Database\Query\QueryException;
17
18
/**
19
 * @link https://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-ddl-tbl.html#fblangref25-ddl-tbl-create
20
 */
21
class CreateTable extends CreateTableAbstract{
22
23
	public function sql():string{
24
25
		if(empty($this->name)){
26
			throw new QueryException('no name specified');
27
		}
28
29
		$sql = $this->ifNotExists ? 'RECREATE ' : 'CREATE '; // nasty
30
		$sql .= $this->temp ? 'GLOBAL TEMPORARY ' : '';
31
		$sql .= 'TABLE ';
32
		$sql .= strtoupper($this->name);
33
34
		$cols = [];
35
36
		if(!empty($this->cols)){
37
38
			foreach($this->cols as $name => $col){
39
40
				if(strtoupper($name) === strtoupper($this->primaryKey)){
41
					$x = explode(' NOT NULL', $col, 2);
42
43
					if(count($x) > 0){
44
						$col = $x[0].' NOT NULL PRIMARY KEY';
45
						$col .= $x[1] ?? '';
46
					}
47
48
				}
49
50
				$cols[] = $col;
51
			}
52
53
			$sql .= ' ('.PHP_EOL."\t".implode(','.PHP_EOL."\t", $cols).PHP_EOL.')';
54
		}
55
56
		$sql .= '';
57
58
		return $sql;
59
	}
60
61
	protected function fieldspec(
62
		string $name,
63
		string $type,
64
		$length = null,
65
		string $constraint = null,
66
		string $collation = null,
67
		bool $isNull = false,
68
		string $defaultType = null,
69
		$defaultValue = null,
70
		string $extra = null
71
	){
72
		$name = strtoupper(trim($name));
73
		$type = strtoupper(trim($type));
74
75
		$field = [$name];
76
77
		// @todo: whitelist types?
78
		$nolengthtypes = ['BLOB', 'SMALLINT', 'INTEGER', 'BIGINT', 'FLOAT',
79
		                  'DOUBLE PRECISION', 'DATE', 'TIME', 'TINYTEXT'];
80
81
		$field[] = (is_int($length) || is_string($length) && count(explode(',', $length)) === 2) && !in_array($type, $nolengthtypes)
82
			? $type.'('. $length . ')'
83
			: $type;
84
85
86
		if(!$isNull && !in_array($type, ['DATE', 'TIME', 'TIMESTAMP', 'CHAR', 'NCHAR', 'VARCHAR'])){
87
			$field[] = 'NOT NULL';
88
		}
89
90
		$defaultType = strtoupper($defaultType);
91
92
		if($defaultType === 'USER_DEFINED'){
93
94
			switch(true){
95
				case $type === 'TIMESTAMP' && intval($defaultValue) === 0:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
96
					$field[] = 'DEFAULT 0';
97
					break;
98
				case strtoupper($defaultValue) === 'NULL' && $isNull:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
99
					$field[] = 'DEFAULT NULL';
100
					break;
101
				default:
102
					$field[] = 'DEFAULT \''.$defaultValue.'\'';
103
			}
104
105
		}
106 View Code Duplication
		else if($defaultType === 'CURRENT_TIMESTAMP'){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107
			$field[] = 'DEFAULT CURRENT_TIMESTAMP';
108
		}
109
		else if($defaultType === 'NULL' && $isNull){
110
			$field[] = 'DEFAULT NULL';
111
		}
112
113
		if($constraint){
0 ignored issues
show
Bug Best Practice introduced by
The expression $constraint 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...
114
			$field[] = strtoupper($constraint);
115
		}
116
117
		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...
118
			$field[] = $extra;
119
		}
120
121
		return implode(' ', $field);
122
	}
123
124
}
125