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 Companies extends Resource |
||
| 6 | { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Create a company |
||
| 10 | * @param array $properties Array of company properties. |
||
| 11 | * |
||
| 12 | * @see http://developers.hubspot.com/docs/methods/companies/create_company |
||
| 13 | * |
||
| 14 | * @return \SevenShores\Hubspot\Http\Response |
||
| 15 | */ |
||
| 16 | View Code Duplication | function create($properties) |
|
|
|
|||
| 17 | { |
||
| 18 | $endpoint = 'https://api.hubapi.com/companies/v2/companies/'; |
||
| 19 | $options['json'] = ['properties' => $properties]; |
||
| 20 | |||
| 21 | return $this->client->request('post', $endpoint, $options); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Updates a company |
||
| 26 | * @param int $id The company id. |
||
| 27 | * @param array $properties The company properties to update. |
||
| 28 | * |
||
| 29 | * @see http://developers.hubspot.com/docs/methods/companies/update_company |
||
| 30 | * |
||
| 31 | * @return \SevenShores\Hubspot\Http\Response |
||
| 32 | */ |
||
| 33 | View Code Duplication | function update($id, $properties) |
|
| 34 | { |
||
| 35 | $endpoint = "https://api.hubapi.com/companies/v2/companies/{$id}"; |
||
| 36 | $options['json'] = ['properties' => $properties]; |
||
| 37 | |||
| 38 | return $this->client->request('put', $endpoint, $options); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Deletes a company |
||
| 43 | * @param int $id The company id |
||
| 44 | * |
||
| 45 | * @see http://developers.hubspot.com/docs/methods/companies/delete_company |
||
| 46 | * |
||
| 47 | * @return \SevenShores\Hubspot\Http\Response |
||
| 48 | */ |
||
| 49 | function delete($id) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns all companies |
||
| 58 | * @param array $params Array of optional parameters ['limit', 'offset', 'properties'] |
||
| 59 | * |
||
| 60 | * @see http://developers.hubspot.com/docs/methods/companies/get-all-companies |
||
| 61 | * |
||
| 62 | * @return \SevenShores\Hubspot\Http\Response |
||
| 63 | */ |
||
| 64 | View Code Duplication | function all($params = []) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the recently modified companies |
||
| 75 | * @param array $params Array of optional parameters ['count', 'offset'] |
||
| 76 | * |
||
| 77 | * @see http://developers.hubspot.com/docs/methods/companies/get_companies_modified |
||
| 78 | * |
||
| 79 | * @return \SevenShores\Hubspot\Http\Response |
||
| 80 | */ |
||
| 81 | View Code Duplication | function getRecentlyModified($params = []) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Returns the recently created companies |
||
| 92 | * @param array $params Array of optional parameters ['count', 'offset'] |
||
| 93 | * |
||
| 94 | * @see http://developers.hubspot.com/docs/methods/companies/get_companies_created |
||
| 95 | * |
||
| 96 | * @return \SevenShores\Hubspot\Http\Response |
||
| 97 | */ |
||
| 98 | function getRecentlyCreated($params = []) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns an array of companies that have a matching domain |
||
| 109 | * @param string $domain The domain of the company eq. 'example.com'. |
||
| 110 | * |
||
| 111 | * @see http://developers.hubspot.com/docs/methods/companies/get_companies_by_domain |
||
| 112 | * |
||
| 113 | * @return \SevenShores\Hubspot\Http\Response |
||
| 114 | */ |
||
| 115 | function getByDomain($domain) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $domain |
||
| 124 | * @param array $properties |
||
| 125 | * @param int $limit |
||
| 126 | * @param int $offset |
||
| 127 | * |
||
| 128 | * @see https://developers.hubspot.com/docs/methods/companies/search_companies_by_domain |
||
| 129 | * |
||
| 130 | * @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
||
| 131 | */ |
||
| 132 | public function searchByDomain($domain, $properties = [], $limit = 100, $offset = 0) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns a company with id $id |
||
| 150 | * @param int $id |
||
| 151 | * |
||
| 152 | * @see http://developers.hubspot.com/docs/methods/companies/get_company |
||
| 153 | * |
||
| 154 | * @return \SevenShores\Hubspot\Http\Response |
||
| 155 | */ |
||
| 156 | function getById($id) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Associates a given contact to a given company |
||
| 165 | * If a contact is already associated to a different company, the contact will be added to the new company |
||
| 166 | * @param int $contactId |
||
| 167 | * @param int $companyId |
||
| 168 | * |
||
| 169 | * @see http://developers.hubspot.com/docs/methods/companies/add_contact_to_company |
||
| 170 | * |
||
| 171 | * @return \SevenShores\Hubspot\Http\Response |
||
| 172 | */ |
||
| 173 | function addContact($contactId, $companyId) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns an array of the associated contacts for the given company |
||
| 182 | * @param int $companyId The id of the company. |
||
| 183 | * @param array $params Array of optional parameters ['count', 'vidOffset'] |
||
| 184 | * |
||
| 185 | * @see http://developers.hubspot.com/docs/methods/companies/get_company_contacts |
||
| 186 | * |
||
| 187 | * @return \SevenShores\Hubspot\Http\Response |
||
| 188 | */ |
||
| 189 | function getAssociatedContacts($companyId, $params = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns all of the contact IDs who are associated with the given company |
||
| 200 | * @param int $companyId The id of the company. |
||
| 201 | * @param array $params Array of optional parameters ['count', 'vidOffset'] |
||
| 202 | * |
||
| 203 | * @see http://developers.hubspot.com/docs/methods/companies/get_company_contacts_by_id |
||
| 204 | * |
||
| 205 | * @return \SevenShores\Hubspot\Http\Response |
||
| 206 | */ |
||
| 207 | function getAssociatedContactIds($companyId, $params = []) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Removes a contact from a company |
||
| 218 | * @param int $contactId |
||
| 219 | * @param int $companyId |
||
| 220 | * |
||
| 221 | * @see http://developers.hubspot.com/docs/methods/companies/remove_contact_from_company |
||
| 222 | * |
||
| 223 | * @return \SevenShores\Hubspot\Http\Response |
||
| 224 | */ |
||
| 225 | function removeContact($contactId, $companyId) |
||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | } |
||
| 235 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.