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 |
||
29 | abstract class Editable extends Column |
||
30 | 1 | { |
|
31 | /** @var bool */ |
||
32 | protected $editable = FALSE; |
||
33 | |||
34 | /** @var bool */ |
||
35 | protected $editableDisabled = FALSE; |
||
36 | |||
37 | /** @var \Nette\Forms\IControl Custom control for inline editing */ |
||
38 | protected $editableControl; |
||
39 | |||
40 | /** @var callback for custom handling with edited data; function($id, $newValue, $oldValue, Editable $column) {} */ |
||
41 | protected $editableCallback; |
||
42 | |||
43 | /** @var callback for custom value; function($row, Columns\Editable $column) {} */ |
||
44 | protected $editableValueCallback; |
||
45 | |||
46 | /** @var callback for getting row; function($row, Columns\Editable $column) {} */ |
||
47 | protected $editableRowCallback; |
||
48 | |||
49 | /** |
||
50 | * Sets column as editable. |
||
51 | * @param callback $callback function($id, $newValue, $oldValue, Columns\Editable $column) {} |
||
52 | * @param \Nette\Forms\IControl $control |
||
53 | * @return Editable |
||
54 | */ |
||
55 | public function setEditable($callback = NULL, \Nette\Forms\IControl $control = NULL) |
||
56 | { |
||
57 | 1 | $this->editable = TRUE; |
|
58 | 1 | $this->setClientSideOptions(); |
|
59 | |||
60 | 1 | $callback && $this->setEditableCallback($callback); |
|
61 | 1 | $control && $this->setEditableControl($control); |
|
62 | |||
63 | 1 | return $this; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Sets control for inline editation. |
||
68 | * @param \Nette\Forms\IControl $control |
||
69 | * @return Editable |
||
70 | */ |
||
71 | public function setEditableControl(\Nette\Forms\IControl $control) |
||
72 | { |
||
73 | 1 | $this->isEditable() ?: $this->setEditable(); |
|
74 | 1 | $this->editableControl = $control; |
|
75 | |||
76 | 1 | return $this; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Sets editable callback. |
||
81 | * @param callback $callback function($id, $newValue, $oldValue, Columns\Editable $column) {} |
||
82 | * @return Editable |
||
83 | */ |
||
84 | public function setEditableCallback($callback) |
||
85 | { |
||
86 | 1 | $this->isEditable() ?: $this->setEditable(); |
|
87 | 1 | $this->editableCallback = $callback; |
|
88 | |||
89 | 1 | return $this; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Sets editable value callback. |
||
94 | * @param callback $callback for custom value; function($row, Columns\Editable $column) {} |
||
95 | * @return Editable |
||
96 | */ |
||
97 | public function setEditableValueCallback($callback) |
||
98 | { |
||
99 | 1 | $this->isEditable() ?: $this->setEditable(); |
|
100 | 1 | $this->editableValueCallback = $callback; |
|
101 | |||
102 | 1 | return $this; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Sets editable row callback - it's required when used editable collumn with customRenderCallback |
||
107 | * @param callback $callback for getting row; function($id, Columns\Editable $column) {} |
||
108 | * @return Editable |
||
109 | */ |
||
110 | public function setEditableRowCallback($callback) |
||
111 | { |
||
112 | 1 | $this->isEditable() ?: $this->setEditable(); |
|
113 | 1 | $this->editableRowCallback = $callback; |
|
114 | |||
115 | 1 | return $this; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return Editable |
||
120 | */ |
||
121 | public function disableEditable() |
||
122 | { |
||
123 | 1 | $this->editable = FALSE; |
|
124 | 1 | $this->editableDisabled = TRUE; |
|
125 | |||
126 | 1 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @throws Exception |
||
131 | */ |
||
132 | protected function setClientSideOptions() |
||
133 | { |
||
134 | 1 | $options = $this->grid->getClientSideOptions(); |
|
135 | 1 | if (!isset($options['editable'])) { //only once |
|
136 | 1 | $this->grid->setClientSideOptions(['editable' => TRUE]); |
|
137 | 1 | $this->grid->onRender[] = function(\Grido\Grid $grid) |
|
138 | { |
||
139 | foreach ($grid->getComponent(Column::ID)->getComponents() as $column) { |
||
|
|||
140 | if (!$column instanceof Editable || !$column->isEditable()) { |
||
141 | continue; |
||
142 | } |
||
143 | |||
144 | $colDb = $column->getColumn(); |
||
145 | $colName = $column->getName(); |
||
146 | $isMissing = function ($method) use ($grid) { |
||
147 | return $grid->model instanceof \Grido\DataSources\Model |
||
148 | ? !method_exists($grid->model->dataSource, $method) |
||
149 | : TRUE; |
||
150 | }; |
||
151 | |||
152 | if (($column->editableCallback === NULL && (!is_string($colDb) || strpos($colDb, '.'))) || |
||
153 | ($column->editableCallback === NULL && $isMissing('update')) |
||
154 | ) { |
||
155 | $msg = "Column '$colName' has error: You must define callback via setEditableCallback()."; |
||
156 | throw new Exception($msg); |
||
157 | } |
||
158 | |||
159 | if ($column->editableRowCallback === NULL && $column->customRender && $isMissing('getRow')) { |
||
160 | $msg = "Column '$colName' has error: You must define callback via setEditableRowCallback()."; |
||
161 | throw new Exception($msg); |
||
162 | } |
||
163 | } |
||
164 | }; |
||
165 | 1 | } |
|
166 | 1 | } |
|
167 | |||
168 | /**********************************************************************************************/ |
||
169 | |||
170 | /** |
||
171 | * Returns header cell prototype (<th> html tag). |
||
172 | * @return \Nette\Utils\Html |
||
173 | */ |
||
174 | public function getHeaderPrototype() |
||
175 | { |
||
176 | $th = parent::getHeaderPrototype(); |
||
177 | |||
178 | if ($this->isEditable()) { |
||
179 | $th->data['grido-editable-handler'] = $this->link('editable!'); |
||
180 | $th->data['grido-editableControl-handler'] = $this->link('editableControl!'); |
||
181 | } |
||
182 | |||
183 | return $th; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns cell prototype (<td> html tag). |
||
188 | * @param mixed $row |
||
189 | * @return \Nette\Utils\Html |
||
190 | */ |
||
191 | public function getCellPrototype($row = NULL) |
||
192 | { |
||
193 | $td = parent::getCellPrototype($row); |
||
194 | |||
195 | if ($this->isEditable() && $row !== NULL) { |
||
196 | if (!in_array('editable', $td->class)) { |
||
197 | $td->class[] = 'editable'; |
||
198 | } |
||
199 | |||
200 | $td->data['grido-editable-value'] = $this->editableValueCallback === NULL |
||
201 | ? $this->getValue($row) |
||
202 | : call_user_func_array($this->editableValueCallback, [$row, $this]); |
||
203 | } |
||
204 | |||
205 | return $td; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Returns control for editation. |
||
210 | * @returns \Nette\Forms\Controls\TextInput |
||
211 | */ |
||
212 | public function getEditableControl() |
||
213 | { |
||
214 | 1 | if ($this->editableControl === NULL) { |
|
215 | 1 | $this->editableControl = new \Nette\Forms\Controls\TextInput; |
|
216 | 1 | $this->editableControl->controlPrototype->class[] = 'form-control'; |
|
217 | 1 | } |
|
218 | |||
219 | 1 | return $this->editableControl; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return callback |
||
224 | * @internal |
||
225 | */ |
||
226 | public function getEditableCallback() |
||
230 | |||
231 | /** |
||
232 | * @return callback |
||
233 | * @internal |
||
234 | */ |
||
235 | public function getEditableValueCallback() |
||
239 | |||
240 | /** |
||
241 | * @return callback |
||
242 | * @internal |
||
243 | */ |
||
244 | public function getEditableRowCallback() |
||
248 | |||
249 | /** |
||
250 | * @return bool |
||
251 | * @internal |
||
252 | */ |
||
253 | public function isEditable() |
||
257 | |||
258 | /** |
||
259 | * @return bool |
||
260 | * @internal |
||
261 | */ |
||
262 | public function isEditableDisabled() |
||
266 | |||
267 | /**********************************************************************************************/ |
||
268 | |||
269 | /** |
||
270 | * @internal |
||
271 | */ |
||
272 | public function handleEditable($id, $newValue, $oldValue) |
||
273 | { |
||
297 | |||
298 | /** |
||
299 | * @internal |
||
300 | */ |
||
301 | public function handleEditableControl($value) |
||
317 | } |
||
318 |
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: