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 |
||
28 | class NativeArrayGateway implements GatewayInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $jsonData; |
||
34 | |||
35 | /** |
||
36 | * @param array $jsonData The raw data. |
||
37 | */ |
||
38 | 192 | function __construct(array $jsonData) |
|
42 | |||
43 | /** |
||
44 | * Whether a offset exists |
||
45 | * |
||
46 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
47 | * |
||
48 | * @param mixed $offset An offset to check for. |
||
49 | * |
||
50 | * @return boolean true on success or false on failure. |
||
51 | */ |
||
52 | 10 | public function offsetExists($offset) |
|
56 | |||
57 | /** |
||
58 | * Offset to retrieve |
||
59 | * |
||
60 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
61 | * |
||
62 | * @param mixed $offset The offset to retrieve. |
||
63 | * |
||
64 | * @return mixed Can return all value types. |
||
65 | */ |
||
66 | 143 | View Code Duplication | public function offsetGet($offset) |
67 | { |
||
68 | 143 | if (isset($this->jsonData[$offset])) { |
|
69 | 142 | return GatewayFactory::factory($this->jsonData[$offset]); |
|
70 | } |
||
71 | |||
72 | 1 | return null; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Offset to set |
||
77 | * |
||
78 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
79 | * |
||
80 | * @param mixed $offset The offset to assign the value to. |
||
81 | * @param mixed $value The value to set. |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | 1 | public function offsetSet($offset, $value) |
|
86 | { |
||
87 | 1 | throw new \BadMethodCallException('The result set is immutable'); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Offset to unset |
||
92 | * |
||
93 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
94 | * |
||
95 | * @param mixed $offset The offset to unset. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | 1 | public function offsetUnset($offset) |
|
100 | { |
||
101 | 1 | throw new \BadMethodCallException('The result set is immutable'); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Return the current element |
||
106 | * |
||
107 | * @link http://php.net/manual/en/iterator.current.php |
||
108 | * @return mixed Can return any type. |
||
109 | */ |
||
110 | 1 | public function current() |
|
114 | |||
115 | /** |
||
116 | * Move forward to next element |
||
117 | * |
||
118 | * @link http://php.net/manual/en/iterator.next.php |
||
119 | * @return void Any returned value is ignored. |
||
120 | */ |
||
121 | 1 | public function next() |
|
125 | |||
126 | /** |
||
127 | * Return the key of the current element |
||
128 | * |
||
129 | * @link http://php.net/manual/en/iterator.key.php |
||
130 | * @return mixed scalar on success, or null on failure. |
||
131 | */ |
||
132 | 1 | public function key() |
|
136 | |||
137 | /** |
||
138 | * Checks if current position is valid |
||
139 | * |
||
140 | * @link http://php.net/manual/en/iterator.valid.php |
||
141 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
142 | */ |
||
143 | 1 | public function valid() |
|
144 | { |
||
145 | 1 | return current($this->jsonData) ? true : false; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Rewind the Iterator to the first element |
||
150 | * |
||
151 | * @link http://php.net/manual/en/iterator.rewind.php |
||
152 | * @return void Any returned value is ignored. |
||
153 | */ |
||
154 | 1 | public function rewind() |
|
158 | |||
159 | /** |
||
160 | * Count elements of an object |
||
161 | * |
||
162 | * @link http://php.net/manual/en/countable.count.php |
||
163 | * @return int The custom count as an integer. The return value is cast to an integer. |
||
164 | */ |
||
165 | 1 | public function count() |
|
166 | { |
||
167 | 1 | return sizeof($this->jsonData); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Returns the original value. |
||
172 | * |
||
173 | * @return mixed |
||
174 | * @author Mario Mueller |
||
175 | */ |
||
176 | 80 | public function getGatewayValue() |
|
180 | } |
||
181 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.