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 NativeObjectGateway implements GatewayInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var object |
||
28 | */ |
||
29 | private $jsonData; |
||
30 | |||
31 | /** |
||
32 | * @var integer |
||
33 | */ |
||
34 | private $propertyCount; |
||
35 | |||
36 | /** |
||
37 | * @var array<integer,integer|string> |
||
38 | */ |
||
39 | private $properties; |
||
40 | |||
41 | /** |
||
42 | * @var integer |
||
43 | */ |
||
44 | private $currentPointer = 0; |
||
45 | |||
46 | /** |
||
47 | * @param object $jsonData |
||
48 | */ |
||
49 | 158 | function __construct($jsonData) |
|
|
|||
50 | { |
||
51 | 158 | $this->jsonData = $jsonData; |
|
52 | 158 | $this->properties = array_keys(get_object_vars($jsonData)); |
|
53 | 158 | $this->propertyCount = sizeof($this->properties); |
|
54 | 158 | } |
|
55 | |||
56 | /** |
||
57 | * (PHP 5 >= 5.0.0)<br/> |
||
58 | * Whether a offset exists |
||
59 | * |
||
60 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
61 | * |
||
62 | * @param mixed $offset <p> |
||
63 | * An offset to check for. |
||
64 | * </p> |
||
65 | * |
||
66 | * @return boolean true on success or false on failure. |
||
67 | * </p> |
||
68 | * <p> |
||
69 | * The return value will be casted to boolean if non-boolean was returned. |
||
70 | */ |
||
71 | 7 | public function offsetExists($offset) |
|
75 | |||
76 | /** |
||
77 | * (PHP 5 >= 5.0.0)<br/> |
||
78 | * Offset to retrieve |
||
79 | * |
||
80 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
81 | * |
||
82 | * @param mixed $offset <p> |
||
83 | * The offset to retrieve. |
||
84 | * </p> |
||
85 | * |
||
86 | * @return mixed Can return all value types. |
||
87 | */ |
||
88 | 144 | View Code Duplication | public function offsetGet($offset) |
89 | { |
||
90 | 144 | if (property_exists($this->jsonData, $offset)) { |
|
91 | 143 | return GatewayFactory::factory($this->jsonData->$offset); |
|
92 | } |
||
93 | |||
94 | 1 | return null; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * (PHP 5 >= 5.0.0)<br/> |
||
99 | * Offset to set |
||
100 | * |
||
101 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
102 | * |
||
103 | * @param mixed $offset <p> |
||
104 | * The offset to assign the value to. |
||
105 | * </p> |
||
106 | * @param mixed $value <p> |
||
107 | * The value to set. |
||
108 | * </p> |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | 1 | public function offsetSet($offset, $value) |
|
113 | { |
||
114 | 1 | throw new \BadMethodCallException('The result set is immutable'); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * (PHP 5 >= 5.0.0)<br/> |
||
119 | * Offset to unset |
||
120 | * |
||
121 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
122 | * |
||
123 | * @param mixed $offset <p> |
||
124 | * The offset to unset. |
||
125 | * </p> |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 1 | public function offsetUnset($offset) |
|
133 | |||
134 | /** |
||
135 | * (PHP 5 >= 5.0.0)<br/> |
||
136 | * Return the current element |
||
137 | * |
||
138 | * @link http://php.net/manual/en/iterator.current.php |
||
139 | * @return mixed Can return any type. |
||
140 | */ |
||
141 | 1 | public function current() |
|
142 | { |
||
143 | 1 | $property = $this->properties[$this->currentPointer]; |
|
144 | |||
145 | 1 | return $this->jsonData->$property; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * (PHP 5 >= 5.0.0)<br/> |
||
150 | * Move forward to next element |
||
151 | * |
||
152 | * @link http://php.net/manual/en/iterator.next.php |
||
153 | * @return void Any returned value is ignored. |
||
154 | */ |
||
155 | 1 | public function next() |
|
159 | |||
160 | /** |
||
161 | * (PHP 5 >= 5.0.0)<br/> |
||
162 | * Return the key of the current element |
||
163 | * |
||
164 | * @link http://php.net/manual/en/iterator.key.php |
||
165 | * @return mixed scalar on success, or null on failure. |
||
166 | */ |
||
167 | 1 | public function key() |
|
171 | |||
172 | /** |
||
173 | * (PHP 5 >= 5.0.0)<br/> |
||
174 | * Checks if current position is valid |
||
175 | * |
||
176 | * @link http://php.net/manual/en/iterator.valid.php |
||
177 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
178 | * Returns true on success or false on failure. |
||
179 | */ |
||
180 | 1 | public function valid() |
|
184 | |||
185 | /** |
||
186 | * (PHP 5 >= 5.0.0)<br/> |
||
187 | * Rewind the Iterator to the first element |
||
188 | * |
||
189 | * @link http://php.net/manual/en/iterator.rewind.php |
||
190 | * @return void Any returned value is ignored. |
||
191 | */ |
||
192 | 1 | public function rewind() |
|
196 | |||
197 | /** |
||
198 | * (PHP 5 >= 5.1.0)<br/> |
||
199 | * Count elements of an object |
||
200 | * |
||
201 | * @link http://php.net/manual/en/countable.count.php |
||
202 | * @return int The custom count as an integer. |
||
203 | * </p> |
||
204 | * <p> |
||
205 | * The return value is cast to an integer. |
||
206 | */ |
||
207 | 1 | public function count() |
|
211 | |||
212 | /** |
||
213 | * Returns the original value. |
||
214 | * |
||
215 | * @return mixed |
||
216 | * @author Mario Mueller |
||
217 | */ |
||
218 | 4 | public function getGatewayValue() |
|
222 | } |
||
223 |
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.