Complex classes like AbstractCrudObject 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 AbstractCrudObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class AbstractCrudObject extends AbstractObject { |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | const FIELD_ID = 'id'; |
||
34 | /** |
||
35 | * @var string[] set of fields to read by default |
||
36 | */ |
||
37 | protected static $defaultReadFields = array(); |
||
38 | /** |
||
39 | * @var array set of fields that have been mutated |
||
40 | */ |
||
41 | protected $changedFields = array(); |
||
42 | /** |
||
43 | * @var Api instance of the Api used by this object |
||
44 | */ |
||
45 | protected $api; |
||
46 | /** |
||
47 | * @var string ID of the adaccount this object belongs to |
||
48 | */ |
||
49 | protected $parentId; |
||
50 | /** |
||
51 | * @param string $id Optional (do not set for new objects) |
||
52 | * @param string $parent_id Optional, needed for creating new objects. |
||
53 | * @param Api $api The Api instance this object should use to make calls |
||
54 | */ |
||
55 | public function __construct($id = null, $parent_id = null, Api $api = null) { |
||
61 | /** |
||
62 | * @param string $id |
||
63 | */ |
||
64 | public function setId($id) { |
||
68 | /** |
||
69 | * @param string $parent_id |
||
70 | */ |
||
71 | public function setParentId($parent_id) { |
||
74 | /** |
||
75 | * @param Api $api The Api instance this object should use to make calls |
||
76 | */ |
||
77 | public function setApi(Api $api) { |
||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | protected function getEndpoint() { |
||
87 | /** |
||
88 | * @param Api|null $instance |
||
89 | * @return Api |
||
90 | * @throws \InvalidArgumentException |
||
91 | */ |
||
92 | protected static function assureApi(Api $instance = null) { |
||
101 | /** |
||
102 | * @return string|null |
||
103 | */ |
||
104 | public function getParentId() { |
||
107 | /** |
||
108 | * @return string |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | protected function assureParentId() { |
||
117 | /** |
||
118 | * @return string |
||
119 | * @throws \Exception |
||
120 | */ |
||
121 | protected function assureId() { |
||
127 | /** |
||
128 | * @return Api |
||
129 | */ |
||
130 | public function getApi() { |
||
133 | /** |
||
134 | * Get the values which have changed |
||
135 | * |
||
136 | * @return array Key value pairs of changed variables |
||
137 | */ |
||
138 | public function getChangedValues() { |
||
141 | /** |
||
142 | * Get the name of the fields that have changed |
||
143 | * |
||
144 | * @return array Array of changed field names |
||
145 | */ |
||
146 | public function getChangedFields() { |
||
149 | /** |
||
150 | * Get the values which have changed, converting them to scalars |
||
151 | */ |
||
152 | public function exportData() { |
||
159 | /** |
||
160 | * @return void |
||
161 | */ |
||
162 | protected function clearHistory() { |
||
165 | /** |
||
166 | * @param string $name |
||
167 | * @param mixed $value |
||
168 | */ |
||
169 | public function __set($name, $value) { |
||
176 | /** |
||
177 | * @param string[] $fields |
||
178 | */ |
||
179 | public static function setDefaultReadFields(array $fields = array()) { |
||
182 | /** |
||
183 | * @return string[] |
||
184 | */ |
||
185 | public static function getDefaultReadFields() { |
||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | protected function getNodePath() { |
||
194 | /** |
||
195 | * Create function for the object. |
||
196 | * |
||
197 | * @param array $params Additional parameters to include in the request |
||
198 | * @return $this |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | public function create(array $params = array()) { |
||
226 | /** |
||
227 | * Read object data from the graph |
||
228 | * |
||
229 | * @param string[] $fields Fields to request |
||
230 | * @param array $params Additional request parameters |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function read(array $fields = array(), array $params = array()) { |
||
246 | /** |
||
247 | * Update the object. Function parameters are similar with the create function |
||
248 | * |
||
249 | * @param array $params Update parameters in assoc |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function update(array $params = array()) { |
||
260 | /** |
||
261 | * Delete this object from the graph |
||
262 | * |
||
263 | * @param array $params |
||
264 | * @return void |
||
265 | */ |
||
266 | public function delete(array $params = array()) { |
||
272 | /** |
||
273 | * Perform object upsert |
||
274 | * |
||
275 | * Helper function which determines whether an object should be created or |
||
276 | * updated |
||
277 | * |
||
278 | * @param array $params |
||
279 | * @return $this |
||
280 | */ |
||
281 | public function save(array $params = array()) { |
||
288 | /** |
||
289 | * @param string $prototype_class |
||
290 | * @param string $endpoint |
||
291 | * @return string |
||
292 | * @throws \InvalidArgumentException |
||
293 | */ |
||
294 | protected function assureEndpoint($prototype_class, $endpoint) { |
||
305 | /** |
||
306 | * @param array $fields |
||
307 | * @param array $params |
||
308 | * @param string $prototype_class |
||
309 | * @param string|null $endpoint |
||
310 | * @return ResponseInterface |
||
311 | */ |
||
312 | protected function fetchConnection( |
||
327 | /** |
||
328 | * Read a single connection object |
||
329 | * |
||
330 | * @param string $prototype_class |
||
331 | * @param array $fields Fields to request |
||
332 | * @param array $params Additional filters for the reading |
||
333 | * @param string|null $endpoint |
||
334 | * @return AbstractObject |
||
335 | */ |
||
336 | protected function getOneByConnection( |
||
352 | /** |
||
353 | * Read objects from a connection |
||
354 | * |
||
355 | * @param string $prototype_class |
||
356 | * @param array $fields Fields to request |
||
357 | * @param array $params Additional filters for the reading |
||
358 | * @param string|null $endpoint |
||
359 | * @return Cursor |
||
360 | */ |
||
361 | protected function getManyByConnection( |
||
372 | /** |
||
373 | * @param string $job_class |
||
374 | * @param array $fields |
||
375 | * @param array $params |
||
376 | * @return AbstractAsyncJobObject |
||
377 | * @throws \InvalidArgumentException |
||
378 | */ |
||
379 | protected function createAsyncJob( |
||
392 | /** |
||
393 | * Delete objects. |
||
394 | * |
||
395 | * Used batch API calls to delete multiple objects at once |
||
396 | * |
||
397 | * @param string[] $ids Array or single Object ID to delete |
||
398 | * @param Api $api Api Object to use |
||
399 | * @return bool Returns true on success |
||
400 | */ |
||
401 | public static function deleteIds(array $ids, Api $api = null) { |
||
422 | /** |
||
423 | * Read function for the object. Convert fields and filters into the query |
||
424 | * part of uri and return objects. |
||
425 | * |
||
426 | * @param mixed $ids Array or single object IDs |
||
427 | * @param array $fields Array of field names to read |
||
428 | * @param array $params Additional filters for the reading, in assoc |
||
429 | * @param Api $api Api Object to use |
||
430 | * @return Cursor |
||
431 | */ |
||
432 | public static function readIds( |
||
455 | } |
||
456 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.