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 HubDB extends Resource |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Get all tables |
||
| 9 | * |
||
| 10 | * @param int $portalId Hub/Portal ID |
||
| 11 | * |
||
| 12 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 13 | */ |
||
| 14 | public function tables($portalId) { |
||
| 15 | $endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables'; |
||
| 16 | |||
| 17 | $queryString = build_query_string(['portalId' => $portalId]); |
||
| 18 | |||
| 19 | return $this->client->request('get', $endpoint, [], $queryString); |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get details about a table |
||
| 24 | * |
||
| 25 | * @param int $portalId Hub ID |
||
| 26 | * @param int $tableId Table ID |
||
| 27 | * |
||
| 28 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 29 | */ |
||
| 30 | View Code Duplication | public function tableInfo($portalId, $tableId) { |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Delete a table |
||
| 40 | * |
||
| 41 | * @param int $tableId Table ID |
||
| 42 | * |
||
| 43 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 44 | */ |
||
| 45 | public function deleteTable($tableId) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Delete a row |
||
| 53 | * |
||
| 54 | * @param int $tableId Table ID |
||
| 55 | * @param int $rowId Row ID |
||
| 56 | * |
||
| 57 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 58 | */ |
||
| 59 | public function deleteRow($tableId, $rowId) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $name table name |
||
| 67 | * @param array $columns column name and type should be represented as associative array, e.g. ["name" => "Name", "type" => "TEXT"], @see https://developers.hubspot.com/docs/methods/hubdb/create_table |
||
| 68 | * @param bool $published whether to publish table |
||
| 69 | * @param bool $useForPages use table for dynamic pages, see https://designers.hubspot.com/docs/tutorials/how-to-build-dynamic-pages-with-hubdb |
||
| 70 | * |
||
| 71 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 72 | */ |
||
| 73 | public function createTable($name, array $columns, $published = true, $useForPages = false) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get table rows |
||
| 86 | * |
||
| 87 | * @param int $portalId Hub/Portal ID |
||
| 88 | * @param int $tableId table ID |
||
| 89 | * @param array $params key-value array to filter and sort rows |
||
| 90 | * |
||
| 91 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function rows($portalId, $tableId, array $params) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $tableId table ID |
||
| 104 | * @param array $values |
||
| 105 | * |
||
| 106 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 107 | */ |
||
| 108 | public function addRow($tableId, array $values) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param int $tableId table ID |
||
| 117 | * @param array $values |
||
| 118 | * @param string $title page title for dynamic page |
||
| 119 | * @param string $path path to access page (appended to domain to form page URL) |
||
| 120 | * |
||
| 121 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 122 | */ |
||
| 123 | public function addRowForPage($tableId, array $values, $title = '', $path = '') { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * update database table row |
||
| 132 | * |
||
| 133 | * @param int $tableId |
||
| 134 | * @param int $rowId |
||
| 135 | * @param array $values |
||
| 136 | * |
||
| 137 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 138 | */ |
||
| 139 | public function updateRow($tableId, $rowId, array $values) { |
||
| 145 | } |
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.