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 AuditDiff 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 AuditDiff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class AuditDiff |
||
| 21 | { |
||
| 22 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 23 | /** |
||
| 24 | * The metadata (additional) audit columns (as stored in the config file). |
||
| 25 | * |
||
| 26 | * @var TableColumnsMetadata |
||
| 27 | */ |
||
| 28 | private $auditColumnsMetadata; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The names of all tables in audit schema. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $auditSchemaTables; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The content of the configuration file. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $config; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The names of all tables in data schema. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $dataSchemaTables; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Array with columns for each table. |
||
| 53 | * array [ |
||
| 54 | * table_name [ |
||
| 55 | * column [ |
||
| 56 | * data table type, |
||
| 57 | * audit table type |
||
| 58 | * ], |
||
| 59 | * ... |
||
| 60 | * ] |
||
| 61 | * ] |
||
| 62 | * |
||
| 63 | * @var array[] |
||
| 64 | */ |
||
| 65 | private $diffColumns; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * If set all tables and columns are shown. |
||
| 69 | * |
||
| 70 | * @var boolean |
||
| 71 | */ |
||
| 72 | private $full; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The Input interface. |
||
| 76 | * |
||
| 77 | * @var InputInterface |
||
| 78 | */ |
||
| 79 | private $input; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The Output decorator. |
||
| 83 | * |
||
| 84 | * @var StratumStyle |
||
| 85 | */ |
||
| 86 | private $io; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The Output interface. |
||
| 90 | * |
||
| 91 | * @var OutputInterface |
||
| 92 | */ |
||
| 93 | private $output; |
||
| 94 | |||
| 95 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 96 | /** |
||
| 97 | * Object constructor. |
||
| 98 | * |
||
| 99 | * @param array[] $config The content of the configuration file. |
||
| 100 | * @param StratumStyle $io The Output decorator. |
||
| 101 | * @param InputInterface $input |
||
| 102 | * @param OutputInterface $output |
||
| 103 | */ |
||
| 104 | public function __construct(&$config, $io, $input, $output) |
||
| 111 | |||
| 112 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 113 | /** |
||
| 114 | * Check full full and return array without new or obsolete columns if full not set. |
||
| 115 | * |
||
| 116 | * @param array[] $columns The metadata of the columns of a table. |
||
| 117 | * |
||
| 118 | * @return array[] |
||
| 119 | */ |
||
| 120 | private static function removeMatchingColumns($columns) |
||
| 152 | |||
| 153 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 154 | /** |
||
| 155 | * The main method: executes the auditing actions for tables. |
||
| 156 | * |
||
| 157 | * @return int The exit status. |
||
| 158 | */ |
||
| 159 | public function main() |
||
| 187 | |||
| 188 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 189 | /** |
||
| 190 | * Add not null to audit columns if it not nullable. |
||
| 191 | * |
||
| 192 | * @param array $theColumns Audit columns. |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | private function addNotNull($theColumns) |
||
| 215 | |||
| 216 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 217 | /** |
||
| 218 | * Get the difference between data and audit tables. |
||
| 219 | * |
||
| 220 | * @param TableColumnsMetadata $dataColumns The table columns from data schema. |
||
| 221 | * @param TableColumnsMetadata $auditColumns The table columns from audit schema. |
||
| 222 | * |
||
| 223 | * @return \array[] |
||
| 224 | */ |
||
| 225 | private function createDiffArray($dataColumns, $auditColumns) |
||
| 231 | |||
| 232 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 233 | /** |
||
| 234 | * Writes the difference between the audit tables and metadata tables to the output. |
||
| 235 | */ |
||
| 236 | private function diffTables() |
||
| 251 | |||
| 252 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 253 | /** |
||
| 254 | * Computes the difference between data and audit tables. |
||
| 255 | */ |
||
| 256 | private function getDiff() |
||
| 275 | |||
| 276 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 277 | /** |
||
| 278 | * Getting list of all tables from information_schema of database from config file. |
||
| 279 | */ |
||
| 280 | private function listOfTables() |
||
| 286 | |||
| 287 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 288 | /** |
||
| 289 | * Writes the difference between the audit and data tables to the output. |
||
| 290 | */ |
||
| 291 | private function printDiff() |
||
| 337 | |||
| 338 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 339 | /** |
||
| 340 | * Resolves the canonical column types of the audit table columns. |
||
| 341 | */ |
||
| 342 | View Code Duplication | private function resolveCanonicalAuditColumns() |
|
| 372 | |||
| 373 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 374 | } |
||
| 375 | |||
| 377 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.