Complex classes like CrudAction 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 CrudAction, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class CrudAction extends Action |
||
29 | { |
||
30 | /** |
||
31 | * @var Table |
||
32 | */ |
||
33 | protected $_table = null; |
||
34 | |||
35 | /** |
||
36 | * Object Identifier |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $_id = null; |
||
41 | |||
42 | protected $_idName = 'id'; |
||
43 | |||
44 | /** |
||
45 | * Crud service. |
||
46 | * |
||
47 | * @var CrudService |
||
48 | */ |
||
49 | protected $_service; |
||
50 | |||
51 | /** |
||
52 | * Parent Object Identifier |
||
53 | * |
||
54 | * Used for nested services |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $_parentId = null; |
||
59 | |||
60 | protected $_parentIdName = null; |
||
61 | |||
62 | /** |
||
63 | * Api table finder method |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $_finder = null; |
||
68 | |||
69 | /** |
||
70 | * Action constructor. |
||
71 | * |
||
72 | * @param array $config Configuration options passed to the constructor |
||
73 | */ |
||
74 | 113 | public function __construct(array $config = []) |
|
110 | |||
111 | /** |
||
112 | * Gets a Table instance. |
||
113 | * |
||
114 | * @return Table |
||
115 | */ |
||
116 | 75 | public function getTable() |
|
120 | |||
121 | /** |
||
122 | * Sets the table instance. |
||
123 | * |
||
124 | * @param Table $table A Table instance. |
||
125 | * @return $this |
||
126 | */ |
||
127 | 113 | public function setTable(Table $table) |
|
133 | |||
134 | /** |
||
135 | * Api method for table. |
||
136 | * |
||
137 | * @param Table $table A Table instance. |
||
138 | * @deprecated 3.4.0 Use setTable()/getTable() instead. |
||
139 | * @return Table |
||
140 | */ |
||
141 | public function table($table = null) |
||
154 | |||
155 | /** |
||
156 | * @return CrudService |
||
157 | */ |
||
158 | 113 | public function getService() |
|
162 | |||
163 | /** |
||
164 | * Model id getter. |
||
165 | * |
||
166 | * @return mixed|string |
||
167 | */ |
||
168 | public function getId() |
||
172 | |||
173 | /** |
||
174 | * Model id field name getter. |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function getIdName() |
||
182 | |||
183 | /** |
||
184 | * Parent id getter. |
||
185 | * |
||
186 | * @return mixed|string |
||
187 | */ |
||
188 | 9 | public function getParentId() |
|
192 | |||
193 | /** |
||
194 | * Parent model id field name getter. |
||
195 | * |
||
196 | * @return mixed|string |
||
197 | */ |
||
198 | 9 | public function getParentIdName() |
|
202 | |||
203 | /** |
||
204 | * Builds new entity instance. |
||
205 | * |
||
206 | * @return EntityInterface |
||
207 | */ |
||
208 | 10 | protected function _newEntity() |
|
214 | |||
215 | /** |
||
216 | * Patch entity. |
||
217 | * |
||
218 | * @param EntityInterface $entity An Entity instance. |
||
219 | * @param array $data Entity data. |
||
220 | * @param array $options Patch entity options. |
||
221 | * @return \Cake\Datasource\EntityInterface|mixed |
||
222 | */ |
||
223 | 13 | protected function _patchEntity($entity, $data, $options = []) |
|
233 | |||
234 | /** |
||
235 | * Builds entities list |
||
236 | * |
||
237 | * @return \Cake\Collection\Collection |
||
238 | */ |
||
239 | 33 | protected function _getEntities() |
|
255 | |||
256 | /** |
||
257 | * Returns single entity by id. |
||
258 | * |
||
259 | * @param mixed $primaryKey Primary key. |
||
260 | * @return \Cake\Collection\Collection |
||
261 | */ |
||
262 | 22 | protected function _getEntity($primaryKey) |
|
276 | |||
277 | /** |
||
278 | * Build condition for get entity method. |
||
279 | * |
||
280 | * @param string $primaryKey Primary key |
||
281 | * @return array |
||
282 | */ |
||
283 | 22 | protected function _buildViewCondition($primaryKey) |
|
304 | |||
305 | /** |
||
306 | * Save entity. |
||
307 | * |
||
308 | * @param EntityInterface $entity An Entity instance. |
||
309 | * @return EntityInterface |
||
310 | */ |
||
311 | 8 | protected function _save($entity) |
|
319 | |||
320 | /** |
||
321 | * Describe table with full details. |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | 1 | protected function _describe() |
|
424 | } |
||
425 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.