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 |
||
4 | class ArrayAdaptor implements \ArrayAccess |
||
5 | { |
||
6 | /** |
||
7 | * ArrayAdaptor constructor. |
||
8 | * @param object|array $obj |
||
9 | */ |
||
10 | 7 | public function __construct(&$obj) |
|
14 | |||
15 | /** |
||
16 | * Whether a offset exists |
||
17 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
18 | * @param mixed $offset <p> |
||
19 | * An offset to check for. |
||
20 | * </p> |
||
21 | * @return boolean true on success or false on failure. |
||
22 | * </p> |
||
23 | * <p> |
||
24 | * The return value will be casted to boolean if non-boolean was returned. |
||
25 | * @since 5.0.0 |
||
26 | */ |
||
27 | 5 | public function offsetExists($offset) |
|
45 | |||
46 | /** |
||
47 | * Offset to retrieve |
||
48 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
49 | * @param mixed $offset <p> |
||
50 | * The offset to retrieve. |
||
51 | * </p> |
||
52 | * @return mixed Can return all value types. |
||
53 | * @since 5.0.0 |
||
54 | */ |
||
55 | 6 | public function offsetGet($offset) |
|
56 | { |
||
57 | 6 | $res = null; |
|
58 | 6 | View Code Duplication | if(is_array($this->obj)){ |
|
|||
59 | 2 | $res = &$this->obj[$offset]; |
|
60 | 6 | }elseif(self::hasProperty($this->obj, $offset)){ |
|
61 | 1 | $res = &$this->obj->{$offset}; |
|
62 | 4 | }elseif(method_exists($this->obj, 'get')){ |
|
63 | 2 | $res = $this->obj->get($offset); |
|
64 | 3 | }elseif(method_exists($this->obj, $method = 'get'.ucfirst($offset))){ |
|
65 | 1 | $res = $this->obj->{$method}(); |
|
66 | 1 | }else{ |
|
67 | throw new \InvalidArgumentException("offsetGet($offset) failed"); |
||
68 | } |
||
69 | 6 | if(is_array($res) || is_object($res)){ |
|
70 | 1 | return new self($res); |
|
71 | } |
||
72 | 6 | return $res; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Offset to set |
||
77 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
78 | * @param mixed $offset <p> |
||
79 | * The offset to assign the value to. |
||
80 | * </p> |
||
81 | * @param mixed $value <p> |
||
82 | * The value to set. |
||
83 | * </p> |
||
84 | * @return void |
||
85 | * @since 5.0.0 |
||
86 | */ |
||
87 | 4 | public function offsetSet($offset, $value) |
|
101 | |||
102 | /** |
||
103 | * Offset to unset |
||
104 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
105 | * @param mixed $offset <p> |
||
106 | * The offset to unset. |
||
107 | * </p> |
||
108 | * @return void |
||
109 | * @since 5.0.0 |
||
110 | */ |
||
111 | 3 | public function offsetUnset($offset) |
|
125 | 1 | static public function strip($obj){ |
|
126 | 1 | if($obj instanceof self){ |
|
127 | 1 | return $obj->obj; |
|
128 | } |
||
129 | 1 | return $obj; |
|
130 | } |
||
131 | 4 | static function hasProperty($object, $name) |
|
146 | private $obj; |
||
147 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.