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 | ||
| 15 | class Bundle implements \Iterator | ||
| 16 | { | ||
| 17 | protected $client = null; | ||
| 18 | protected $position = 0; | ||
| 19 | public $detail = null; | ||
| 20 | public $location = null; | ||
| 21 | |||
| 22 | public function __construct($key, $client = null, $user_agent = '') | ||
| 23 |     { | ||
| 24 | $this->client = (is_null($client)) ? new \Clarify\Client($key, $client, $user_agent) : $client; | ||
| 25 | } | ||
| 26 | |||
| 27 | /** | ||
| 28 | * @param string $name | ||
| 29 | * @param string $media_url | ||
| 30 | * @param string $metadata | ||
| 31 | * @param string $notify_url | ||
| 32 | * @param string $audio_channel | ||
| 33 | * @return bool | ||
| 34 | * @throws Exceptions\InvalidJSONException | ||
| 35 | * @throws Exceptions\InvalidEnumTypeException | ||
| 36 | */ | ||
| 37 | public function create($name = '', $media_url = '', $metadata = '', $notify_url = '', $audio_channel = '') | ||
| 38 |     { | ||
| 39 | $params = array(); | ||
| 40 | $params['name'] = $name; | ||
| 41 | $params['media_url'] = $media_url; | ||
| 42 | $params['metadata'] = $metadata; | ||
| 43 | $params['notify_url'] = $notify_url; | ||
| 44 | $params['audio_channel'] = $audio_channel; | ||
| 45 | |||
| 46 | $ob = json_decode($metadata); | ||
| 47 |         if ($metadata != '' && $ob === null) { | ||
| 48 | throw new InvalidJSONException(); | ||
| 49 | } | ||
| 50 | |||
| 51 | $audio_channel = isset($params['audio_channel']) ? $params['audio_channel'] : ''; | ||
| 52 | View Code Duplication |         if (!in_array($audio_channel, array('left', 'right', 'split', ''))) { | |
| 53 | throw new InvalidEnumTypeException(); | ||
| 54 | } | ||
| 55 | |||
| 56 |         $result = $this->client->post('bundles', $params); | ||
| 57 | $this->detail = $this->client->detail; | ||
| 58 |         $this->location = $this->client->response->getHeader('Location')[0]; | ||
| 59 | return $result; | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * @param $id | ||
| 64 | * @param string $name | ||
| 65 | * @param string $notify_url | ||
| 66 | * @param int $version | ||
| 67 | * @return mixed | ||
| 68 | * @throws InvalidIntegerArgumentException | ||
| 69 | */ | ||
| 70 | public function update($id, $name = '', $notify_url = '', $version = 1) | ||
| 71 |     { | ||
| 72 | $params = array(); | ||
| 73 | $params['name'] = $name; | ||
| 74 | $params['notify_url'] = $notify_url; | ||
| 75 | $params['version'] = $version; | ||
| 76 |         if (!is_numeric($params['version'])) { | ||
| 77 | throw new InvalidIntegerArgumentException(); | ||
| 78 | } | ||
| 79 | |||
| 80 | return $this->client->put($id, $params); | ||
| 81 | } | ||
| 82 | |||
| 83 | public function delete($id) | ||
| 87 | |||
| 88 | public function load($id) | ||
| 92 | |||
| 93 | public function index($limit = 10, $embed = '') | ||
| 94 |     { | ||
| 95 |         $params = array('limit' => $limit, 'embed' => $embed); | ||
| 96 |         $this->detail = $this->client->get('bundles', $params); | ||
| 97 | |||
| 98 | return $this->detail; | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * The nice thing about search is that once you've loaded some results, you can use the normal pagination functions | ||
| 103 | * without having to do anything special because search results are just another collection. | ||
| 104 | * | ||
| 105 | * @param $query | ||
| 106 | * @param int $limit How many search results to return at a time | ||
| 107 | * @param string $embed Should the full item be embedded in the results or just a reference? | ||
| 108 | * @param string $query_fields | ||
| 109 | * @param string $filter | ||
| 110 | * @param string $language | ||
| 111 | * @return array|bool|float|int|string | ||
| 112 | */ | ||
| 113 | public function search($query, $limit = 10, $embed = '', $query_fields = '', $filter = '', $language = 'en') | ||
| 114 |     { | ||
| 115 |         $params = array('query' => $query, 'limit' => $limit, 'embed' => $embed, | ||
| 116 | 'query_fields' => $query_fields, 'filter' => $filter, 'language' => $language); | ||
| 117 |         $this->detail = $this->client->get('search', $params); | ||
| 118 | |||
| 119 | return $this->detail; | ||
| 120 | } | ||
| 121 | |||
| 122 | public function hasMorePages() | ||
| 126 | |||
| 127 | public function getNextPage() | ||
| 131 | |||
| 132 | public function getPreviousPage() | ||
| 136 | |||
| 137 | protected function getPage($direction = 'next') | ||
| 138 |     { | ||
| 139 |         if (isset($this->detail['_links'][$direction])) { | ||
| 140 | $next_uri = $this->detail['_links'][$direction]['href']; | ||
| 141 | $_params = parse_url($next_uri); | ||
| 150 | |||
| 151 | public function getResponse() | ||
| 155 | |||
| 156 | public function getStatusCode() | ||
| 160 | |||
| 161 | public function rewind() | ||
| 165 | |||
| 166 | public function current() | ||
| 172 | |||
| 173 | public function key() | ||
| 177 | |||
| 178 | public function next() | ||
| 182 | |||
| 183 | public function valid() | ||
| 187 | |||
| 188 | public function count() | ||
| 192 | |||
| 193 | /** | ||
| 194 | * @param $name | ||
| 195 | * @return Metadata|Tracks|Insights | ||
| 196 | * @throws Exceptions\InvalidResourceException | ||
| 197 | */ | ||
| 198 | public function __get($name) | ||
| 209 | } |