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 | * @var array |
||
| 10 | */ |
||
| 11 | private static $types = [ |
||
| 12 | 'throw' => 'Bool', // Throw CURLExceptions? |
||
| 13 | 'pipeline' => 'Bool', // Use HTTP/1.1 pipelining? |
||
| 14 | 'multiplex' => 'Bool', // Use HTTP/2 multiplexing? |
||
| 15 | 'autoschedule' => 'Bool', // Use AutoScheduler? |
||
| 16 | 'interval' => 'NaturalFloat', // curl_multi_select() timeout |
||
| 17 | 'concurrency' => 'NaturalInt', // Limit of TCP connections |
||
| 18 | ]; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default values. |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private static $defaults = [ |
||
| 25 | 'throw' => true, |
||
| 26 | 'pipeline' => false, |
||
| 27 | 'multiplex' => true, |
||
| 28 | 'autoschedule' => false, |
||
| 29 | 'interval' => 0.002, |
||
| 30 | 'concurrency' => 6, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Actual values. |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $options; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Set default options. |
||
| 41 | * @param array $options |
||
| 42 | */ |
||
| 43 | 8 | public static function setDefault(array $options) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Get default options. |
||
| 50 | * @return array $options |
||
| 51 | */ |
||
| 52 | 8 | public static function getDefault() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor. |
||
| 59 | * @param array $options |
||
| 60 | */ |
||
| 61 | 45 | public function __construct(array $options = []) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Reconfigure to get new instance. |
||
| 68 | * @return CoOption |
||
| 69 | */ |
||
| 70 | 1 | public function reconfigure(array $options) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Implemention of ArrayAccess. |
||
| 77 | * @param mixed $offset |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 1 | public function offsetExists($offset) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Implemention of ArrayAccess. |
||
| 87 | * @param mixed $offset |
||
| 88 | * @return mixed |
||
| 89 | */ |
||
| 90 | 42 | public function offsetGet($offset) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Implemention of ArrayAccess. |
||
| 100 | * @param mixed $offset |
||
| 101 | * @param mixed $value |
||
| 102 | * @throws BadMethodCallException |
||
| 103 | */ |
||
| 104 | 1 | public function offsetSet($offset, $value) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Implemention of ArrayAccess. |
||
| 111 | * @param mixed $offset |
||
| 112 | * @throws BadMethodCallException |
||
| 113 | */ |
||
| 114 | 1 | public function offsetUnset($offset) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Validate options. |
||
| 121 | * @param array $options |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 50 | private static function validateOptions(array $options) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Validate bool value. |
||
| 141 | * @param string $key |
||
| 142 | * @param mixed $value |
||
| 143 | * @throws InvalidArgumentException |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | 16 | private static function validateBool($key, $value) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Validate natural float value. |
||
| 159 | * @param string $key |
||
| 160 | * @param mixed $value |
||
| 161 | * @throws InvalidArgumentException |
||
| 162 | * @return float |
||
| 163 | */ |
||
| 164 | 7 | View Code Duplication | private static function validateNaturalFloat($key, $value) |
| 175 | |||
| 176 | /** |
||
| 177 | * Validate natural int value. |
||
| 178 | * @param string $key |
||
| 179 | * @param mixed $value |
||
| 180 | * @throws InvalidArgumentException |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | 17 | View Code Duplication | private static function validateNaturalInt($key, $value) |
| 194 | } |
||
| 195 |