Complex classes like MapAPITest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MapAPITest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class MapAPITest extends SapphireTest { |
||
4 | |||
5 | public function setUpOnce() { |
||
11 | |||
12 | public function testSetKey() { |
||
15 | |||
16 | |||
17 | public function testSetIncludeDownloadJavascript() { |
||
20 | |||
21 | |||
22 | public function testSetShowInlineMapDivStyle() { |
||
25 | |||
26 | |||
27 | public function testSetAdditionalCSSClasses() { |
||
34 | |||
35 | |||
36 | public function testSetMapStyle() { |
||
39 | |||
40 | |||
41 | public function testSetDelayLoadMapFunction() { |
||
44 | |||
45 | |||
46 | public function testSetDivId() { |
||
53 | |||
54 | |||
55 | public function testSetDirectionDivId() { |
||
58 | |||
59 | |||
60 | public function testSetSize() { |
||
66 | |||
67 | |||
68 | public function testSetIconSize() { |
||
71 | |||
72 | |||
73 | public function testSetLang() { |
||
79 | |||
80 | |||
81 | public function testSetZoom() { |
||
90 | |||
91 | |||
92 | public function testSetInfoWindowZoom() { |
||
95 | |||
96 | |||
97 | public function testSetEnableWindowZoom() { |
||
100 | |||
101 | |||
102 | public function testSetEnableAutomaticCenterZoom() { |
||
108 | |||
109 | |||
110 | public function testSetCenter() { |
||
119 | |||
120 | |||
121 | public function testSetLatLongCenter() { |
||
130 | |||
131 | |||
132 | public function testSetMapType() { |
||
157 | |||
158 | |||
159 | public function testSetAllowFullScreen() { |
||
171 | |||
172 | |||
173 | public function testSetDisplayDirectionFields() { |
||
184 | |||
185 | |||
186 | public function testMapWithMarkers() { |
||
195 | |||
196 | |||
197 | public function testMapWithMarkersDifferentCategory() { |
||
200 | |||
201 | |||
202 | public function testSetDefaultHideMarker() { |
||
205 | |||
206 | |||
207 | public function testgetGoogleMap() { |
||
210 | |||
211 | |||
212 | public function testgetContent() { |
||
215 | |||
216 | |||
217 | public function testgeocoding() { |
||
220 | |||
221 | public function testaddMarkerByCoords() { |
||
224 | |||
225 | |||
226 | public function testaddMarkerByAddress() { |
||
229 | |||
230 | |||
231 | public function testaddArrayMarkerByCoords() { |
||
234 | |||
235 | |||
236 | public function testaddMarkerAsObject() { |
||
239 | |||
240 | |||
241 | public function testconnectPoints() { |
||
244 | |||
245 | |||
246 | public function testaddArrayMarkerByAddress() { |
||
249 | |||
250 | |||
251 | public function testaddDirection() { |
||
254 | |||
255 | |||
256 | public function testaddKML() { |
||
259 | |||
260 | |||
261 | public function testaddLine() { |
||
264 | |||
265 | |||
266 | public function testjsonRemoveUnicodeSequences() { |
||
269 | |||
270 | |||
271 | public function testprocessTemplateJS() { |
||
274 | |||
275 | |||
276 | public function testprocessTemplateHTML() { |
||
279 | |||
280 | |||
281 | |||
282 | |||
283 | private function getMap() { |
||
287 | |||
288 | private function getMapMultipleItems() { |
||
309 | |||
310 | } |
||
311 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: