Conditions | 17 |
Paths | 384 |
Total Lines | 62 |
Code Lines | 40 |
Lines | 6 |
Ratio | 9.68 % |
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 |
||
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: |
||
|
|||
96 | $field[] = 'DEFAULT 0'; |
||
97 | break; |
||
98 | case strtoupper($defaultValue) === 'NULL' && $isNull: |
||
99 | $field[] = 'DEFAULT NULL'; |
||
100 | break; |
||
101 | default: |
||
102 | $field[] = 'DEFAULT \''.$defaultValue.'\''; |
||
103 | } |
||
104 | |||
105 | } |
||
106 | View Code Duplication | else if($defaultType === 'CURRENT_TIMESTAMP'){ |
|
107 | $field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
||
108 | } |
||
109 | else if($defaultType === 'NULL' && $isNull){ |
||
110 | $field[] = 'DEFAULT NULL'; |
||
111 | } |
||
112 | |||
113 | if($constraint){ |
||
114 | $field[] = strtoupper($constraint); |
||
115 | } |
||
116 | |||
117 | if($extra){ |
||
118 | $field[] = $extra; |
||
119 | } |
||
120 | |||
121 | return implode(' ', $field); |
||
122 | } |
||
123 | |||
125 |
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.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.