Conditions | 27 |
Paths | 3744 |
Total Lines | 83 |
Code Lines | 55 |
Lines | 15 |
Ratio | 18.07 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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 |
||
75 | protected function fieldspec( |
||
76 | string $name, |
||
77 | string $type, |
||
78 | $length = null, |
||
79 | string $attribute = null, |
||
80 | string $collation = null, |
||
81 | bool $isNull = null, |
||
82 | string $defaultType = null, |
||
83 | $defaultValue = null, |
||
84 | string $extra = null |
||
85 | ){ |
||
86 | $name = trim($name); |
||
87 | $type = strtoupper(trim($type)); |
||
88 | |||
89 | $field = ['`'.$name.'`']; |
||
90 | |||
91 | // @todo: whitelist types? |
||
92 | $nolengthtypes = ['DATE', 'TINYBLOB', 'TINYTEXT', 'BLOB', 'TEXT', 'MEDIUMBLOB', |
||
93 | 'MEDIUMTEXT', 'LONGBLOB', 'LONGTEXT', 'SERIAL', 'BOOLEAN', 'UUID']; |
||
94 | |||
95 | $field[] = (is_int($length) || is_string($length) && count(explode(',', $length)) === 2) && !in_array($type, $nolengthtypes) |
||
96 | ? $type.'('. $length . ')' |
||
97 | : $type; |
||
98 | |||
99 | if($attribute){ |
||
100 | $field[] = strtoupper($attribute); |
||
101 | } |
||
102 | |||
103 | $collationtypes = ['TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT', 'VARCHAR', 'CHAR', 'ENUM', 'SET']; |
||
104 | if($collation && in_array($type, $collationtypes)){ |
||
105 | list($charset) = explode('_', $collation); |
||
106 | |||
107 | $field[] = 'CHARACTER SET '.$charset; |
||
108 | |||
109 | if($charset !== $collation){ |
||
110 | $field[] = 'COLLATE '.$collation; |
||
111 | } |
||
112 | |||
113 | } |
||
114 | |||
115 | if(is_bool($isNull)){ |
||
116 | $field[] = $isNull ? 'NULL' : 'NOT NULL'; |
||
117 | } |
||
118 | |||
119 | $defaultType = strtoupper($defaultType); |
||
120 | |||
121 | if($defaultType === 'USER_DEFINED'){ |
||
122 | |||
123 | switch(true){ |
||
124 | case $type === 'TIMESTAMP' && intval($defaultValue) === 0: |
||
125 | $field[] = 'DEFAULT 0'; |
||
126 | break; |
||
127 | View Code Duplication | case $type === 'BIT': |
|
128 | $field[] = 'DEFAULT b\''.preg_replace('/[^01]/', '0', $defaultValue).'\''; |
||
129 | break; |
||
130 | View Code Duplication | case $type === 'BOOLEAN': |
|
131 | $field[] = 'DEFAULT '.preg_match('/^1|T|TRUE|YES$/i', $defaultValue) ? 'TRUE' : 'FALSE'; |
||
132 | break; |
||
133 | case $type === 'BINARY' || $type === 'VARBINARY': |
||
134 | $field[] = 'DEFAULT 0x'.$defaultValue; |
||
135 | break; |
||
136 | View Code Duplication | case strtoupper($defaultValue) === 'NULL' && $isNull === true: |
|
137 | $field[] = 'DEFAULT NULL'; |
||
138 | break; |
||
139 | default: |
||
140 | $field[] = 'DEFAULT '.(is_int($defaultValue) || is_float($defaultValue) ? $defaultValue : '\''.$defaultValue.'\'') ; |
||
141 | } |
||
142 | |||
143 | } |
||
144 | View Code Duplication | else if($defaultType === 'CURRENT_TIMESTAMP'){ |
|
145 | $field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
||
146 | } |
||
147 | else if($defaultType === 'NULL' && $isNull === true){ |
||
148 | $field[] = 'DEFAULT NULL'; |
||
149 | } |
||
150 | |||
151 | |||
152 | if($extra){ |
||
153 | $field[] = $extra; |
||
154 | } |
||
155 | |||
156 | return implode(' ', $field); |
||
157 | } |
||
158 | |||
160 |
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.