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 Workflows extends Resource |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Get all workflows. |
||
| 9 | * |
||
| 10 | * @return \SevenShores\Hubspot\Http\Response |
||
| 11 | */ |
||
| 12 | public function all() |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Get a specific workflow. |
||
| 21 | * |
||
| 22 | * @param int $id |
||
| 23 | * |
||
| 24 | * @return \SevenShores\Hubspot\Http\Response |
||
| 25 | */ |
||
| 26 | public function getById($id) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Enroll a contact in a workflow. |
||
| 35 | * |
||
| 36 | * @param int $workflow_id |
||
| 37 | * @param string $email |
||
| 38 | * |
||
| 39 | * @return \SevenShores\Hubspot\Http\Response |
||
| 40 | */ |
||
| 41 | public function enrollContact($workflow_id, $email) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Unenroll a contact from a workflow. |
||
| 50 | * |
||
| 51 | * @param int $workflow_id |
||
| 52 | * @param string $email |
||
| 53 | * |
||
| 54 | * @return \SevenShores\Hubspot\Http\Response |
||
| 55 | */ |
||
| 56 | public function unenrollContact($workflow_id, $email) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Create a new workflow. |
||
| 65 | * |
||
| 66 | * @param array $workflow The workflow properties |
||
| 67 | * |
||
| 68 | * @return \SevenShores\Hubspot\Http\Response |
||
| 69 | */ |
||
| 70 | public function create($workflow) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Delete a workflow. |
||
| 81 | * |
||
| 82 | * @param int $id |
||
| 83 | * |
||
| 84 | * @return \SevenShores\Hubspot\Http\Response |
||
| 85 | */ |
||
| 86 | public function delete($id) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get current enrollments for a contact. |
||
| 95 | * |
||
| 96 | * @param int $contact_id |
||
| 97 | * |
||
| 98 | * @return \SevenShores\Hubspot\Http\Response |
||
| 99 | */ |
||
| 100 | public function enrollmentsForContact($contact_id) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get a history of events for a specific workflow, filtered for a |
||
| 109 | * specific contact and/or event type(s). |
||
| 110 | * |
||
| 111 | * @param int $workflow_id |
||
| 112 | * @param array $filter |
||
| 113 | * @param array $params Optional parameters. |
||
| 114 | * |
||
| 115 | * @return \SevenShores\Hubspot\Http\Response |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function logEvents($workflow_id, $filter, $params = []) |
|
| 127 | } |
||
| 128 |
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.