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 |
||
| 5 | class CoOption implements \ArrayAccess |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Field types. |
||
| 9 | */ |
||
| 10 | const TYPES = [ |
||
| 11 | 'throw' => 'Bool', // Throw CURLExceptions? |
||
| 12 | 'pipeline' => 'Bool', // Use HTTP/1.1 pipelining? |
||
| 13 | 'multiplex' => 'Bool', // Use HTTP/2 multiplexing? |
||
| 14 | 'interval' => 'NaturalFloat', // curl_multi_select() timeout |
||
| 15 | 'concurrency' => 'NaturalInt', // Limit of TCP connections |
||
| 16 | ]; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Default values. |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private static $defaults = [ |
||
| 23 | 'throw' => true, |
||
| 24 | 'pipeline' => false, |
||
| 25 | 'multiplex' => true, |
||
| 26 | 'interval' => 0.5, |
||
| 27 | 'concurrency' => 6, |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Actual values. |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $options; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Set default options. |
||
| 38 | * @param array $options |
||
| 39 | */ |
||
| 40 | 8 | public static function setDefault(array $options) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Get default options. |
||
| 47 | * @return array $options |
||
| 48 | */ |
||
| 49 | 8 | public static function getDefault() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor. |
||
| 56 | * @param array $options |
||
| 57 | */ |
||
| 58 | 31 | public function __construct(array $options = []) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Reconfigure to get new instance. |
||
| 65 | * @return CoOption |
||
| 66 | */ |
||
| 67 | 5 | public function reconfigure(array $options) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Implemention of ArrayAccess. |
||
| 74 | * @param mixed $offset |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | 1 | public function offsetExists($offset) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Implemention of ArrayAccess. |
||
| 84 | * @param mixed $offset |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | 22 | public function offsetGet($offset) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Implemention of ArrayAccess. |
||
| 97 | * @param mixed $offset |
||
| 98 | * @param mixed $value |
||
| 99 | * @throws BadMethodCallException |
||
| 100 | */ |
||
| 101 | 1 | public function offsetSet($offset, $value) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Implemention of ArrayAccess. |
||
| 108 | * @param mixed $offset |
||
| 109 | * @throws BadMethodCallException |
||
| 110 | */ |
||
| 111 | 1 | public function offsetUnset($offset) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Validate options. |
||
| 118 | * @param array $options |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 36 | private static function validateOptions(array $options) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Validate bool value. |
||
| 135 | * @param string $key |
||
| 136 | * @param mixed $value |
||
| 137 | * @throws InvalidArgumentException |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 16 | private static function validateBool($key, $value) |
|
| 141 | 16 | { |
|
| 142 | 16 | $value = filter_var($value, FILTER_VALIDATE_BOOLEAN, [ |
|
| 143 | 16 | 'flags' => FILTER_NULL_ON_FAILURE, |
|
| 144 | ]); |
||
| 145 | 16 | if ($value === null) { |
|
| 146 | 1 | throw new \InvalidArgumentException("Option[$key] must be boolean."); |
|
| 147 | } |
||
| 148 | 16 | return $value; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Validate natural float value. |
||
| 153 | * @param string $key |
||
| 154 | * @param mixed $value |
||
| 155 | * @throws InvalidArgumentException |
||
| 156 | * @return float |
||
| 157 | */ |
||
| 158 | 11 | View Code Duplication | private static function validateNaturalFloat($key, $value) |
| 166 | |||
| 167 | /** |
||
| 168 | * Validate natural int value. |
||
| 169 | * @param string $key |
||
| 170 | * @param mixed $value |
||
| 171 | * @throws InvalidArgumentException |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | 15 | View Code Duplication | private static function validateNaturalInt($key, $value) |
| 182 | } |
||
| 183 |