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() |
||
| 135 | { |
||
| 136 | 1 | $options = $this->grid->getClientSideOptions(); |
|
| 137 | 1 | if (!isset($options['editable'])) { //only once |
|
| 138 | 1 | $this->grid->setClientSideOptions(['editable' => TRUE]); |
|
| 139 | 1 | $this->grid->onRender[] = function(\Grido\Grid $grid) |
|
| 140 | { |
||
| 141 | 1 | foreach ($grid->getComponent(Column::ID)->getComponents() as $column) { |
|
|
|
|||
| 142 | 1 | if (!$column instanceof Editable || !$column->isEditable()) { |
|
| 143 | 1 | continue; |
|
| 144 | } |
||
| 145 | |||
| 146 | 1 | $colDb = $column->getColumn(); |
|
| 147 | 1 | $colName = $column->getName(); |
|
| 148 | 1 | $isMissing = function ($method) use ($grid) { |
|
| 149 | 1 | return $grid->model instanceof \Grido\DataSources\Model |
|
| 150 | 1 | ? !method_exists($grid->model->dataSource, $method) |
|
| 151 | 1 | : TRUE; |
|
| 152 | 1 | }; |
|
| 153 | |||
| 154 | 1 | if (($column->editableCallback === NULL && (!is_string($colDb) || strpos($colDb, '.'))) || |
|
| 155 | 1 | ($column->editableCallback === NULL && $isMissing('update')) |
|
| 156 | 1 | ) { |
|
| 157 | 1 | $msg = "Column '$colName' has error: You must define callback via setEditableCallback()."; |
|
| 158 | 1 | throw new Exception($msg); |
|
| 159 | } |
||
| 160 | |||
| 161 | 1 | if ($column->editableRowCallback === NULL && $column->customRender && $isMissing('getRow')) { |
|
| 162 | 1 | $msg = "Column '$colName' has error: You must define callback via setEditableRowCallback()."; |
|
| 163 | 1 | throw new Exception($msg); |
|
| 164 | } |
||
| 165 | 1 | } |
|
| 166 | 1 | }; |
|
| 167 | 1 | } |
|
| 168 | 1 | } |
|
| 169 | |||
| 170 | /**********************************************************************************************/ |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns header cell prototype (<th> html tag). |
||
| 174 | * @return \Nette\Utils\Html |
||
| 175 | */ |
||
| 176 | public function getHeaderPrototype() |
||
| 177 | { |
||
| 178 | 1 | $th = parent::getHeaderPrototype(); |
|
| 179 | |||
| 180 | 1 | if ($this->isEditable()) { |
|
| 181 | 1 | $th->setAttribute('data-grido-editable-handler', $this->link('editable!')); |
|
| 182 | 1 | $th->setAttribute('data-grido-editableControl-handler', $this->link('editableControl!')); |
|
| 183 | 1 | } |
|
| 184 | |||
| 185 | 1 | return $th; |
|
| 186 | } |
||
| 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) |
||
| 194 | { |
||
| 195 | 1 | $td = parent::getCellPrototype($row); |
|
| 196 | |||
| 197 | 1 | if ($this->isEditable() && $row !== NULL) { |
|
| 198 | 1 | if (!in_array('editable', $td->class)) { |
|
| 199 | 1 | $td->class[] = 'editable'; |
|
| 200 | 1 | } |
|
| 201 | |||
| 202 | 1 | $value = $this->editableValueCallback === NULL |
|
| 203 | 1 | ? $this->getValue($row) |
|
| 204 | 1 | : call_user_func_array($this->editableValueCallback, [$row, $this]); |
|
| 205 | |||
| 206 | 1 | $td->setAttribute('data-grido-editable-value', $value); |
|
| 207 | 1 | } |
|
| 208 | |||
| 209 | 1 | return $td; |
|
| 210 | } |
||
| 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: