Complex classes like Editable 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 Editable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class Editable extends Column |
||
| 32 | 1 | { |
|
| 33 | /** @var bool */ |
||
| 34 | protected $editable = FALSE; |
||
| 35 | |||
| 36 | /** @var bool */ |
||
| 37 | protected $editableDisabled = FALSE; |
||
| 38 | |||
| 39 | /** @var \Nette\Forms\IControl Custom control for inline editing */ |
||
| 40 | protected $editableControl; |
||
| 41 | |||
| 42 | /** @var callback for custom handling with edited data; function($id, $newValue, $oldValue, Editable $column) {} */ |
||
| 43 | protected $editableCallback; |
||
| 44 | |||
| 45 | /** @var callback for custom value; function($row, Columns\Editable $column) {} */ |
||
| 46 | protected $editableValueCallback; |
||
| 47 | |||
| 48 | /** @var callback for getting row; function($row, Columns\Editable $column) {} */ |
||
| 49 | protected $editableRowCallback; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Sets column as editable. |
||
| 53 | * @param callback $callback function($id, $newValue, $oldValue, Columns\Editable $column) {} |
||
| 54 | * @param \Nette\Forms\IControl $control |
||
| 55 | * @return Editable |
||
| 56 | */ |
||
| 57 | public function setEditable($callback = NULL, \Nette\Forms\IControl $control = NULL) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets control for inline editation. |
||
| 70 | * @param \Nette\Forms\IControl $control |
||
| 71 | * @return Editable |
||
| 72 | */ |
||
| 73 | public function setEditableControl(\Nette\Forms\IControl $control) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sets editable callback. |
||
| 83 | * @param callback $callback function($id, $newValue, $oldValue, Columns\Editable $column) {} |
||
| 84 | * @return Editable |
||
| 85 | */ |
||
| 86 | public function setEditableCallback($callback) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Sets editable value callback. |
||
| 96 | * @param callback $callback for custom value; function($row, Columns\Editable $column) {} |
||
| 97 | * @return Editable |
||
| 98 | */ |
||
| 99 | public function setEditableValueCallback($callback) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets editable row callback - it's required when used editable collumn with customRenderCallback |
||
| 109 | * @param callback $callback for getting row; function($id, Columns\Editable $column) {} |
||
| 110 | * @return Editable |
||
| 111 | */ |
||
| 112 | public function setEditableRowCallback($callback) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return Editable |
||
| 122 | */ |
||
| 123 | public function disableEditable() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @throws Exception |
||
| 133 | */ |
||
| 134 | protected function setClientSideOptions() |
||
| 169 | |||
| 170 | /**********************************************************************************************/ |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns header cell prototype (<th> html tag). |
||
| 174 | * @return \Nette\Utils\Html |
||
| 175 | */ |
||
| 176 | public function getHeaderPrototype() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns cell prototype (<td> html tag). |
||
| 190 | * @param mixed $row |
||
| 191 | * @return \Nette\Utils\Html |
||
| 192 | */ |
||
| 193 | public function getCellPrototype($row = NULL) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns control for editation. |
||
| 214 | * @returns \Nette\Forms\Controls\TextInput |
||
| 215 | */ |
||
| 216 | public function getEditableControl() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return callback |
||
| 228 | * @internal |
||
| 229 | */ |
||
| 230 | public function getEditableCallback() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return callback |
||
| 237 | * @internal |
||
| 238 | */ |
||
| 239 | public function getEditableValueCallback() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return callback |
||
| 246 | * @internal |
||
| 247 | */ |
||
| 248 | public function getEditableRowCallback() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return bool |
||
| 255 | * @internal |
||
| 256 | */ |
||
| 257 | public function isEditable() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return bool |
||
| 264 | * @internal |
||
| 265 | */ |
||
| 266 | public function isEditableDisabled() |
||
| 270 | |||
| 271 | /**********************************************************************************************/ |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @internal |
||
| 275 | */ |
||
| 276 | public function handleEditable($id, $newValue, $oldValue) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @internal |
||
| 304 | */ |
||
| 305 | public function handleEditableControl($value) |
||
| 321 | } |
||
| 322 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: