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 |
||
| 19 | class Moip |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * endpoint of production. |
||
| 23 | * |
||
| 24 | * @const string |
||
| 25 | */ |
||
| 26 | const ENDPOINT_PRODUCTION = 'https://api.moip.com.br'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * endpoint of sandbox. |
||
| 30 | * |
||
| 31 | * @const string |
||
| 32 | */ |
||
| 33 | const ENDPOINT_SANDBOX = 'https://sandbox.moip.com.br'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Client name. |
||
| 37 | * |
||
| 38 | * @const string |
||
| 39 | * */ |
||
| 40 | const CLIENT = 'Moip SDK'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Authentication that will be added to the header of request. |
||
| 44 | * |
||
| 45 | * @var \Moip\MoipAuthentication |
||
| 46 | */ |
||
| 47 | private $moipAuthentication; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Endpoint of request. |
||
| 51 | * |
||
| 52 | * @var \Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX |
||
| 53 | */ |
||
| 54 | private $endpoint; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Requests_Session HTTP session configured to use the moip API. |
||
| 58 | */ |
||
| 59 | private $session; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create a new aurhentication with the endpoint. |
||
| 63 | * |
||
| 64 | * @param \Moip\Auth\MoipAuthentication $moipAuthentication |
||
| 65 | * @param string $endpoint |
||
| 66 | */ |
||
| 67 | public function __construct(Authentication $moipAuthentication, $endpoint = self::ENDPOINT_PRODUCTION) |
||
| 68 | { |
||
| 69 | $this->moipAuthentication = $moipAuthentication; |
||
|
|
|||
| 70 | $this->endpoint = $endpoint; |
||
| 71 | $this->createNewSession(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates a new Request_Session with all the default values. |
||
| 76 | * A Session is created at construction. |
||
| 77 | * |
||
| 78 | * @param float $timeout How long should we wait for a response?(seconds with a millisecond precision, default: 30, example: 0.01). |
||
| 79 | * @param float $connect_timeout How long should we wait while trying to connect? (seconds with a millisecond precision, default: 10, example: 0.01) |
||
| 80 | */ |
||
| 81 | public function createNewSession($timeout = 30.0, $connect_timeout = 30.0) |
||
| 82 | { |
||
| 83 | View Code Duplication | if (function_exists('posix_uname')) { |
|
| 84 | $uname = posix_uname(); |
||
| 85 | $user_agent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s %s; %s; %s)', self::CLIENT, PHP_SAPI, PHP_VERSION, $uname['sysname'], $uname['machine']); |
||
| 86 | } else { |
||
| 87 | $user_agent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s %s; %s)', self::CLIENT, PHP_SAPI, PHP_VERSION, PHP_OS); |
||
| 88 | } |
||
| 89 | $sess = new Requests_Session($this->endpoint); |
||
| 90 | $sess->options['auth'] = $this->moipAuthentication; |
||
| 91 | $sess->options['timeout'] = $timeout; |
||
| 92 | $sess->options['connect_timeout'] = $connect_timeout; |
||
| 93 | $sess->options['useragent'] = $user_agent; |
||
| 94 | $this->session = $sess; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the http session created. |
||
| 99 | * |
||
| 100 | * @return Requests_Session |
||
| 101 | */ |
||
| 102 | public function getSession() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Replace the http session by a custom one. |
||
| 109 | * |
||
| 110 | * @param Requests_Session $session |
||
| 111 | */ |
||
| 112 | public function setSession($session) |
||
| 113 | { |
||
| 114 | $this->session = $session; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Create a new Customer instance. |
||
| 119 | * |
||
| 120 | * @return \Moip\Resource\Customer |
||
| 121 | */ |
||
| 122 | public function customers() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Create a new Account instance. |
||
| 129 | * |
||
| 130 | * @return \Moip\Resource\Account |
||
| 131 | */ |
||
| 132 | public function accounts() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Create a new Entry instance. |
||
| 139 | * |
||
| 140 | * @return \Moip\Resource\Entry |
||
| 141 | */ |
||
| 142 | public function entries() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Create a new Orders instance. |
||
| 149 | * |
||
| 150 | * @return \Moip\Resource\Orders |
||
| 151 | */ |
||
| 152 | public function orders() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Create a new Payment instance. |
||
| 159 | * |
||
| 160 | * @return \Moip\Resource\Payment |
||
| 161 | */ |
||
| 162 | public function payments() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Create a new Multiorders instance. |
||
| 169 | * |
||
| 170 | * @return \Moip\Resource\Multiorders |
||
| 171 | */ |
||
| 172 | public function multiorders() |
||
| 176 | /** |
||
| 177 | * Create a new Transfers. |
||
| 178 | * |
||
| 179 | * @return \Moip\Resource\Transfers |
||
| 180 | */ |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Create a new Transfers instance. |
||
| 184 | * |
||
| 185 | * @return Transfers |
||
| 186 | */ |
||
| 187 | public function transfers() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Create a new Notification Prefences instance. |
||
| 194 | * |
||
| 195 | * @return NotificationPreferences |
||
| 196 | */ |
||
| 197 | public function notifications() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the endpoint. |
||
| 204 | * |
||
| 205 | * @return \Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX |
||
| 206 | */ |
||
| 207 | public function getEndpoint() |
||
| 211 | } |
||
| 212 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..