Conditions | 16 |
Paths | 192 |
Total Lines | 65 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
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 |
||
65 | protected function fieldspec( |
||
66 | string $name, |
||
67 | string $type, |
||
68 | $length = null, |
||
69 | string $attribute = null, |
||
70 | string $collation = null, |
||
71 | bool $isNull = null, |
||
72 | string $defaultType = null, |
||
73 | $defaultValue = null, |
||
74 | string $extra = null |
||
75 | ){ |
||
76 | $name = trim($name); |
||
77 | $type = strtoupper(trim($type)); |
||
78 | $collation = strtoupper($collation); |
||
79 | |||
80 | $field = ['"'.$name.'"']; |
||
81 | |||
82 | $type_translation = [ |
||
83 | 'MEDIUMTEXT' => 'TEXT', |
||
84 | 'LONGTEXT' => 'TEXT', |
||
85 | ][$type] ?? $type; |
||
86 | |||
87 | |||
88 | |||
89 | if(is_int($length)&& in_array($type, ['CHAR', 'NCHAR','VARCHAR', 'NVARCHAR', 'CHARACTER']) |
||
90 | || is_string($length) && count(explode(',', $length)) === 2 && $type === 'DECIMAL'){ |
||
91 | $field[] = $type_translation.'('. $length . ')'; |
||
92 | } |
||
93 | else{ |
||
94 | $field[] = $type_translation; |
||
95 | } |
||
96 | |||
97 | if(is_bool($isNull)){ |
||
98 | $field[] = $isNull ? 'NULL' : 'NOT NULL'; |
||
99 | } |
||
100 | |||
101 | if($collation && in_array($collation, ['BINARY', 'NOCASE', 'RTRIM'])){ |
||
102 | $field[] = 'COLLATE '.$collation; |
||
103 | } |
||
104 | |||
105 | |||
106 | $defaultType = strtoupper($defaultType); |
||
107 | |||
108 | if($defaultType === 'USER_DEFINED'){ |
||
109 | $field[] = 'DEFAULT \''.$defaultValue.'\''; |
||
110 | } |
||
111 | else if(in_array($defaultType, ['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'])){ |
||
112 | $field[] = 'DEFAULT '.$defaultType; |
||
113 | } |
||
114 | else if($defaultType === 'NULL' && $isNull === true){ |
||
115 | $field[] = 'DEFAULT NULL'; |
||
116 | } |
||
117 | |||
118 | |||
119 | |||
120 | if($attribute){ |
||
|
|||
121 | $field[] = $attribute; |
||
122 | } |
||
123 | |||
124 | if($extra){ |
||
125 | $field[] = $extra; |
||
126 | } |
||
127 | |||
128 | return implode(' ', $field); |
||
129 | } |
||
130 | } |
||
131 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: