Complex classes like FlexiBeeRW 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 FlexiBeeRW, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class FlexiBeeRW extends FlexiBeeRO |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Sloupeček obsahující datum vložení záznamu do shopu. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $myCreateColumn = 'false'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $myLastModifiedColumn = 'lastUpdate'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Last Inserted ID. |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | public $lastInsertedID = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Array of fields for next curl POST operation |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $postFields = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Transaction processing mode |
||
| 48 | * |
||
| 49 | * @link https://www.flexibee.eu/api/dokumentace/ref/tx/ Transakční zpracování |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | public $atomic = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * SetUp Object to be ready for work |
||
| 56 | * |
||
| 57 | * @param array $options Object Options (authSessionId,user,password, |
||
| 58 | * url,company,evidence, |
||
| 59 | * prefix,defaultUrlParams,debug, |
||
| 60 | * detail,offline,atomic,filter,ignore404 |
||
| 61 | */ |
||
| 62 | public function setUp($options = array()) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Save record (if evidence allow to). |
||
| 72 | * Uloží záznam (pokud to evidence dovoluje) |
||
| 73 | * |
||
| 74 | * @param array $data Data to save |
||
| 75 | * @throws Exception Evidence does not support Import |
||
| 76 | * |
||
| 77 | * @return array odpověď |
||
| 78 | */ |
||
| 79 | public function insertToFlexiBee($data = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Parse Response array |
||
| 110 | * |
||
| 111 | * @param array $responseDecoded |
||
| 112 | * @param int $responseCode Request Response Code |
||
| 113 | * |
||
| 114 | * @return array main data part of response |
||
| 115 | */ |
||
| 116 | public function parseResponse($responseDecoded, $responseCode) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Assign result IDs to its source objects |
||
| 135 | * |
||
| 136 | * @param array $candidates FlexiBee insert IDs prepared by extractResultIDs() |
||
| 137 | */ |
||
| 138 | public function assignResultIDs($candidates) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Extract IDs from FlexiBee response Array |
||
| 164 | * |
||
| 165 | * @param array $resultInfo FlexiBee response |
||
| 166 | * |
||
| 167 | * @return array List of [ 'evidence1'=>[ 'original-id'=>numericID,'original-id2'=>numericID2 ], 'evidence2'=> ... ] |
||
| 168 | */ |
||
| 169 | public function extractResultIDs($resultInfo) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Give you last inserted record ID. |
||
| 187 | * |
||
| 188 | * @return int |
||
| 189 | */ |
||
| 190 | public function getLastInsertedId() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Smaže záznam |
||
| 197 | * Delete record in FlexiBee |
||
| 198 | * |
||
| 199 | * @param int|string $id identifikátor záznamu |
||
| 200 | * |
||
| 201 | * @return boolean Response code is 200 ? |
||
| 202 | */ |
||
| 203 | public function deleteFromFlexiBee($id = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Control for existing column names in evidence and take data |
||
| 215 | * |
||
| 216 | * @param array $data Data to keep |
||
| 217 | * |
||
| 218 | * @return int number of records taken |
||
| 219 | */ |
||
| 220 | public function takeData($data) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Control data for mandatory columns presence. |
||
| 252 | * |
||
| 253 | * @deprecated since version 1.8.7 |
||
| 254 | * |
||
| 255 | * @param array $data |
||
| 256 | * |
||
| 257 | * @return array List of missing columns. Empty if all is ok |
||
| 258 | */ |
||
| 259 | public function controlMandatoryColumns($data = null) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Control data for readonly columns presence. |
||
| 281 | * |
||
| 282 | * @param array $data |
||
| 283 | * |
||
| 284 | * @return array List of ReadOnly columns. Empty if all is ok |
||
| 285 | */ |
||
| 286 | public function controlReadOnlyColumns($data = null) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Convert Timestamp to FlexiBee Date format. |
||
| 308 | * |
||
| 309 | * @param int $timpestamp |
||
| 310 | * |
||
| 311 | * @return string FlexiBee Date or NULL |
||
| 312 | */ |
||
| 313 | public static function timestampToFlexiDate($timpestamp = null) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Convert Timestamp to Flexi DateTime format. |
||
| 326 | * |
||
| 327 | * @param int $timpestamp |
||
| 328 | * |
||
| 329 | * @return string FlexiBee DateTime or NULL |
||
| 330 | */ |
||
| 331 | public static function timestampToFlexiDateTime($timpestamp = null) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Add Data to evidence Branch |
||
| 344 | * Přidá data do větve |
||
| 345 | * |
||
| 346 | * @thanksto Karel Běl |
||
| 347 | * |
||
| 348 | * @see Relations |
||
| 349 | * |
||
| 350 | * @param array $data pole dat |
||
| 351 | * @param string $relationPath path evidence (relation) pro vkládaná data |
||
| 352 | * |
||
| 353 | * @return boolean Operation success |
||
| 354 | */ |
||
| 355 | public function addArrayToBranch($data, $relationPath) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Vloží do větve data z objektu |
||
| 380 | * |
||
| 381 | * @param FlexiBeeRO $object objekt evidence |
||
| 382 | */ |
||
| 383 | public function addObjectToBranch($object) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Přidá uživatelskou vazbu |
||
| 390 | * |
||
| 391 | * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/ |
||
| 392 | * @param string $vazba |
||
| 393 | */ |
||
| 394 | public function vazbaAdd($vazba) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Smaže uživatelskou vazbu |
||
| 402 | * |
||
| 403 | * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/ |
||
| 404 | * @param string $vazba |
||
| 405 | */ |
||
| 406 | public function vazbaDel($vazba) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Převede data do Json formátu pro FlexiBee. |
||
| 415 | * Pokud jsou štítky pole, jsou převedeny na seznam oddělený čárkou. |
||
| 416 | * Convert data to FlexiBee like Json format. |
||
| 417 | * Array of Labels is converted to coma separated list |
||
| 418 | * |
||
| 419 | * @param array $data |
||
| 420 | * @param int $options json_encode options like JSON_PRETTY_PRINT etc |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getJsonizedData($data = null, $options = 0) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get Data Fragment specific for current object |
||
| 441 | * |
||
| 442 | * @param array $data |
||
| 443 | * |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | public function getDataForJSON($data = null) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Insert current data into FlexiBee and load actual record data back |
||
| 460 | * |
||
| 461 | * @return boolean Operation success |
||
| 462 | */ |
||
| 463 | public function sync() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Insert current data into FlexiBee and load actual record data back |
||
| 478 | * |
||
| 479 | * @deprecated since version 1.8.9 |
||
| 480 | * |
||
| 481 | * @return boolean Operation success |
||
| 482 | */ |
||
| 483 | public function refresh() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Perform given action (if availble) on current evidence/record |
||
| 490 | * @url https://demo.flexibee.eu/devdoc/actions |
||
| 491 | * |
||
| 492 | * @param string $action one of evidence actions |
||
| 493 | * @param string $method ext|int External method call operation in URL. |
||
| 494 | * Internal add the @action element to request body |
||
| 495 | * |
||
| 496 | * @return boolean operation success |
||
| 497 | */ |
||
| 498 | public function performAction($action, $method = 'int') |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Add External ID to Current Record |
||
| 538 | * |
||
| 539 | * @param string $extId |
||
| 540 | * |
||
| 541 | * @return array Insert result |
||
| 542 | */ |
||
| 543 | public function addExternalID($extId) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Change Value of external id identified by selector. Add new if not exists |
||
| 550 | * |
||
| 551 | * @param string $selector ext:$selector:$newValue |
||
| 552 | * @param string|int $newValue string or number |
||
| 553 | * @param string|int $forID Other than current record id |
||
| 554 | * |
||
| 555 | * @return array operation result |
||
| 556 | */ |
||
| 557 | public function changeExternalID($selector, $newValue, $forID = null) |
||
| 564 | } |
||
| 565 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.