Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | 1 | protected function _getPortableTableColumnDefinition($tableColumn) |
|
| 107 | { |
||
| 108 | 1 | $tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
|
| 109 | |||
| 110 | 1 | $dbType = strtolower($tableColumn['type']); |
|
| 111 | 1 | $dbType = strtok($dbType, '(), '); |
|
| 112 | 1 | if (isset($tableColumn['length'])) { |
|
| 113 | $length = $tableColumn['length']; |
||
| 114 | } else { |
||
| 115 | 1 | $length = strtok('(), '); |
|
| 116 | } |
||
| 117 | |||
| 118 | 1 | $fixed = null; |
|
| 119 | |||
| 120 | 1 | if ( ! isset($tableColumn['name'])) { |
|
| 121 | 1 | $tableColumn['name'] = ''; |
|
| 122 | } |
||
| 123 | |||
| 124 | 1 | $scale = null; |
|
| 125 | 1 | $precision = null; |
|
| 126 | |||
| 127 | 1 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
|
| 128 | |||
| 129 | // In cases where not connected to a database DESCRIBE $table does not return 'Comment' |
||
| 130 | 1 | View Code Duplication | if (isset($tableColumn['comment'])) { |
| 131 | 1 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
|
| 132 | 1 | $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
|
| 133 | } |
||
| 134 | |||
| 135 | switch ($dbType) { |
||
| 136 | 1 | case 'char': |
|
| 137 | 1 | case 'binary': |
|
| 138 | $fixed = true; |
||
| 139 | break; |
||
| 140 | 1 | case 'float': |
|
| 141 | 1 | case 'double': |
|
| 142 | 1 | case 'real': |
|
| 143 | 1 | case 'numeric': |
|
| 144 | 1 | case 'decimal': |
|
| 145 | View Code Duplication | 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 | 1 | case 'tinytext': |
|
| 152 | $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT; |
||
| 153 | break; |
||
| 154 | 1 | case 'text': |
|
| 155 | $length = MySqlPlatform::LENGTH_LIMIT_TEXT; |
||
| 156 | break; |
||
| 157 | 1 | case 'mediumtext': |
|
| 158 | $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT; |
||
| 159 | break; |
||
| 160 | 1 | case 'tinyblob': |
|
| 161 | $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB; |
||
| 162 | break; |
||
| 163 | 1 | case 'blob': |
|
| 164 | $length = MySqlPlatform::LENGTH_LIMIT_BLOB; |
||
| 165 | break; |
||
| 166 | 1 | case 'mediumblob': |
|
| 167 | $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB; |
||
| 168 | break; |
||
| 169 | 1 | case 'tinyint': |
|
| 170 | 1 | case 'smallint': |
|
| 171 | 1 | case 'mediumint': |
|
| 172 | 1 | case 'int': |
|
| 173 | 1 | case 'integer': |
|
| 174 | 1 | case 'bigint': |
|
| 175 | 1 | case 'year': |
|
| 176 | 1 | $length = null; |
|
| 177 | 1 | break; |
|
| 178 | } |
||
| 179 | |||
| 180 | 1 | $length = ((int) $length === 0) ? null : (int) $length; |
|
| 181 | |||
| 182 | |||
| 183 | 1 | $isNotNull = ($tableColumn['null'] !== 'YES'); |
|
| 184 | |||
| 185 | // For MariaDB >= 10.2.7 |
||
| 186 | 1 | if ($this->_platform instanceof MariaDb102Platform) { |
|
| 187 | 1 | $columnDefault = ($tableColumn['default'] === 'NULL') ? null : $tableColumn['default']; |
|
| 188 | 1 | if ($columnDefault !== null) { |
|
| 189 | // Get an unquoted version |
||
| 190 | 1 | $columnDefault = preg_replace('/^\'(.*)\'$/', '$1', stripcslashes($columnDefault)); |
|
| 191 | } |
||
| 192 | } else { |
||
| 193 | // Regular MySQL |
||
| 194 | 1 | $columnDefault = (isset($tableColumn['default'])) ? $tableColumn['default'] : null; |
|
| 195 | } |
||
| 196 | |||
| 197 | $options = [ |
||
| 198 | 1 | 'length' => $length, |
|
| 199 | 1 | 'unsigned' => (bool) (strpos($tableColumn['type'], 'unsigned') !== false), |
|
| 200 | 1 | 'fixed' => (bool) $fixed, |
|
| 201 | 1 | 'default' => $columnDefault, |
|
| 202 | 1 | 'notnull' => $isNotNull, |
|
| 203 | 'scale' => null, |
||
| 204 | 'precision' => null, |
||
| 205 | 1 | 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false), |
|
| 206 | 1 | 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
|
| 207 | 1 | ? $tableColumn['comment'] |
|
| 208 | : null, |
||
| 209 | ]; |
||
| 210 | |||
| 211 | 1 | View Code Duplication | if ($scale !== null && $precision !== null) { |
| 212 | $options['scale'] = $scale; |
||
| 213 | $options['precision'] = $precision; |
||
| 214 | } |
||
| 215 | |||
| 216 | 1 | $column = new Column($tableColumn['field'], Type::getType($type), $options); |
|
| 217 | |||
| 218 | 1 | if (isset($tableColumn['collation'])) { |
|
| 219 | 1 | $column->setPlatformOption('collation', $tableColumn['collation']); |
|
| 220 | } |
||
| 221 | |||
| 222 | 1 | return $column; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 1 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
|
| 268 | } |
||
| 269 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.