Conditions | 32 |
Paths | 13312 |
Total Lines | 106 |
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 |
||
95 | protected function _getPortableTableColumnDefinition($tableColumn) { |
||
96 | $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER); |
||
97 | |||
98 | $dbType = \strtolower($tableColumn['type']); |
||
99 | $dbType = \strtok($dbType, '(), '); |
||
100 | if (isset($tableColumn['length'])) { |
||
101 | $length = $tableColumn['length']; |
||
102 | } else { |
||
103 | $length = \strtok('(), '); |
||
104 | } |
||
105 | |||
106 | $fixed = null; |
||
107 | |||
108 | if (! isset($tableColumn['name'])) { |
||
109 | $tableColumn['name'] = ''; |
||
110 | } |
||
111 | |||
112 | $scale = null; |
||
113 | $precision = null; |
||
114 | |||
115 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
||
116 | |||
117 | // In cases where not connected to a database DESCRIBE $table does not return 'Comment' |
||
118 | if (isset($tableColumn['comment'])) { |
||
119 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
120 | $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
121 | } |
||
122 | |||
123 | switch ($dbType) { |
||
124 | case 'char': |
||
125 | case 'binary': |
||
126 | $fixed = true; |
||
127 | break; |
||
128 | case 'float': |
||
129 | case 'double': |
||
130 | case 'real': |
||
131 | case 'numeric': |
||
132 | case 'decimal': |
||
133 | if (\preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) { |
||
134 | $precision = $match[1]; |
||
135 | $scale = $match[2]; |
||
136 | $length = null; |
||
137 | } |
||
138 | break; |
||
139 | case 'tinytext': |
||
140 | $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT; |
||
141 | break; |
||
142 | case 'text': |
||
143 | $length = MySqlPlatform::LENGTH_LIMIT_TEXT; |
||
144 | break; |
||
145 | case 'mediumtext': |
||
146 | $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT; |
||
147 | break; |
||
148 | case 'tinyblob': |
||
149 | $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB; |
||
150 | break; |
||
151 | case 'blob': |
||
152 | $length = MySqlPlatform::LENGTH_LIMIT_BLOB; |
||
153 | break; |
||
154 | case 'mediumblob': |
||
155 | $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB; |
||
156 | break; |
||
157 | case 'tinyint': |
||
158 | case 'smallint': |
||
159 | case 'mediumint': |
||
160 | case 'int': |
||
161 | case 'integer': |
||
162 | case 'bigint': |
||
163 | case 'year': |
||
164 | $length = null; |
||
165 | break; |
||
166 | } |
||
167 | |||
168 | $length = ((int) $length == 0) ? null : (int) $length; |
||
169 | |||
170 | $default = isset($tableColumn['default']) ? $tableColumn['default'] : null; |
||
171 | $columnDefault = $this->getMariaDb1027ColumnDefault($default); |
||
172 | |||
173 | $options = [ |
||
174 | 'length' => $length, |
||
175 | 'unsigned' => (bool) (\strpos($tableColumn['type'], 'unsigned') !== false), |
||
176 | 'fixed' => (bool) $fixed, |
||
177 | // This line was changed to fix breaking change introduced in MariaDB 10.2.6 |
||
178 | 'default' => $columnDefault, |
||
179 | 'notnull' => (bool) ($tableColumn['null'] != 'YES'), |
||
180 | 'scale' => null, |
||
181 | 'precision' => null, |
||
182 | 'autoincrement' => (bool) (\strpos($tableColumn['extra'], 'auto_increment') !== false), |
||
183 | 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
||
184 | ? $tableColumn['comment'] |
||
185 | : null, |
||
186 | ]; |
||
187 | |||
188 | if ($scale !== null && $precision !== null) { |
||
189 | $options['scale'] = $scale; |
||
190 | $options['precision'] = $precision; |
||
191 | } |
||
192 | |||
193 | $column = new Column($tableColumn['field'], Type::getType($type), $options); |
||
194 | |||
195 | if (isset($tableColumn['collation'])) { |
||
196 | $column->setPlatformOption('collation', $tableColumn['collation']); |
||
197 | } |
||
198 | |||
199 | return $column; |
||
200 | } |
||
201 | |||
245 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: