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 VcrHandler |
||
| 15 | { |
||
| 16 | const CONFIG_ONLY_ENCODE_BINARY = 'only_encode_binary'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $cassette; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Configuration values |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected static $config; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $cassette fixture path |
||
| 32 | * @return \GuzzleHttp\HandlerStack |
||
| 33 | */ |
||
| 34 | 5 | public static function turnOn($cassette, array $config = null) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | * |
||
| 60 | * @param string $cassette fixture path |
||
| 61 | */ |
||
| 62 | 4 | protected function __construct($cassette) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Returns configuration value by name |
||
| 69 | * |
||
| 70 | * @param string $name Name of configuration value |
||
| 71 | * |
||
| 72 | * @return null|mixed |
||
| 73 | */ |
||
| 74 | 5 | protected static function getConfig($name = null) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Returns True if response content is binary |
||
| 89 | * |
||
| 90 | * @param GuzzleHttp\Psr7\Response $response Response object |
||
| 91 | * |
||
| 92 | * @return boolean |
||
| 93 | */ |
||
| 94 | protected static function isBinary(Response $response) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Resolve an object Response from given value. |
||
| 102 | * If value is already a Response returns value. |
||
| 103 | * If value is an array attempts to create a Response object |
||
| 104 | * otherwise throws an exception. |
||
| 105 | * |
||
| 106 | * @param Response|array $value |
||
| 107 | * |
||
| 108 | * @throws \InvalidArgumentException |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | 5 | protected static function ensureResponse($value) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Encode body content from given response. |
||
| 133 | * |
||
| 134 | * @param Response|array $response Response value |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 4 | View Code Duplication | protected static function encodeBodyFrom($response) |
| 151 | |||
| 152 | /** |
||
| 153 | * Decode body content from given response. |
||
| 154 | * |
||
| 155 | * @param Response|array $response Response value |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 5 | View Code Duplication | protected static function decodeBodyFrom($response) |
| 172 | |||
| 173 | /** |
||
| 174 | * Decodes every responses body from base64 |
||
| 175 | * |
||
| 176 | * @param $cassette |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | 5 | protected static function decodeResponses($cassette) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Handle the request/response |
||
| 194 | * |
||
| 195 | * @param callable $handler |
||
| 196 | * @return \Closure |
||
| 197 | */ |
||
| 198 | 4 | public function __invoke(callable $handler) |
|
| 226 | } |
||
| 227 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.