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 |
||
| 12 | class Client implements ClientInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The default ttl for cached responses (24 hours). |
||
| 16 | * |
||
| 17 | * @link http://developer.marvel.com/documentation/attribution Marvel's rules for caching. |
||
| 18 | * |
||
| 19 | * @const integer |
||
| 20 | */ |
||
| 21 | const MAX_TTL = 86400; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The public api key issued by Marvel. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $publicApiKey; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The private api key issued by Marvel. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $privateApiKey; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Guzzle HTTP Client implementation. |
||
| 39 | * |
||
| 40 | * @var GuzzleHttp\ClientInterface |
||
| 41 | */ |
||
| 42 | private $guzzleClient; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Cache implementation. |
||
| 46 | * |
||
| 47 | * @var CacheInterface |
||
| 48 | */ |
||
| 49 | private $cache; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The Marvel API url. |
||
| 53 | * |
||
| 54 | * @const string |
||
| 55 | */ |
||
| 56 | const BASE_URL = 'http://gateway.marvel.com/v1/public/'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Construct a new Client. |
||
| 60 | * |
||
| 61 | * @param string $privateApiKey The private api key issued by Marvel. |
||
| 62 | * @param string $publicApiKey The public api key issued by Marvel. |
||
| 63 | * @param GuzzleHttp\ClientInterface $guzzleClient Implementation of a Guzzle HTTP client. |
||
| 64 | * @param CacheInterface $cache Implementation of Cache. |
||
| 65 | */ |
||
| 66 | final public function __construct( |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Execute a search request against the Marvel API. |
||
| 80 | * |
||
| 81 | * @param string $resource The API resource to search for. |
||
| 82 | * @param array $filters Array of search criteria to use in request. |
||
| 83 | * |
||
| 84 | * @return null|DataWrapper |
||
| 85 | * |
||
| 86 | * @throws \InvalidArgumentException Thrown if $resource is empty or not a string. |
||
| 87 | */ |
||
| 88 | View Code Duplication | final public function search(string $resource, array $filters = []) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Execute a GET request against the Marvel API for a single resource. |
||
| 100 | * |
||
| 101 | * @param string $resource The API resource to search for. |
||
| 102 | * @param integer $id The id of the API resource. |
||
| 103 | * |
||
| 104 | * @return null|DataWrapper |
||
| 105 | */ |
||
| 106 | View Code Duplication | final public function get(string $resource, int $id) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Send the given API url request. |
||
| 118 | * |
||
| 119 | * @param string $resource The API resource to search for. |
||
| 120 | * @param integer $id The id of a specific API resource. |
||
| 121 | * @param array $query Array of search criteria to use in request. |
||
| 122 | * |
||
| 123 | * @return ResponseInterface |
||
| 124 | */ |
||
| 125 | final private function send(string $resource, int $id = null, array $query = []) : ResponseInterface |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Allow calls such as $client->characters(); |
||
| 153 | * |
||
| 154 | * @param string $name The name of the api resource. |
||
| 155 | * @param array $arguments The parameters to pass to get() or search(). |
||
| 156 | * |
||
| 157 | * @return Collection|EntityInterface|null |
||
| 158 | */ |
||
| 159 | final public function __call(string $name, array $arguments) |
||
| 176 | } |
||
| 177 |