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 JsonDbStructure 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 JsonDbStructure, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class JsonDbStructure |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | * |
||
| 27 | * Names of database objects that can be manipulated |
||
| 28 | * using major DDL keywords such as 'create', 'alter' |
||
| 29 | * and 'drop' |
||
| 30 | */ |
||
| 31 | private $topLevelObjects = [ |
||
| 32 | ':database', |
||
| 33 | ':table', |
||
| 34 | ':table-group', |
||
| 35 | ':view', |
||
| 36 | ':index', |
||
| 37 | ':trigger', |
||
| 38 | ':function', |
||
| 39 | ':stored-procedure', |
||
| 40 | ':storage', |
||
| 41 | ':security', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | * An array of object definers. |
||
| 47 | * |
||
| 48 | * Object Definers are Special keywords that accepts array values |
||
| 49 | * in a json structure file definition |
||
| 50 | */ |
||
| 51 | private $objectDefiners = [ |
||
| 52 | 'columns', |
||
| 53 | 'add-column', |
||
| 54 | 'foreign-key', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | * Special Characters used in jsyn files. |
||
| 60 | * |
||
| 61 | * Characters which have a special meaning such as braces and |
||
| 62 | * square brackets are listed in this array |
||
| 63 | */ |
||
| 64 | private $specialCharacters = [ |
||
| 65 | 'left-curly-brace' => '{', |
||
| 66 | 'right-curly-brace' => '}', |
||
| 67 | 'left-square-bracket' => '[', |
||
| 68 | 'right-square-bracket' => ']', |
||
| 69 | 'left-bracket' => '(', |
||
| 70 | 'right-bracket' => ')', |
||
| 71 | ]; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $crudActionKeyword = ':crud-action'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $objectGroupKeyword = '-group'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $jsynExtension = '.jsyn'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | private $jsynDirectory = __DIR__.'/bin/'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var null | array |
||
| 95 | */ |
||
| 96 | private $jsonStructure; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var null | string |
||
| 100 | */ |
||
| 101 | private $sqlVendor; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $generatedSql = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $jsonStructureFile PathUtil | string | Array |
||
| 110 | * @param $sqlVendor string |
||
| 111 | */ |
||
| 112 | public function __construct($jsonStructureFile, $sqlVendor = 'default') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $jsynDirectory string |
||
| 124 | * |
||
| 125 | * @return void |
||
| 126 | */ |
||
| 127 | public function setJsynDirectory($jsynDirectory) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $sqlVendor string |
||
| 134 | * |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function setSqlVendor($sqlVendor) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $topLevelObject string |
||
| 144 | * @param $crudAction string |
||
| 145 | * |
||
| 146 | * Based on the values provided in the $topLevelObject and $crudAction |
||
| 147 | * variables, this method tries to derive the name of the jsyn file to use |
||
| 148 | * for parsing. |
||
| 149 | * |
||
| 150 | * @return string | bool |
||
| 151 | */ |
||
| 152 | private function guessJsynFileName($topLevelObject, $crudAction) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param $jsonFile PathUtil | string |
||
| 165 | * |
||
| 166 | * Gets the content of a json file, decodes it and |
||
| 167 | * returns an array of the decoded json. |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | private function getObjectFromJsonFile($jsonFile) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param $jsonStructure array |
||
| 180 | * |
||
| 181 | * Tries to get the top level object from an array of |
||
| 182 | * a json structure, returns false if no top level object |
||
| 183 | * is found. |
||
| 184 | * |
||
| 185 | * @return string | bool |
||
| 186 | */ |
||
| 187 | private function getProvidedTopLevelObject($jsonStructure) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $jsonStructure array |
||
| 200 | * |
||
| 201 | * Determines if a top level object is a valid one by checking |
||
| 202 | * the $topLevelObjects array to see if its present. |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | private function isValidTopLevelObject($jsonStructure) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param $objectIdentifier string |
||
| 219 | * |
||
| 220 | * Strips a supplied $objectIdentifier string variable of |
||
| 221 | * special characters and returns a new string with only alphanumeric |
||
| 222 | * characters. |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | private function objectIdentifierToString($objectIdentifier) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param $jsonStructure array |
||
| 233 | * |
||
| 234 | * Converts a $jsonStructure array into a string containing valid |
||
| 235 | * sql statements. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function generateSqlFromStructure($jsonStructure) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param $jsonStructures array |
||
| 298 | * @param $objectDefiner string |
||
| 299 | * |
||
| 300 | * While the {@link generateSqlFromStructure()} method above generates sql string |
||
| 301 | * from only valid top level objects, this method generates sql statements from valid |
||
| 302 | * object definers. Accepts an $objectDefiner and a $jsonStructure array as parameters. |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function generateSqlFromObjectDefiner($jsonStructures, $objectDefiner) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param $encloserPre string |
||
| 367 | * @param $encloserPost string |
||
| 368 | * @param $enclosee string |
||
| 369 | * |
||
| 370 | * Checks to see if a string ($enclosee) is enclosed by special characters |
||
| 371 | * such as '{' and '}' and '[' and ']'. |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | private function enclosed($encloserPre, $encloserPost, $enclosee) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Parses a jsonStructure in global scope and assigns |
||
| 386 | * a generated array to either of the sql string generator methods |
||
| 387 | * depending on the top level objects or object definers. |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | public function parseStructure() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param $jsonStructure array |
||
| 433 | * |
||
| 434 | * Determines if another top level object or object definer is |
||
| 435 | * present within the supplied json structure. |
||
| 436 | * Returns the name of the object if found and false if not found. |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function isAnotherObjectPresent($jsonStructure) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param $delimiter string |
||
| 451 | * |
||
| 452 | * Returns the parsed and generated string containing the sql |
||
| 453 | * statement delimited by a value supplied in the $delimiter |
||
| 454 | * parameter. |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public function getGeneratedSql($delimiter = ";\n") |
||
| 462 | } |
||
| 463 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..