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 |
||
| 20 | class Request extends AbstractRequest |
||
| 21 | { |
||
| 22 | protected $responseClass = Response::class; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array default cURL options |
||
| 26 | */ |
||
| 27 | public $defaultOptions = [ |
||
| 28 | CURLOPT_SSL_VERIFYPEER => false, |
||
| 29 | CURLOPT_RETURNTRANSFER => true, |
||
| 30 | CURLOPT_HEADER => true, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $options |
||
| 35 | * @throws RequestErrorException |
||
| 36 | * @return array|mixed |
||
| 37 | */ |
||
| 38 | public function send($options = []) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param array $options |
||
| 61 | * @throws RequestErrorException |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | protected function prepareCurlOptions($options) |
||
| 65 | { |
||
| 66 | $requestOptions = $this->buildMethodOptions(); |
||
| 67 | $requestOptions[CURLOPT_HTTPHEADER] = $this->buildHeaderLines(); |
||
| 68 | |||
| 69 | if ($this->getVersion() === '1.1') { |
||
| 70 | $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; |
||
| 71 | } elseif ($this->getVersion() === '1.0') { |
||
| 72 | $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0; |
||
| 73 | } else { |
||
| 74 | throw new RequestErrorException('Request version "' . $this->getVersion() . '" is not support by cURL', $this); |
||
| 75 | } |
||
| 76 | |||
| 77 | return ArrayHelper::merge($this->defaultOptions, $this->getDb()->config, $requestOptions, $options); |
||
|
|
|||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | protected function buildMethodOptions() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | View Code Duplication | protected function buildHeaderLines() |
|
| 123 | } |
||
| 124 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: