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 | |||
| 174 | |||
| 175 | |||
| 176 | public function testSetDisplayDirectionFields() { |
||
| 179 | |||
| 180 | |||
| 181 | public function testSetDefaultHideMarker() { |
||
| 184 | |||
| 185 | |||
| 186 | public function testgetGoogleMap() { |
||
| 189 | |||
| 190 | |||
| 191 | public function testgetContent() { |
||
| 194 | |||
| 195 | |||
| 196 | public function testgeocoding() { |
||
| 199 | |||
| 200 | public function testaddMarkerByCoords() { |
||
| 203 | |||
| 204 | |||
| 205 | public function testaddMarkerByAddress() { |
||
| 208 | |||
| 209 | |||
| 210 | public function testaddArrayMarkerByCoords() { |
||
| 213 | |||
| 214 | |||
| 215 | public function testaddMarkerAsObject() { |
||
| 218 | |||
| 219 | |||
| 220 | public function testconnectPoints() { |
||
| 223 | |||
| 224 | |||
| 225 | public function testforTemplate() { |
||
| 228 | |||
| 229 | |||
| 230 | public function testaddArrayMarkerByAddress() { |
||
| 233 | |||
| 234 | |||
| 235 | public function testaddDirection() { |
||
| 238 | |||
| 239 | |||
| 240 | public function testaddKML() { |
||
| 243 | |||
| 244 | |||
| 245 | public function testaddLine() { |
||
| 248 | |||
| 249 | |||
| 250 | public function testjsonRemoveUnicodeSequences() { |
||
| 253 | |||
| 254 | |||
| 255 | public function testgenerate() { |
||
| 258 | |||
| 259 | |||
| 260 | public function testprocessTemplateJS() { |
||
| 263 | |||
| 264 | |||
| 265 | public function testprocessTemplateHTML() { |
||
| 268 | |||
| 269 | private function getMap() { |
||
| 273 | |||
| 274 | } |
||
| 275 |
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: