Conditions | 12 |
Paths | 22 |
Total Lines | 68 |
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:
1 | <?php |
||
100 | public function declareColumn( |
||
101 | AbstractColumn $column, |
||
102 | string $definition, |
||
103 | bool $hasDefault, |
||
104 | $default = null |
||
105 | ) { |
||
106 | if ( |
||
107 | class_exists($definition) |
||
108 | && is_a($definition, ColumnInterface::class, true) |
||
109 | ) { |
||
110 | //Dedicating column definition to our column class |
||
111 | call_user_func([$definition, 'describeColumn'], $column); |
||
112 | |||
113 | return $column; |
||
114 | } |
||
115 | |||
116 | //Expression used to declare column type, easy to read |
||
117 | $pattern = '/(?P<type>[a-z]+)(?: *\((?P<options>[^\)]+)\))?(?: *, *(?P<nullable>null(?:able)?))?/i'; |
||
118 | |||
119 | if (!preg_match($pattern, $definition, $type)) { |
||
120 | throw new DefinitionException( |
||
121 | "Invalid column type definition in '{$column->getTable()}'.'{$column->getName()}'" |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | if (!empty($type['options'])) { |
||
126 | //Exporting and trimming |
||
127 | $type['options'] = array_map('trim', explode(',', $type['options'])); |
||
128 | } |
||
129 | |||
130 | //ORM force EVERY column to NOT NULL state unless different is said |
||
131 | $column->nullable(false); |
||
132 | |||
133 | if (!empty($type['nullable'])) { |
||
134 | //Indication that column is nullable |
||
135 | $column->nullable(true); |
||
136 | } |
||
137 | try { |
||
138 | //Bypassing call to AbstractColumn->__call method (or specialized column method) |
||
139 | call_user_func_array( |
||
140 | [$column, $type['type']], |
||
141 | !empty($type['options']) ? $type['options'] : [] |
||
142 | ); |
||
143 | } catch (\Throwable $e) { |
||
144 | throw new DefinitionException( |
||
145 | "Invalid column type definition in '{$column->getTable()}'.'{$column->getName()}'", |
||
146 | $e->getCode(), |
||
147 | $e |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | if (in_array($column->abstractType(), ['primary', 'bigPrimary'])) { |
||
152 | //No default value can be set of primary keys |
||
153 | return $column; |
||
154 | } |
||
155 | |||
156 | if (!$hasDefault && !$column->isNullable()) { |
||
157 | //We have to come up with some default value |
||
158 | return $column->defaultValue($this->castDefault($column)); |
||
159 | } |
||
160 | |||
161 | if (is_null($default)) { |
||
162 | //Default value is stated and NULL, clear what to do |
||
163 | $column->nullable(true); |
||
164 | } |
||
165 | |||
166 | return $column->defaultValue($default); |
||
167 | } |
||
168 | |||
199 | } |
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.