| Total Complexity | 42 |
| Total Lines | 276 |
| Duplicated Lines | 0 % |
| Coverage | 95.28% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DiffTable 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.
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 DiffTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class DiffTable |
||
| 16 | { |
||
| 17 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 18 | /** |
||
| 19 | * The audit table. |
||
| 20 | * |
||
| 21 | * @var TableMetadata |
||
| 22 | */ |
||
| 23 | private $auditTable; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The data table. |
||
| 27 | * |
||
| 28 | * @var TableMetadata |
||
| 29 | */ |
||
| 30 | private $dataTable; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The join column names of the audit and data table. |
||
| 34 | * |
||
| 35 | * @var array[] |
||
| 36 | */ |
||
| 37 | private $rows = []; |
||
| 38 | |||
| 39 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 40 | /** |
||
| 41 | * Object constructor. |
||
| 42 | * |
||
| 43 | * @param TableMetadata $dataTable The data table. |
||
| 44 | * @param TableMetadata $auditTable The audit table. |
||
| 45 | */ |
||
| 46 | 6 | public function __construct(TableMetadata $dataTable, TableMetadata $auditTable) |
|
| 47 | { |
||
| 48 | 6 | $this->dataTable = $dataTable; |
|
| 49 | 6 | $this->auditTable = $auditTable; |
|
| 50 | 6 | } |
|
| 51 | |||
| 52 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 53 | /** |
||
| 54 | * @param AuditStyle $io The IO object. |
||
| 55 | * @param bool $full If false and only if only differences are shown. |
||
| 56 | */ |
||
| 57 | 6 | public function print(AuditStyle $io, bool $full): void |
|
| 58 | { |
||
| 59 | 6 | $this->rowsEnhanceWithTableColumns(); |
|
| 60 | 6 | $this->rowsEnhanceWithTableOptions(); |
|
| 61 | 6 | $this->rowsEnhanceWithDiffIndicator(); |
|
| 62 | 6 | $this->rowsEnhanceWithColumnTypeInfo(); |
|
| 63 | 6 | $this->rowsEnhanceWithFormatting(); |
|
| 64 | |||
| 65 | 6 | if ($full || $this->hasDifferences()) |
|
| 66 | { |
||
| 67 | 5 | $io->writeln('<dbo>'.$this->dataTable->getTableName().'</dbo>'); |
|
| 68 | |||
| 69 | 5 | $table = new Table($io); |
|
| 70 | 5 | $table->setHeaders(['column', 'audit table', 'config / data table']) |
|
| 71 | 5 | ->setRows($this->getRows($full)); |
|
| 72 | 5 | $table->render(); |
|
| 73 | |||
| 74 | 5 | $io->writeln(''); |
|
| 75 | } |
||
| 76 | 6 | } |
|
| 77 | |||
| 78 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 79 | /** |
||
| 80 | * Returns the rows suitable for Symfony's table. |
||
| 81 | * |
||
| 82 | * @param bool $full If false and only if only differences are shown. |
||
| 83 | * |
||
| 84 | * @return array[] |
||
| 85 | */ |
||
| 86 | 5 | private function getRows(bool $full): array |
|
| 87 | { |
||
| 88 | 5 | $ret = []; |
|
| 89 | 5 | $options = false; |
|
| 90 | 5 | foreach ($this->rows as $row) |
|
| 91 | { |
||
| 92 | // Add separator between columns and options. |
||
| 93 | 5 | if ($options===false && $row['type']=='option') |
|
| 94 | { |
||
| 95 | 5 | if (!empty($ret)) $ret[] = new TableSeparator(); |
|
| 96 | 5 | $options = true; |
|
| 97 | } |
||
| 98 | |||
| 99 | 5 | if ($full || $row['diff']) |
|
| 100 | { |
||
| 101 | 5 | $ret[] = [$row['name'], $row['audit1'], $row['data1']]; |
|
| 102 | |||
| 103 | 5 | if ($row['rowspan']==2) |
|
| 104 | { |
||
| 105 | 3 | $ret[] = ['', $row['audit2'], $row['data2']]; |
|
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | 5 | return $ret; |
|
| 111 | } |
||
| 112 | |||
| 113 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 114 | /** |
||
| 115 | * Returns true if and only if the audit and data tables have differences. |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 6 | private function hasDifferences(): bool |
|
| 127 | } |
||
| 128 | |||
| 129 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 130 | /** |
||
| 131 | * Enhances rows with column type info. |
||
| 132 | */ |
||
| 133 | 6 | private function rowsEnhanceWithColumnTypeInfo(): void |
|
| 162 | } |
||
| 163 | } |
||
| 164 | 6 | } |
|
| 165 | |||
| 166 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 167 | /** |
||
| 168 | * Enhances rows with diff indicator. |
||
| 169 | */ |
||
| 170 | 6 | private function rowsEnhanceWithDiffIndicator(): void |
|
| 171 | { |
||
| 172 | 6 | foreach ($this->rows as &$row) |
|
| 173 | { |
||
| 174 | 6 | switch ($row['type']) |
|
| 175 | { |
||
| 176 | 6 | case 'column': |
|
| 177 | 6 | $row['diff'] = (isset($row['audit'])!==isset($row['data']) || |
|
| 178 | 6 | $row['audit']->getColumnDefinition()!=$row['data']->getColumnDefinition()); |
|
| 179 | 6 | break; |
|
| 180 | |||
| 181 | 6 | case 'option': |
|
| 182 | 6 | $row['diff'] = ($row['audit1']!=$row['data1']); |
|
| 183 | 6 | break; |
|
| 184 | |||
| 185 | default: |
||
| 186 | throw new FallenException('type', $row['type']); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | 6 | } |
|
| 190 | |||
| 191 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 192 | /** |
||
| 193 | * Enhances rows text with formatting. |
||
| 194 | */ |
||
| 195 | 6 | private function rowsEnhanceWithFormatting(): void |
|
| 196 | { |
||
| 197 | 6 | foreach ($this->rows as &$row) |
|
| 198 | { |
||
| 199 | 6 | if ($row['diff']) |
|
| 200 | { |
||
| 201 | 4 | $row['name'] = sprintf('<mm_column>%s</mm_column>', $row['name']); |
|
| 202 | } |
||
| 203 | |||
| 204 | 6 | if ($row['audit1']!=$row['data1']) |
|
| 205 | { |
||
| 206 | 3 | $row['audit1'] = sprintf('<mm_type>%s</mm_type>', $row['audit1']); |
|
| 207 | 3 | $row['data1'] = sprintf('<mm_type>%s</mm_type>', $row['data1']); |
|
| 208 | } |
||
| 209 | |||
| 210 | 6 | if ($row['rowspan']==2 && ($row['audit2']!=$row['data2'])) |
|
| 211 | { |
||
| 212 | 2 | $row['audit2'] = sprintf('<mm_type>%s</mm_type>', $row['audit2']); |
|
| 213 | 2 | $row['data2'] = sprintf('<mm_type>%s</mm_type>', $row['data2']); |
|
| 214 | } |
||
| 215 | } |
||
| 216 | 6 | } |
|
| 217 | |||
| 218 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 219 | /** |
||
| 220 | * Computes the joins columns of the audit and data table. |
||
| 221 | */ |
||
| 222 | 6 | private function rowsEnhanceWithTableColumns(): void |
|
| 260 | } |
||
| 261 | } |
||
| 262 | 6 | } |
|
| 263 | |||
| 264 | //-------------------------------------------------------------------------------------------------------------------- |
||
| 265 | /** |
||
| 266 | * Adds table options to the rows. |
||
| 267 | */ |
||
| 268 | 6 | private function rowsEnhanceWithTableOptions(): void |
|
| 299 |