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 |
||
| 14 | class TwitchRequest |
||
| 15 | { |
||
| 16 | /** @var string Set the useragnet */ |
||
| 17 | private $userAgent = 'jofner TwitchSDK 2.*'; |
||
| 18 | |||
| 19 | /** @var integer Set connect timeout */ |
||
| 20 | public $connectTimeout = 30; |
||
| 21 | |||
| 22 | /** @var integer Set timeout default. */ |
||
| 23 | public $timeout = 30; |
||
| 24 | |||
| 25 | /** @var boolean Verify SSL Cert */ |
||
| 26 | public $sslVerifypeer = false; |
||
| 27 | |||
| 28 | /** @var integer Contains the last HTTP status code returned */ |
||
| 29 | public $httpCode = 0; |
||
| 30 | |||
| 31 | /** @var array Contains the last HTTP headers returned */ |
||
| 32 | public $httpInfo = array(); |
||
| 33 | |||
| 34 | /** @var array Contains the last Server headers returned */ |
||
| 35 | public $httpHeader = array(); |
||
| 36 | |||
| 37 | /** @var boolean Throw cURL errors */ |
||
| 38 | public $throwCurlErrors = true; |
||
| 39 | |||
| 40 | /** @var int API version to use */ |
||
| 41 | private $apiVersion = 3; |
||
| 42 | |||
| 43 | const URL_TWITCH = 'https://api.twitch.tv/kraken/'; |
||
| 44 | const URL_TWITCH_TEAM = 'http://api.twitch.tv/api/team/'; |
||
| 45 | const URI_AUTH = 'oauth2/authorize'; |
||
| 46 | const URI_AUTH_TOKEN = 'oauth2/token'; |
||
| 47 | const MIME_TYPE = 'application/vnd.twitchtv.v%d+json'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * TwitchRequest constructor |
||
| 51 | * @param array $config |
||
| 52 | */ |
||
| 53 | public function __construct($config) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set the API version to use |
||
| 60 | * @param integer $version |
||
| 61 | * @deprecated will be removed, force to use v3 API, which is current stable Twitch API version |
||
| 62 | */ |
||
| 63 | public function setApiVersion($version) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the API version |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | public function getApiVersion() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * TwitchAPI request |
||
| 81 | * @param string |
||
| 82 | * @param string |
||
| 83 | * @param string |
||
| 84 | * @return \stdClass |
||
| 85 | * @throws \jofner\SDK\TwitchTV\TwitchException |
||
| 86 | */ |
||
| 87 | public function request($uri, $method = 'GET', $postfields = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Twitch Team API request |
||
| 94 | * @param string |
||
| 95 | * @param string |
||
| 96 | * @param string |
||
| 97 | * @return \stdClass |
||
| 98 | * @throws \jofner\SDK\TwitchTV\TwitchException |
||
| 99 | */ |
||
| 100 | public function teamRequest($uri, $method = 'GET', $postfields = null) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * TwitchAPI request |
||
| 107 | * method used by teamRequest && request methods |
||
| 108 | * because there are two different Twitch APIs |
||
| 109 | * don't call it directly |
||
| 110 | * @param array |
||
| 111 | * @param string |
||
| 112 | * @param string |
||
| 113 | * @param string |
||
| 114 | * @return \stdClass |
||
| 115 | * @throws \jofner\SDK\TwitchTV\TwitchException |
||
| 116 | */ |
||
| 117 | private function generalRequest($uri, $method = 'GET', $postfields = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the header info to store |
||
| 170 | */ |
||
| 171 | private function getHeader($ch, $header) |
||
| 182 | } |
||
| 183 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: