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 |
||
| 10 | class Client |
||
| 11 | { |
||
| 12 | protected $baseUrl; |
||
| 13 | |||
| 14 | /** @var AuthInterface */ |
||
| 15 | protected $auth; |
||
| 16 | |||
| 17 | /** @var Browser */ |
||
| 18 | protected $browser; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Client constructor |
||
| 22 | * |
||
| 23 | * @param AuthInterface $auth Authentication you want to use to access the api |
||
| 24 | * @param Browser $browser Client you want to use, by default browser with curl will be used |
||
| 25 | * @param string $baseUrl URL to the HipChat server endpoint |
||
| 26 | * |
||
| 27 | * @return self |
||
|
|
|||
| 28 | */ |
||
| 29 | public function __construct(AuthInterface $auth, Browser $browser = null, $baseUrl = 'https://api.hipchat.com') |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Set the base URL for the requests. Defaults to the public API |
||
| 44 | * but this allows it to work with internal implementations too |
||
| 45 | * |
||
| 46 | * @param string $url URL to the HipChat server endpoint |
||
| 47 | * |
||
| 48 | * @deprecated Use constructor to change default baseUrl instead, will be removed in 2.0 |
||
| 49 | */ |
||
| 50 | public function setBaseUrl($url) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Common get request for all API calls |
||
| 57 | * |
||
| 58 | * @param string $resource The path to the resource wanted. For example v2/room |
||
| 59 | * @param array $query Parameters to filter the response for example array('max-results' => 50) |
||
| 60 | * |
||
| 61 | * @return array Decoded array containing response |
||
| 62 | * @throws Exception\RequestException |
||
| 63 | */ |
||
| 64 | public function get($resource, $query = array()) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Common post request for all API calls |
||
| 92 | * |
||
| 93 | * @param string $resource The path to the resource wanted. For example v2/room |
||
| 94 | * @param array $content Parameters be posted for example: |
||
| 95 | * array( |
||
| 96 | * 'name' => 'Example name', |
||
| 97 | * 'privacy' => 'private', |
||
| 98 | * 'is_archived' => 'false', |
||
| 99 | * 'is_guest_accessible' => 'false', |
||
| 100 | * 'topic' => 'New topic', |
||
| 101 | * ) |
||
| 102 | * |
||
| 103 | * @return array Decoded array containing response |
||
| 104 | * @throws Exception\RequestException |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function post($resource, $content) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Common put request for all API calls |
||
| 130 | * |
||
| 131 | * @param string $resource The path to the resource wanted. For example v2/room |
||
| 132 | * @param array $content Parameters be putted for example: |
||
| 133 | * array( |
||
| 134 | * 'name' => 'Example name', |
||
| 135 | * 'privacy' => 'private', |
||
| 136 | * 'is_archived' => 'false', |
||
| 137 | * 'is_guest_accessible' => 'false', |
||
| 138 | * 'topic' => 'New topic', |
||
| 139 | * ) |
||
| 140 | * |
||
| 141 | * @return array Decoded array containing response |
||
| 142 | * @throws Exception\RequestException |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function put($resource, $content = array()) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Common delete request for all API calls |
||
| 167 | * |
||
| 168 | * @param string $resource The path to the resource wanted. For example v2/room |
||
| 169 | * |
||
| 170 | * @return array Decoded array containing response |
||
| 171 | * @throws Exception\RequestException |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function delete($resource) |
|
| 193 | } |
||
| 194 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.