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 | 17 | * SetUp Object to be ready for work |
|
| 56 | * |
||
| 57 | 17 | * @param array $options Object Options (authSessionId,user,password, |
|
| 58 | 17 | * url,company,evidence, |
|
| 59 | 17 | * prefix,defaultUrlParams,debug, |
|
| 60 | * detail,offline,atomic,filter,ignore404 |
||
| 61 | */ |
||
| 62 | 17 | public function setUp($options = array()) |
|
| 69 | |||
| 70 | /** |
||
| 71 | 7 | * Save record (if evidence allow to). |
|
| 72 | 10 | * Uloží záznam (pokud to evidence dovoluje) |
|
| 73 | 10 | * |
|
| 74 | 10 | * @param array $data Data to save |
|
| 75 | 17 | * @throws Exception Evidence does not support Import |
|
| 76 | * |
||
| 77 | 17 | * @return array odpověď |
|
| 78 | */ |
||
| 79 | public function insertToFlexiBee($data = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Parse Response array |
||
| 91 | * |
||
| 92 | * @param array $responseDecoded |
||
| 93 | * @param int $responseCode Request Response Code |
||
| 94 | * |
||
| 95 | * @return array main data part of response |
||
| 96 | */ |
||
| 97 | public function parseResponse($responseDecoded, $responseCode) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Assign result IDs to its source objects |
||
| 116 | * |
||
| 117 | * @param array $candidates FlexiBee insert IDs prepared by extractResultIDs() |
||
| 118 | */ |
||
| 119 | public function assignResultIDs($candidates) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Extract IDs from FlexiBee response Array |
||
| 145 | * |
||
| 146 | * @param array $resultInfo FlexiBee response |
||
| 147 | * |
||
| 148 | * @return array List of [ 'evidence1'=>[ 'original-id'=>numericID,'original-id2'=>numericID2 ], 'evidence2'=> ... ] |
||
| 149 | */ |
||
| 150 | public function extractResultIDs($resultInfo) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Give you last inserted record ID. |
||
| 168 | * |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | public function getLastInsertedId() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Smaže záznam |
||
| 178 | * Delete record in FlexiBee |
||
| 179 | * |
||
| 180 | * @param int|string $id identifikátor záznamu |
||
| 181 | * |
||
| 182 | * @return boolean Response code is 200 ? |
||
| 183 | */ |
||
| 184 | public function deleteFromFlexiBee($id = null) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Control for existing column names in evidence and take data |
||
| 196 | * |
||
| 197 | * @param array $data Data to keep |
||
| 198 | * |
||
| 199 | * @return int number of records taken |
||
| 200 | */ |
||
| 201 | public function takeData($data) |
||
| 230 | 17 | ||
| 231 | /** |
||
| 232 | * Control data for mandatory columns presence. |
||
| 233 | * |
||
| 234 | * @deprecated since version 1.8.7 |
||
| 235 | * |
||
| 236 | * @param array $data |
||
| 237 | * |
||
| 238 | * @return array List of missing columns. Empty if all is ok |
||
| 239 | */ |
||
| 240 | public function controlMandatoryColumns($data = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Control data for readonly columns presence. |
||
| 262 | * |
||
| 263 | * @param array $data |
||
| 264 | * |
||
| 265 | * @return array List of ReadOnly columns. Empty if all is ok |
||
| 266 | */ |
||
| 267 | public function controlReadOnlyColumns($data = null) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Convert Timestamp to FlexiBee Date format. |
||
| 289 | * |
||
| 290 | * @param int $timpestamp |
||
| 291 | * |
||
| 292 | * @return string FlexiBee Date or NULL |
||
| 293 | */ |
||
| 294 | public static function timestampToFlexiDate($timpestamp = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Convert Timestamp to Flexi DateTime format. |
||
| 307 | * |
||
| 308 | * @param int $timpestamp |
||
| 309 | * |
||
| 310 | * @return string FlexiBee DateTime or NULL |
||
| 311 | */ |
||
| 312 | public static function timestampToFlexiDateTime($timpestamp = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Add Data to evidence Branch |
||
| 325 | * Přidá data do větve |
||
| 326 | * |
||
| 327 | * @thanksto Karel Běl |
||
| 328 | * |
||
| 329 | * @see Relations |
||
| 330 | * |
||
| 331 | * @param array $data pole dat |
||
| 332 | * @param string $relationPath path evidence (relation) pro vkládaná data |
||
| 333 | * |
||
| 334 | * @return boolean Operation success |
||
| 335 | */ |
||
| 336 | public function addArrayToBranch($data, $relationPath = 'polozkyDokladu') |
||
| 347 | 17 | ||
| 348 | 17 | /** |
|
| 349 | 15 | * Vloží do větve data z objektu |
|
| 350 | 15 | * |
|
| 351 | 15 | * @param FlexiBeeRO $object objekt evidence |
|
| 352 | 4 | */ |
|
| 353 | 4 | public function addObjectToBranch($object) |
|
| 357 | 15 | ||
| 358 | 15 | /** |
|
| 359 | * Přidá uživatelskou vazbu |
||
| 360 | * |
||
| 361 | 15 | * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/ |
|
| 362 | 4 | * @param string $vazba |
|
| 363 | 4 | */ |
|
| 364 | 4 | public function vazbaAdd($vazba) |
|
| 369 | 15 | ||
| 370 | 15 | /** |
|
| 371 | 15 | * Smaže uživatelskou vazbu |
|
| 372 | 15 | * |
|
| 373 | 15 | * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/ |
|
| 374 | 17 | * @param string $vazba |
|
| 375 | 17 | */ |
|
| 376 | public function vazbaDel($vazba) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Převede data do Json formátu pro FlexiBee. |
||
| 385 | * Pokud jsou štítky pole, jsou převedeny na seznam oddělený čárkou. |
||
| 386 | * Convert data to FlexiBee like Json format. |
||
| 387 | * Array of Labels is converted to coma separated list |
||
| 388 | * |
||
| 389 | * @param array $data |
||
| 390 | * @param int $options json_encode options like JSON_PRETTY_PRINT etc |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getJsonizedData($data = null, $options = 0) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get Data Fragment specific for current object |
||
| 411 | * |
||
| 412 | * @param array $data |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public function getDataForJSON($data = null) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Insert current data into FlexiBee and load actual record data back |
||
| 430 | * |
||
| 431 | * @param array $data Initial data to save |
||
| 432 | * |
||
| 433 | * @return boolean Operation success |
||
| 434 | */ |
||
| 435 | public function sync($data = null) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Insert current data into FlexiBee and load actual record data back |
||
| 450 | * |
||
| 451 | * @deprecated since version 1.8.9 |
||
| 452 | * |
||
| 453 | * @return boolean Operation success |
||
| 454 | */ |
||
| 455 | public function refresh() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Perform given action (if availble) on current evidence/record |
||
| 462 | * @url https://demo.flexibee.eu/devdoc/actions |
||
| 463 | * |
||
| 464 | * @param string $action one of evidence actions |
||
| 465 | * @param string $method ext|int External method call operation in URL. |
||
| 466 | * Internal add the @action element to request body |
||
| 467 | * |
||
| 468 | * @return boolean operation success |
||
| 469 | */ |
||
| 470 | public function performAction($action, $method = 'int') |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Add External ID to Current Record |
||
| 510 | * |
||
| 511 | * @param string $extId ext:whatever:123 or simplay whatever:123 |
||
| 512 | * |
||
| 513 | * @return array Insert result |
||
| 514 | */ |
||
| 515 | public function addExternalID($extId) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Change Value of external id identified by selector. Add new if not exists |
||
| 522 | * |
||
| 523 | * @param string $selector ext:$selector:$newValue |
||
| 524 | * @param string|int $newValue string or number |
||
| 525 | * @param string|int $forID Other than current record id |
||
| 526 | * |
||
| 527 | * @return array operation result |
||
| 528 | */ |
||
| 529 | public function changeExternalID($selector, $newValue, $forID = null) |
||
| 536 | } |
||
| 537 |
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.