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 |
||
| 6 | class AnnotationParams implements \Countable, \ArrayAccess |
||
| 7 | { |
||
| 8 | 22 | public function __construct($text, $limit) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * 普通状态 |
||
| 40 | */ |
||
| 41 | 21 | private function stateNormal($text, $pos, &$next) |
|
| 70 | /** |
||
| 71 | * 进入空格状态 |
||
| 72 | */ |
||
| 73 | 7 | private function stateSpace($text, $pos, &$next) |
|
| 86 | // /** |
||
| 87 | // * 进入单引号状态 |
||
| 88 | // */ |
||
| 89 | // private function stateSingleQ($text, $pos, &$next){ |
||
| 90 | // |
||
| 91 | // $found = []; |
||
| 92 | // $todo = substr($text,$pos); |
||
| 93 | // if(!preg_match('/[\\\\\']/', $todo, $found, PREG_OFFSET_CAPTURE) || |
||
| 94 | // count($found)==0){ |
||
| 95 | // return false; |
||
| 96 | // } |
||
| 97 | // list($chars, $offset) = $found[0]; |
||
| 98 | // if($chars == '\\'){ |
||
| 99 | // return $pos+$offset+2; |
||
| 100 | // }else{ |
||
| 101 | // $next = 'stateNormal'; |
||
| 102 | // return $pos+$offset+1; |
||
| 103 | // } |
||
| 104 | // } |
||
| 105 | /** |
||
| 106 | * 进入双引号状态 |
||
| 107 | */ |
||
| 108 | 3 | private function stateDoubleQ($text, $pos, &$next){ |
|
| 124 | |||
| 125 | 21 | public function count() |
|
| 129 | |||
| 130 | 22 | public function getParam($pos, $default = null, $ignoreError=false) |
|
| 144 | |||
| 145 | 6 | public function getRawParam($pos, $default = null) |
|
| 153 | |||
| 154 | 22 | private function stripSlashes($text, $ignoreError) |
|
| 169 | private $cachedParams = []; |
||
| 170 | private $rawParams = []; |
||
| 171 | private $prePos = 0; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Whether a offset exists |
||
| 175 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 176 | * @param mixed $offset <p> |
||
| 177 | * An offset to check for. |
||
| 178 | * </p> |
||
| 179 | * @return boolean true on success or false on failure. |
||
| 180 | * </p> |
||
| 181 | * <p> |
||
| 182 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 183 | * @since 5.0.0 |
||
| 184 | */ |
||
| 185 | public function offsetExists($offset) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Offset to retrieve |
||
| 192 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 193 | * @param mixed $offset <p> |
||
| 194 | * The offset to retrieve. |
||
| 195 | * </p> |
||
| 196 | * @return mixed Can return all value types. |
||
| 197 | * @since 5.0.0 |
||
| 198 | */ |
||
| 199 | 17 | public function offsetGet($offset) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Offset to set |
||
| 206 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 207 | * @param mixed $offset <p> |
||
| 208 | * The offset to assign the value to. |
||
| 209 | * </p> |
||
| 210 | * @param mixed $value <p> |
||
| 211 | * The value to set. |
||
| 212 | * </p> |
||
| 213 | * @return void |
||
| 214 | * @since 5.0.0 |
||
| 215 | */ |
||
| 216 | public function offsetSet($offset, $value) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Offset to unset |
||
| 223 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 224 | * @param mixed $offset <p> |
||
| 225 | * The offset to unset. |
||
| 226 | * </p> |
||
| 227 | * @return void |
||
| 228 | * @since 5.0.0 |
||
| 229 | */ |
||
| 230 | public function offsetUnset($offset) |
||
| 234 | } |
||
| 235 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.