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 |
||
| 24 | class XoopsArray extends \ArrayObject implements AttributeInterface |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Retrieve an attribute |
||
| 29 | * |
||
| 30 | * @param string $name Name of the attribute |
||
| 31 | * @param mixed $default A default value returned if the requested attribute is not set. |
||
| 32 | * |
||
| 33 | * @return mixed The value of the attribute, or null if not set. |
||
| 34 | */ |
||
| 35 | 42 | public function get($name, $default = null) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Set an item attribute |
||
| 46 | * |
||
| 47 | * @param string $name Name of the attribute |
||
| 48 | * @param mixed $value Value for the attribute |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | 13 | public function set($name, $value) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * has - test if an attribute with a given name is set |
||
| 60 | * |
||
| 61 | * @param string $name Name of the attribute |
||
| 62 | * |
||
| 63 | * @return boolean true if named attribute is set |
||
| 64 | */ |
||
| 65 | 4 | public function has($name) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Remove an attribute. |
||
| 72 | * |
||
| 73 | * @param string $name An attribute name. |
||
| 74 | * |
||
| 75 | * @return mixed An attribute value, if the named attribute existed and |
||
| 76 | * has been removed, otherwise NULL. |
||
| 77 | */ |
||
| 78 | 5 | View Code Duplication | public function remove($name) |
| 88 | |||
| 89 | /** |
||
| 90 | * Remove all attributes. |
||
| 91 | * |
||
| 92 | * @return array old values |
||
| 93 | */ |
||
| 94 | 3 | public function clear() |
|
| 98 | } |
||
| 99 |