Complex classes like MySqlSchemaManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySqlSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class MySqlSchemaManager extends AbstractSchemaManager |
||
36 | { |
||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | protected function _getPortableViewDefinition($view) |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | protected function _getPortableTableDefinition($table) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | protected function _getPortableUserDefinition($user) |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | protected function _getPortableSequenceDefinition($sequence) |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | protected function _getPortableDatabaseDefinition($database) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | protected function _getPortableTableColumnDefinition($tableColumn) |
||
107 | { |
||
108 | $tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
||
109 | |||
110 | $dbType = strtolower($tableColumn['type']); |
||
111 | $dbType = strtok($dbType, '(), '); |
||
112 | if (isset($tableColumn['length'])) { |
||
113 | $length = $tableColumn['length']; |
||
114 | } else { |
||
115 | $length = strtok('(), '); |
||
116 | } |
||
117 | |||
118 | $fixed = null; |
||
119 | |||
120 | if ( ! isset($tableColumn['name'])) { |
||
121 | $tableColumn['name'] = ''; |
||
122 | } |
||
123 | |||
124 | $scale = null; |
||
125 | $precision = null; |
||
126 | |||
127 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
||
128 | |||
129 | // In cases where not connected to a database DESCRIBE $table does not return 'Comment' |
||
130 | if (isset($tableColumn['comment'])) { |
||
131 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
132 | $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
133 | } |
||
134 | |||
135 | switch ($dbType) { |
||
136 | case 'char': |
||
137 | case 'binary': |
||
138 | $fixed = true; |
||
139 | break; |
||
140 | case 'float': |
||
141 | case 'double': |
||
142 | case 'real': |
||
143 | case 'numeric': |
||
144 | case 'decimal': |
||
145 | if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) { |
||
146 | $precision = $match[1]; |
||
147 | $scale = $match[2]; |
||
148 | $length = null; |
||
149 | } |
||
150 | break; |
||
151 | case 'tinytext': |
||
152 | $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT; |
||
153 | break; |
||
154 | case 'text': |
||
155 | $length = MySqlPlatform::LENGTH_LIMIT_TEXT; |
||
156 | break; |
||
157 | case 'mediumtext': |
||
158 | $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT; |
||
159 | break; |
||
160 | case 'tinyblob': |
||
161 | $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB; |
||
162 | break; |
||
163 | case 'blob': |
||
164 | $length = MySqlPlatform::LENGTH_LIMIT_BLOB; |
||
165 | break; |
||
166 | case 'mediumblob': |
||
167 | $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB; |
||
168 | break; |
||
169 | case 'tinyint': |
||
170 | case 'smallint': |
||
171 | case 'mediumint': |
||
172 | case 'int': |
||
173 | case 'integer': |
||
174 | case 'bigint': |
||
175 | case 'year': |
||
176 | $length = null; |
||
177 | break; |
||
178 | } |
||
179 | |||
180 | if ($this->_platform instanceof MariaDb1027Platform) { |
||
181 | $columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default']); |
||
182 | } else { |
||
183 | $columnDefault = $tableColumn['default']; |
||
184 | } |
||
185 | |||
186 | $options = [ |
||
187 | 'length' => $length !== null ? (int) $length : null, |
||
188 | 'unsigned' => strpos($tableColumn['type'], 'unsigned') !== false, |
||
189 | 'fixed' => (bool) $fixed, |
||
190 | 'default' => $columnDefault, |
||
191 | 'notnull' => $tableColumn['null'] !== 'YES', |
||
192 | 'scale' => null, |
||
193 | 'precision' => null, |
||
194 | 'autoincrement' => strpos($tableColumn['extra'], 'auto_increment') !== false, |
||
195 | 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
||
196 | ? $tableColumn['comment'] |
||
197 | : null, |
||
198 | ]; |
||
199 | |||
200 | if ($scale !== null && $precision !== null) { |
||
201 | $options['scale'] = (int) $scale; |
||
202 | $options['precision'] = (int) $precision; |
||
203 | } |
||
204 | |||
205 | $column = new Column($tableColumn['field'], Type::getType($type), $options); |
||
206 | |||
207 | if (isset($tableColumn['collation'])) { |
||
208 | $column->setPlatformOption('collation', $tableColumn['collation']); |
||
209 | } |
||
210 | |||
211 | return $column; |
||
212 | } |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Return Doctrine/Mysql-compatible column default values for MariaDB 10.2.7+ servers. |
||
217 | * |
||
218 | * - Since MariaDb 10.2.7 column defaults stored in information_schema are now quoted |
||
219 | * to distinguish them from expressions (see MDEV-10134 for a what is an expression). |
||
220 | * - CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE are stored in information_schema |
||
221 | * as current_timestamp(), currdate(), currtime() |
||
222 | * - Literal escaping is normalized in information schema (store "''" instead of "\'") |
||
223 | * - Note: columnDefault should only be null when column is not_nullable otherwise |
||
224 | * a string 'NULL' should be given. Unfortunately, it's not 100% correct in case of |
||
225 | * upgrade from previous versions: see https://jira.mariadb.org/browse/MDEV-14053. |
||
226 | * Otherwise we could simplify the condition === 'NULL' || === null by adding the parameter |
||
227 | * is_nullable. |
||
228 | * |
||
229 | * @link https://mariadb.com/kb/en/library/information-schema-columns-table/ |
||
230 | * @link https://jira.mariadb.org/browse/MDEV-13132 |
||
231 | * @link https://jira.mariadb.org/browse/MDEV-14053 |
||
232 | * |
||
233 | * @param null|string $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7 |
||
234 | */ |
||
235 | private function getMariaDb1027ColumnDefault(MariaDb1027Platform $platform, ?string $columnDefault) : ?string { |
||
236 | |||
237 | if ($columnDefault === 'NULL' || $columnDefault === null) { |
||
238 | return null; |
||
239 | } |
||
240 | if ($columnDefault[0] === "'") { |
||
241 | return stripslashes( |
||
242 | str_replace("''", "'", |
||
243 | preg_replace('/^\'(.*)\'$/', '$1', $columnDefault) |
||
244 | ) |
||
245 | ); |
||
246 | } |
||
247 | switch($columnDefault) { |
||
248 | case 'current_timestamp()': |
||
249 | return $platform->getCurrentTimestampSQL(); |
||
250 | case 'curdate()': |
||
251 | return $platform->getCurrentDateSQL(); |
||
252 | case 'curtime()': |
||
253 | return $platform->getCurrentTimeSQL(); |
||
254 | } |
||
255 | return $columnDefault; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 1 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
|
301 | } |
||
302 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.