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 CalendarEvents extends Resource |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @param array $properties |
||
| 9 | * |
||
| 10 | * @return \SevenShores\Hubspot\Http\Response |
||
| 11 | */ |
||
| 12 | public function createTask($properties) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param int $id The task id. |
||
| 22 | * @param array $properties The task properties to update. |
||
| 23 | * |
||
| 24 | * @return \SevenShores\Hubspot\Http\Response |
||
| 25 | */ |
||
| 26 | public function updateTask($id, $properties) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param int $id |
||
| 37 | * |
||
| 38 | * @return \SevenShores\Hubspot\Http\Response |
||
| 39 | */ |
||
| 40 | public function getTaskById($id) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param int $id |
||
| 49 | * @return \SevenShores\Hubspot\Http\Response |
||
| 50 | */ |
||
| 51 | public function deleteTask($id) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get an aggregation for all calendar event types. |
||
| 60 | * |
||
| 61 | * |
||
| 62 | * @see https://developers.hubspot.com/docs/methods/calendar/list-events |
||
| 63 | * |
||
| 64 | * @param int $startDate The beginning value of the query range, in UTC, in milliseconds |
||
| 65 | * @param int $endDate The end value of the query range, in UTC, in milliseconds |
||
| 66 | * @param array $params Array of optional parameters: |
||
| 67 | * limit, type, campaignGuid, contentCategory, includeNoCampaigns |
||
| 68 | * |
||
| 69 | * @return \SevenShores\Hubspot\Http\Response |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function all($startDate, $endDate, $params = []) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Retrieve task events for Calendar. |
||
| 83 | * A shortcut of the standard events call for finer-grained control. |
||
| 84 | * |
||
| 85 | * @see https://developers.hubspot.com/docs/methods/calendar/list-tasks |
||
| 86 | * |
||
| 87 | * @param int $startDate The beginning value of the query range, in UTC, in milliseconds |
||
| 88 | * @param int $endDate The end value of the query range, in UTC, in milliseconds |
||
| 89 | * @param array $params Array of optional parameters: |
||
| 90 | * limit, campaignGuid, includeNoCampaigns |
||
| 91 | * |
||
| 92 | * @return \SevenShores\Hubspot\Http\Response |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function allTasks($startDate, $endDate, $params = []) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Retrieve social events for Calendar. |
||
| 106 | * A shortcut of the standard events call for finer-grained control. |
||
| 107 | * |
||
| 108 | * @see https://developers.hubspot.com/docs/methods/calendar/list-social-events |
||
| 109 | * |
||
| 110 | * @param int $startDate The beginning value of the query range, in UTC, in milliseconds |
||
| 111 | * @param int $endDate The end value of the query range, in UTC, in milliseconds |
||
| 112 | * @param array $params Array of optional parameters: |
||
| 113 | * limit, offset, campaignGuid, includeNoCampaigns |
||
| 114 | * |
||
| 115 | * @return \SevenShores\Hubspot\Http\Response |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function allSocialEvents($startDate, $endDate, $params = []) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * List content events. |
||
| 129 | * Get events from the calendar. |
||
| 130 | * |
||
| 131 | * @see https://developers.hubspot.com/docs/methods/calendar/list-content-events |
||
| 132 | * |
||
| 133 | * @param int $startDate The beginning value of the query range, in UTC, in milliseconds |
||
| 134 | * @param int $endDate The end value of the query range, in UTC, in milliseconds |
||
| 135 | * @param array $params Array of optional parameters: |
||
| 136 | * limit, offset, contentCategory, campaignGuid, includeNoCampaigns |
||
| 137 | * |
||
| 138 | * @return \SevenShores\Hubspot\Http\Response |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function allContentEvents($startDate, $endDate, $params = []) |
|
| 149 | } |
||
| 150 |
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.