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 | /** |
||
| 52 | * @deprecated deprecate constructor with null and parent_id |
||
| 53 | * @param string $id Optional (do not set for new objects) |
||
| 54 | * @param string $parent_id Optional, needed for creating new objects. |
||
| 55 | * @param Api $api The Api instance this object should use to make calls |
||
| 56 | */ |
||
| 57 | public function __construct($id = null, $parent_id = null, Api $api = null) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $id |
||
| 89 | */ |
||
| 90 | public function setId($id) { |
||
| 94 | /** |
||
| 95 | * @param string $parent_id |
||
| 96 | */ |
||
| 97 | public function setParentId($parent_id) { |
||
| 100 | /** |
||
| 101 | * @param Api $api The Api instance this object should use to make calls |
||
| 102 | */ |
||
| 103 | public function setApi(Api $api) { |
||
| 107 | /** |
||
| 108 | * @deprecated getEndpoint function is deprecated |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | protected function getEndpoint() { |
||
| 114 | /** |
||
| 115 | * @param Api|null $instance |
||
| 116 | * @return Api |
||
| 117 | * @throws \InvalidArgumentException |
||
| 118 | */ |
||
| 119 | protected static function assureApi(Api $instance = null) { |
||
| 128 | /** |
||
| 129 | * @return string|null |
||
| 130 | */ |
||
| 131 | public function getParentId() { |
||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | protected function assureParentId() { |
||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | * @throws \Exception |
||
| 147 | */ |
||
| 148 | protected function assureId() { |
||
| 154 | /** |
||
| 155 | * @return Api |
||
| 156 | */ |
||
| 157 | public function getApi() { |
||
| 160 | /** |
||
| 161 | * Get the values which have changed |
||
| 162 | * |
||
| 163 | * @return array Key value pairs of changed variables |
||
| 164 | */ |
||
| 165 | public function getChangedValues() { |
||
| 168 | /** |
||
| 169 | * Get the name of the fields that have changed |
||
| 170 | * |
||
| 171 | * @return array Array of changed field names |
||
| 172 | */ |
||
| 173 | public function getChangedFields() { |
||
| 176 | /** |
||
| 177 | * Get the values which have changed, converting them to scalars |
||
| 178 | */ |
||
| 179 | public function exportData() { |
||
| 186 | /** |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | protected function clearHistory() { |
||
| 192 | /** |
||
| 193 | * @param string $name |
||
| 194 | * @param mixed $value |
||
| 195 | */ |
||
| 196 | public function __set($name, $value) { |
||
| 203 | /** |
||
| 204 | * @param string[] $fields |
||
| 205 | */ |
||
| 206 | public static function setDefaultReadFields(array $fields = array()) { |
||
| 209 | /** |
||
| 210 | * @return string[] |
||
| 211 | */ |
||
| 212 | public static function getDefaultReadFields() { |
||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | protected function getNodePath() { |
||
| 221 | /** |
||
| 222 | * Create function for the object. |
||
| 223 | * |
||
| 224 | * @param array $params Additional parameters to include in the request |
||
| 225 | * @return $this |
||
| 226 | * @throws \Exception |
||
| 227 | */ |
||
| 228 | public function create(array $params = array()) { |
||
| 253 | /** |
||
| 254 | * Read object data from the graph |
||
| 255 | * |
||
| 256 | * @param string[] $fields Fields to request |
||
| 257 | * @param array $params Additional request parameters |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | public function read(array $fields = array(), array $params = array()) { |
||
| 273 | /** |
||
| 274 | * Update the object. Function parameters are similar with the create function |
||
| 275 | * |
||
| 276 | * @param array $params Update parameters in assoc |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function update(array $params = array()) { |
||
| 287 | /** |
||
| 288 | * Delete this object from the graph |
||
| 289 | * |
||
| 290 | * @param array $params |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function deleteSelf(array $params = array()) { |
||
| 299 | /** |
||
| 300 | * Perform object upsert |
||
| 301 | * |
||
| 302 | * Helper function which determines whether an object should be created or |
||
| 303 | * updated |
||
| 304 | * |
||
| 305 | * @param array $params |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function save(array $params = array()) { |
||
| 315 | /** |
||
| 316 | * @param string $prototype_class |
||
| 317 | * @param string $endpoint |
||
| 318 | * @return string |
||
| 319 | * @throws \InvalidArgumentException |
||
| 320 | */ |
||
| 321 | protected function assureEndpoint($prototype_class, $endpoint) { |
||
| 332 | /** |
||
| 333 | * @param array $fields |
||
| 334 | * @param array $params |
||
| 335 | * @param string $prototype_class |
||
| 336 | * @param string|null $endpoint |
||
| 337 | * @return ResponseInterface |
||
| 338 | */ |
||
| 339 | protected function fetchConnection( |
||
| 354 | /** |
||
| 355 | * Read a single connection object |
||
| 356 | * |
||
| 357 | * @param string $prototype_class |
||
| 358 | * @param array $fields Fields to request |
||
| 359 | * @param array $params Additional filters for the reading |
||
| 360 | * @param string|null $endpoint |
||
| 361 | * @return AbstractObject |
||
| 362 | */ |
||
| 363 | protected function getOneByConnection( |
||
| 379 | /** |
||
| 380 | * Read objects from a connection |
||
| 381 | * |
||
| 382 | * @param string $prototype_class |
||
| 383 | * @param array $fields Fields to request |
||
| 384 | * @param array $params Additional filters for the reading |
||
| 385 | * @param string|null $endpoint |
||
| 386 | * @return Cursor |
||
| 387 | */ |
||
| 388 | protected function getManyByConnection( |
||
| 399 | /** |
||
| 400 | * @param string $job_class |
||
| 401 | * @param array $fields |
||
| 402 | * @param array $params |
||
| 403 | * @return AbstractAsyncJobObject |
||
| 404 | * @throws \InvalidArgumentException |
||
| 405 | */ |
||
| 406 | protected function createAsyncJob( |
||
| 419 | /** |
||
| 420 | * Delete objects. |
||
| 421 | * |
||
| 422 | * Used batch API calls to delete multiple objects at once |
||
| 423 | * |
||
| 424 | * @param string[] $ids Array or single Object ID to delete |
||
| 425 | * @param Api $api Api Object to use |
||
| 426 | * @return bool Returns true on success |
||
| 427 | */ |
||
| 428 | public static function deleteIds(array $ids, Api $api = null) { |
||
| 449 | /** |
||
| 450 | * Read function for the object. Convert fields and filters into the query |
||
| 451 | * part of uri and return objects. |
||
| 452 | * |
||
| 453 | * @param mixed $ids Array or single object IDs |
||
| 454 | * @param array $fields Array of field names to read |
||
| 455 | * @param array $params Additional filters for the reading, in assoc |
||
| 456 | * @param Api $api Api Object to use |
||
| 457 | * @return Cursor |
||
| 458 | */ |
||
| 459 | public static function readIds( |
||
| 482 | } |
||
| 483 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.