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 | * @return \SevenShores\Hubspot\Http\Response |
||
| 24 | */ |
||
| 25 | public function getById($id) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Enroll a contact in a workflow. |
||
| 34 | * |
||
| 35 | * @param int $workflow_id |
||
| 36 | * @param string $email |
||
| 37 | * @return \SevenShores\Hubspot\Http\Response |
||
| 38 | */ |
||
| 39 | public function enrollContact($workflow_id, $email) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Unenroll a contact from a workflow. |
||
| 48 | * |
||
| 49 | * @param int $workflow_id |
||
| 50 | * @param string $email |
||
| 51 | * @return \SevenShores\Hubspot\Http\Response |
||
| 52 | */ |
||
| 53 | public function unenrollContact($workflow_id, $email) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create a new workflow. |
||
| 62 | * |
||
| 63 | * @param array $workflow The workflow properties |
||
| 64 | * @return \SevenShores\Hubspot\Http\Response |
||
| 65 | */ |
||
| 66 | public function create($workflow) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Delete a workflow. |
||
| 77 | * |
||
| 78 | * @param int $id |
||
| 79 | * @return \SevenShores\Hubspot\Http\Response |
||
| 80 | */ |
||
| 81 | public function delete($id) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get current enrollments for a contact. |
||
| 90 | * |
||
| 91 | * @param int $contact_id |
||
| 92 | * @return \SevenShores\Hubspot\Http\Response |
||
| 93 | */ |
||
| 94 | public function enrollmentsForContact($contact_id) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get a history of events for a specific workflow, filtered for a |
||
| 103 | * specific contact and/or event type(s). |
||
| 104 | * |
||
| 105 | * @param int $workflow_id |
||
| 106 | * @param array $filter |
||
| 107 | * @param array $params Optional parameters. |
||
| 108 | * @return \SevenShores\Hubspot\Http\Response |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function logEvents($workflow_id, $filter, $params = []) |
|
| 120 | } |
||
| 121 |
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.