Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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 | * Save record (if evidence allow to). |
||
| 48 | * Uloží záznam (pokud to evidence dovoluje) |
||
| 49 | * |
||
| 50 | * @param array $data Data to save |
||
| 51 | * @throws Exception Evidence does not support Import |
||
| 52 | * |
||
| 53 | * @return array odpověď |
||
| 54 | */ |
||
| 55 | public function insertToFlexiBee($data = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Give you last inserted record ID. |
||
| 94 | * |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | public function getLastInsertedId() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Smaže záznam |
||
| 104 | * Delete record in FlexiBee |
||
| 105 | * |
||
| 106 | * @param int|string $id identifikátor záznamu |
||
| 107 | * @return boolean Response code is 200 ? |
||
| 108 | */ |
||
| 109 | public function deleteFromFlexiBee($id = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Control for existing column names in evidence and take data |
||
| 121 | * |
||
| 122 | * @param array $data Data to keep |
||
| 123 | * @return int number of records taken |
||
| 124 | */ |
||
| 125 | public function takeData($data) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Control data for mandatory columns presence. |
||
| 157 | * |
||
| 158 | * @param array $data |
||
| 159 | * @return array List of missing columns. Empty if all is ok |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function controlMandatoryColumns($data = null) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Control data for readonly columns presence. |
||
| 182 | * |
||
| 183 | * @param array $data |
||
| 184 | * @return array List of ReadOnly columns. Empty if all is ok |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function controlReadOnlyColumns($data = null) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Convert Timestamp to FlexiBee Date format. |
||
| 207 | * |
||
| 208 | * @param int $timpestamp |
||
| 209 | * |
||
| 210 | * @return string FlexiBee Date or NULL |
||
| 211 | */ |
||
| 212 | View Code Duplication | public static function timestampToFlexiDate($timpestamp = null) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Convert Timestamp to Flexi DateTime format. |
||
| 225 | * |
||
| 226 | * @param int $timpestamp |
||
| 227 | * |
||
| 228 | * @return string FlexiBee DateTime or NULL |
||
| 229 | */ |
||
| 230 | View Code Duplication | public static function timestampToFlexiDateTime($timpestamp = null) |
|
| 240 | |||
| 241 | } |
||
| 242 |
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.