Complex classes like CrudApi 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 CrudApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class CrudApi |
||
8 | { |
||
9 | use DetectsApplicationNamespace; |
||
10 | |||
11 | public $model; |
||
12 | public $instance; |
||
13 | public $namespace; |
||
14 | public $modelHelper; |
||
15 | public $fieldHelper; |
||
16 | |||
17 | /** |
||
18 | * CrudApi Helper. |
||
19 | * @param array $options |
||
20 | */ |
||
21 | public function __construct($options = []) |
||
38 | |||
39 | /** |
||
40 | * Set the Crud API Model Helper. |
||
41 | * @param object $modelHelper Model Helper Object. |
||
42 | */ |
||
43 | public function setModelHelper($modelHelper) |
||
47 | |||
48 | /** |
||
49 | * Set the namespace to search within. |
||
50 | * |
||
51 | * @param string $namespace |
||
52 | */ |
||
53 | public function setNamespace($namespace) |
||
57 | |||
58 | /** |
||
59 | * Set the model to work with. |
||
60 | * |
||
61 | * @param string $model Model name. |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function setModel($model) |
||
71 | |||
72 | /** |
||
73 | * Set a model instance from which to work. |
||
74 | * |
||
75 | * @param Object $item Model instance |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setInstance($item) |
||
85 | |||
86 | /** |
||
87 | * Get Fields |
||
88 | * |
||
89 | * Get a models fillable fields. |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | public function getFields() |
||
107 | |||
108 | /** |
||
109 | * Get a models display name. |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getModelDisplayName() |
||
119 | |||
120 | /** |
||
121 | * Get an instance of a model. |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function getModelInstance() |
||
137 | |||
138 | /** |
||
139 | * Generate a modal for a model. |
||
140 | * @param string $type Modal type (edit, create, delete). |
||
141 | * @return object |
||
142 | */ |
||
143 | public function modal($type) |
||
159 | |||
160 | /** |
||
161 | * Get the modals url. |
||
162 | * |
||
163 | * @param $type |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | private function modalUrl($type) |
||
180 | |||
181 | /** |
||
182 | * Get the name of the javascript method for a model. |
||
183 | * |
||
184 | * @param $method |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function jsMethodName($method) |
||
195 | |||
196 | /** |
||
197 | * Render fields. |
||
198 | * @param $type |
||
199 | * @param array $fields |
||
200 | */ |
||
201 | public function renderFields($type, $fields = []) |
||
238 | |||
239 | public function trimmedModel() |
||
243 | |||
244 | public function getRelatedOptions($relation) |
||
261 | |||
262 | public function getRelatedModel($f) |
||
295 | |||
296 | public function getRelatedDisplay($f) |
||
316 | |||
317 | /** |
||
318 | * Allow certain methods to be passed through to the specified |
||
319 | * helper in order to make methods easier to remember. |
||
320 | * |
||
321 | * @param $method |
||
322 | * @param $args |
||
323 | * |
||
324 | * @method string getRelatedField |
||
325 | * @method string getPrimaryField |
||
326 | * @method bool isIdField |
||
327 | * @method mixed getModel |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function __call($method, $args) |
||
349 | } |
||
350 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: