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 DiffCommand 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 DiffCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DiffCommand extends AuditCommand |
||
| 25 | { |
||
| 26 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 27 | /** |
||
| 28 | * The metadata (additional) audit columns (as stored in the config file). |
||
| 29 | * |
||
| 30 | * @var TableColumnsMetadata |
||
| 31 | */ |
||
| 32 | private $auditColumnsMetadata; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The names of all tables in audit schema. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $auditSchemaTables; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The names of all tables in data schema. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $dataSchemaTables; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Array with columns for each table. |
||
| 50 | * array [ |
||
| 51 | * table_name [ |
||
| 52 | * column [ |
||
| 53 | * data table type, |
||
| 54 | * audit table type |
||
| 55 | * ], |
||
| 56 | * ... |
||
| 57 | * ] |
||
| 58 | * ] |
||
| 59 | * |
||
| 60 | * @var array[] |
||
| 61 | */ |
||
| 62 | private $diffColumns; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * If set all tables and columns are shown. |
||
| 66 | * |
||
| 67 | * @var boolean |
||
| 68 | */ |
||
| 69 | private $full; |
||
| 70 | |||
| 71 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 72 | /** |
||
| 73 | * Check full full and return array without new or obsolete columns if full not set. |
||
| 74 | * |
||
| 75 | * @param array[] $columns The metadata of the columns of a table. |
||
| 76 | * |
||
| 77 | * @return array[] |
||
| 78 | */ |
||
| 79 | private static function removeMatchingColumns($columns) |
||
| 111 | |||
| 112 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 113 | /** |
||
| 114 | * Getting list of all tables from information_schema of database from config file. |
||
| 115 | */ |
||
| 116 | public function listOfTables() |
||
| 122 | |||
| 123 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | protected function configure() |
||
| 134 | |||
| 135 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 174 | |||
| 175 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 176 | /** |
||
| 177 | * Resolves the canonical column types of the audit table columns. |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function resolveCanonicalAuditColumns() |
|
| 209 | |||
| 210 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 211 | /** |
||
| 212 | * Add not null to audit columns if it not nullable. |
||
| 213 | * |
||
| 214 | * @param array $theColumns Audit columns. |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | private function addNotNull($theColumns) |
||
| 237 | |||
| 238 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 239 | /** |
||
| 240 | * Get the difference between data and audit tables. |
||
| 241 | * |
||
| 242 | * @param TableColumnsMetadata $dataColumns The table columns from data schema. |
||
| 243 | * @param TableColumnsMetadata $auditColumns The table columns from audit schema. |
||
| 244 | * |
||
| 245 | * @return \array[] |
||
| 246 | */ |
||
| 247 | private function createDiffArray($dataColumns, $auditColumns) |
||
| 253 | |||
| 254 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 255 | /** |
||
| 256 | * Writes the difference between the audit tables and metadata tables to the output. |
||
| 257 | * |
||
| 258 | * @param OutputInterface $output The output. |
||
| 259 | */ |
||
| 260 | private function diffTables($output) |
||
| 275 | |||
| 276 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 277 | /** |
||
| 278 | * Computes the difference between data and audit tables. |
||
| 279 | */ |
||
| 280 | private function getDiff() |
||
| 299 | |||
| 300 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 301 | /** |
||
| 302 | * Writes the difference between the audit and data tables to the output. |
||
| 303 | * |
||
| 304 | * @param OutputInterface $output The output. |
||
| 305 | */ |
||
| 306 | private function printDiff($output) |
||
| 352 | |||
| 353 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 354 | } |
||
| 355 | |||
| 357 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.