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 |
||
| 5 | class Timeline extends Resource |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Create or Update Timeline Event |
||
| 9 | * |
||
| 10 | * @param int $appId |
||
| 11 | * @param int $eventTypeId |
||
| 12 | * @param string $id |
||
| 13 | * @param int|null $objectId |
||
| 14 | * @param string|null $email |
||
| 15 | * @param string|null $utk |
||
| 16 | * @param array $extraData |
||
| 17 | * @param mixed $timestamp |
||
| 18 | * @param array $eventTypeData |
||
| 19 | * |
||
| 20 | * @return mixed |
||
| 21 | * |
||
| 22 | * @see http://developers.hubspot.com/docs/methods/timeline/create-or-update-event |
||
| 23 | */ |
||
| 24 | public function createOrUpdate( |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Batch Create or Update Timeline Events |
||
| 52 | * |
||
| 53 | * @param int $appId |
||
| 54 | * @param array $events |
||
| 55 | * |
||
| 56 | * @return mixed |
||
| 57 | * |
||
| 58 | * @see https://developers.hubspot.com/docs/methods/timeline/batch-create-or-update-events |
||
| 59 | */ |
||
| 60 | public function createOrUpdateBatch($appId,$events=[]) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get Timeline Event Types |
||
| 71 | * |
||
| 72 | * @param int $appId |
||
| 73 | * |
||
| 74 | * @return mixed |
||
| 75 | * |
||
| 76 | * @see http://developers.hubspot.com/docs/methods/timeline/get-event-types |
||
| 77 | */ |
||
| 78 | public function getEventTypes($appId) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Create Timeline Event Type |
||
| 86 | * |
||
| 87 | * @param int $appId |
||
| 88 | * @param string $name |
||
| 89 | * @param string|null $headerTemplate |
||
| 90 | * @param string|null $detailTemplate |
||
| 91 | * @param string|null $objectType |
||
| 92 | * |
||
| 93 | * @return mixed |
||
| 94 | * |
||
| 95 | * @see http://developers.hubspot.com/docs/methods/timeline/create-event-type |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function createEventType( |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Update Timeline Event Type |
||
| 119 | * |
||
| 120 | * @param int $appId |
||
| 121 | * @param int $eventTypeId |
||
| 122 | * @param string|null $name |
||
| 123 | * @param string|null $headerTemplate |
||
| 124 | * @param string|null $detailTemplate |
||
| 125 | * @param string|null $objectType |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | * |
||
| 129 | * @see http://developers.hubspot.com/docs/methods/timeline/update-event-type |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function updateEventType( |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Delete Timeline Event Type |
||
| 154 | * |
||
| 155 | * @param int $appId |
||
| 156 | * @param int $eventTypeId |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | * |
||
| 160 | * @see http://developers.hubspot.com/docs/methods/timeline/delete-event-type |
||
| 161 | */ |
||
| 162 | public function deleteEventType($appId, $eventTypeId) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get Properties for Timeline Event Type |
||
| 170 | * |
||
| 171 | * @param int $appId |
||
| 172 | * @param int $eventTypeId |
||
| 173 | * |
||
| 174 | * @return mixed |
||
| 175 | * |
||
| 176 | * @see http://developers.hubspot.com/docs/methods/timeline/get-timeline-event-type-properties |
||
| 177 | */ |
||
| 178 | public function getEventTypeProperties($appId, $eventTypeId) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Create Property for Timeline Event Type |
||
| 186 | * |
||
| 187 | * @param int $appId |
||
| 188 | * @param int $eventTypeId |
||
| 189 | * @param string $name |
||
| 190 | * @param string $label |
||
| 191 | * @param string $propertyType |
||
| 192 | * @param string|null $objectProperty |
||
| 193 | * @param array $options |
||
| 194 | * |
||
| 195 | * @return mixed |
||
| 196 | * |
||
| 197 | * @see http://developers.hubspot.com/docs/methods/timeline/create-timeline-event-type-property |
||
| 198 | */ |
||
| 199 | public function createEventTypeProperty( |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Update Property for Timeline Event Type |
||
| 223 | * |
||
| 224 | * @param int $appId |
||
| 225 | * @param int $eventTypeId |
||
| 226 | * @param int $eventTypePropertyId |
||
| 227 | * @param string $name |
||
| 228 | * @param string $label |
||
| 229 | * @param string $propertyType |
||
| 230 | * @param array|null $options |
||
| 231 | * |
||
| 232 | * @return mixed |
||
| 233 | * |
||
| 234 | * @see http://developers.hubspot.com/docs/methods/timeline/udpate-timeline-event-type-property |
||
| 235 | */ |
||
| 236 | public function updateEventTypeProperty( |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Delete Property for Timeline Event Type |
||
| 263 | * |
||
| 264 | * @param int $appId |
||
| 265 | * @param int $eventTypeId |
||
| 266 | * @param int $eventTypePropertyId |
||
| 267 | * |
||
| 268 | * @return mixed |
||
| 269 | * |
||
| 270 | * @see http://developers.hubspot.com/docs/methods/timeline/delete-timeline-event-type-property |
||
| 271 | */ |
||
| 272 | public function deleteEventTypeProperty($appId, $eventTypeId, $eventTypePropertyId) |
||
| 277 | } |
||
| 278 |
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.