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 Row 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 Row, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Row extends Nette\Object |
||
| 21 | 1 | { |
|
| 22 | |||
| 23 | 1 | /** |
|
| 24 | * @var DataGrid |
||
| 25 | */ |
||
| 26 | protected $datagrid; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var mixed |
||
| 30 | */ |
||
| 31 | protected $item; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $primary_key; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var mixed |
||
| 40 | */ |
||
| 41 | protected $id; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Html |
||
| 45 | */ |
||
| 46 | protected $control; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param DataGrid $datagrid |
||
| 51 | * @param mixed $item |
||
| 52 | * @param string $primary_key |
||
| 53 | */ |
||
| 54 | public function __construct(DataGrid $datagrid, $item, $primary_key) |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Get id value of item |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public function getId() |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Get item value of key |
||
| 80 | * @param mixed $key |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | public function getValue($key) |
||
| 84 | { |
||
| 85 | 1 | if ($this->item instanceof LeanMapper\Entity) { |
|
| 86 | 1 | return $this->getLeanMapperEntityProperty($this->item, $key); |
|
| 87 | |||
| 88 | 1 | } else if ($this->item instanceof Nextras\Orm\Entity\Entity) { |
|
| 89 | return $this->getNextrasEntityProperty($this->item, $key); |
||
| 90 | |||
| 91 | 1 | } else if ($this->item instanceof DibiRow) { |
|
|
|
|||
| 92 | return $this->item->{$this->formatDibiRowKey($key)}; |
||
| 93 | |||
| 94 | 1 | } else if ($this->item instanceof ActiveRow) { |
|
| 95 | return $this->getActiveRowProperty($this->item, $key); |
||
| 96 | |||
| 97 | 1 | } else if ($this->item instanceof Nette\Database\Row) { |
|
| 98 | return $this->item->{$key}; |
||
| 99 | |||
| 100 | 1 | } else if (is_array($this->item)) { |
|
| 101 | 1 | return $this->item[$key]; |
|
| 102 | |||
| 103 | } else { |
||
| 104 | /** |
||
| 105 | * Doctrine entity |
||
| 106 | */ |
||
| 107 | 1 | return $this->getDoctrineEntityProperty($this->item, $key); |
|
| 108 | |||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @return Html |
||
| 115 | */ |
||
| 116 | public function getControl() |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getControlClass() |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @param ActiveRow $item |
||
| 137 | * @param string $key |
||
| 138 | * @return mixed|NULL |
||
| 139 | */ |
||
| 140 | public function getActiveRowProperty(ActiveRow $item, $key) |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * LeanMapper: Access object properties to get a item value |
||
| 168 | * @param LeanMapper\Entity $item |
||
| 169 | * @param mixed $key |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key) |
|
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Nextras: Access object properties to get a item value |
||
| 198 | * @param Nextras\Orm\Entity\Entity $item |
||
| 199 | * @param string $key |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function getNextrasEntityProperty(Nextras\Orm\Entity\Entity $item, $key) |
|
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Doctrine: Access object properties to get a item value |
||
| 228 | * @param mixed $item |
||
| 229 | * @param mixed $key |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function getDoctrineEntityProperty($item, $key) |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Get original item |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | public function getItem() |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Has particular row group actions allowed? |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function hasGroupAction() |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Has particular row a action allowed? |
||
| 281 | * @param mixed $key |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function hasAction($key) |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Has particular row inlie edit allowed? |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function hasInlineEdit() |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $key |
||
| 306 | * @param Column\Column $column |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function applyColumnCallback($key, Column\Column $column) |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * Key may contain ".", get rid of it (+ the table alias) |
||
| 323 | * |
||
| 324 | * @param string $key |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | private function formatDibiRowKey($key) |
||
| 335 | |||
| 336 | } |
||
| 337 |