test.php ➔ test_methods()   B
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 10
nop 1
dl 0
loc 68
rs 7.1426
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// Uncomment to test
4
# run_test();
5
6
function run_test() {
0 ignored issues
show
Best Practice introduced by
The function run_test() has been defined more than once; this definition is ignored, only the first definition in require/libs/geoPHP/tests/postgis.php (L5-42) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
7
  set_time_limit(0);
8
9
  header("Content-type: text");
10
11
  include_once('../geoPHP.inc');
12
13
  if (geoPHP::geosInstalled()) {
14
    print "GEOS is installed.\n";
15
  }
16
  else {
17
    print "GEOS is not installed.\n";
18
  }
19
20
  foreach (scandir('./input') as $file) {
21
    $parts = explode('.',$file);
22
    if ($parts[0]) {
23
      $format = $parts[1];
24
      $value = file_get_contents('./input/'.$file);
25
      print '---- Testing '.$file."\n";
26
      $geometry = geoPHP::load($value, $format);
27
      test_adapters($geometry, $format, $value);
28
      test_methods($geometry);
29
      test_geometry($geometry);
30
      test_detection($value, $format, $file);
31
    }
32
  }
33
  print "Testing Done!";
34
}
35
36
function test_geometry($geometry) {
37
38
  // Test common functions
39
  $geometry->area();
40
  $geometry->boundary();
41
  $geometry->envelope();
42
  $geometry->getBBox();
43
  $geometry->centroid();
44
  $geometry->length();
45
  $geometry->greatCircleLength();
46
  $geometry->haversineLength();
47
  $geometry->y();
48
  $geometry->x();
49
  $geometry->numGeometries();
50
  $geometry->geometryN(1);
51
  $geometry->startPoint();
52
  $geometry->endPoint();
53
  $geometry->isRing();
54
  $geometry->isClosed();
55
  $geometry->numPoints();
56
  $geometry->pointN(1);
57
  $geometry->exteriorRing();
58
  $geometry->numInteriorRings();
59
  $geometry->interiorRingN(1);
60
  $geometry->dimension();
61
  $geometry->geometryType();
62
  $geometry->SRID();
63
  $geometry->setSRID(4326);
64
65
  // Aliases
66
  $geometry->getCentroid();
67
  $geometry->getArea();
68
  $geometry->getX();
69
  $geometry->getY();
70
  $geometry->getGeos();
71
  $geometry->getGeomType();
72
  $geometry->getSRID();
73
  $geometry->asText();
74
  $geometry->asBinary();
75
76
  // GEOS only functions
77
  $geometry->geos();
78
  $geometry->setGeos($geometry->geos());
79
  $geometry->pointOnSurface();
80
  $geometry->equals($geometry);
81
  $geometry->equalsExact($geometry);
82
  $geometry->relate($geometry);
83
  $geometry->checkValidity();
84
  $geometry->isSimple();
85
  $geometry->buffer(10);
86
  $geometry->intersection($geometry);
87
  $geometry->convexHull();
88
  $geometry->difference($geometry);
89
  $geometry->symDifference($geometry);
90
  $geometry->union($geometry);
91
  $geometry->simplify(0);// @@TODO: Adjust this once we can deal with empty geometries
92
  $geometry->disjoint($geometry);
93
  $geometry->touches($geometry);
94
  $geometry->intersects($geometry);
95
  $geometry->crosses($geometry);
96
  $geometry->within($geometry);
97
  $geometry->contains($geometry);
98
  $geometry->overlaps($geometry);
99
  $geometry->covers($geometry);
100
  $geometry->coveredBy($geometry);
101
  $geometry->distance($geometry);
102
  $geometry->hausdorffDistance($geometry);
103
104
105
  // Place holders
106
  $geometry->hasZ();
107
  $geometry->is3D();
108
  $geometry->isMeasured();
109
  $geometry->isEmpty();
110
  $geometry->coordinateDimension();
111
  $geometry->z();
112
  $geometry->m();
113
}
114
115
function test_adapters($geometry, $format, $input) {
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
  // Test adapter output and input. Do a round-trip and re-test
117
  foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) {
118
    if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test
119
      $output = $geometry->out($adapter_key);
120
      if ($output) {
121
        $adapter_loader = new $adapter_class();
122
        $test_geom_1 = $adapter_loader->read($output);
123
        $test_geom_2 = $adapter_loader->read($test_geom_1->out($adapter_key));
124
125
        if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) {
126
          print "Mismatched adapter output in ".$adapter_class."\n";
127
        }
128
      }
129
      else {
130
        print "Empty output on "  . $adapter_key . "\n";
131
      }
132
    }
133
  }
134
135
  // Test to make sure adapter work the same wether GEOS is ON or OFF
136
  // Cannot test methods if GEOS is not intstalled
137
  if (!geoPHP::geosInstalled()) return;
138
139
  foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) {
140
    if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test
141
      // Turn GEOS on
142
      geoPHP::geosInstalled(TRUE);
143
144
      $output = $geometry->out($adapter_key);
145
      if ($output) {
146
        $adapter_loader = new $adapter_class();
147
148
        $test_geom_1 = $adapter_loader->read($output);
149
150
        // Turn GEOS off
151
        geoPHP::geosInstalled(FALSE);
152
153
        $test_geom_2 = $adapter_loader->read($output);
154
155
        // Turn GEOS back On
156
        geoPHP::geosInstalled(TRUE);
157
158
        // Check to make sure a both are the same with geos and without
159
        if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) {
160
          print "Mismatched adapter output between GEOS and NORM in ".$adapter_class."\n";
161
        }
162
      }
163
    }
164
  }
165
}
166
167
168
function test_methods($geometry) {
169
  // Cannot test methods if GEOS is not intstalled
170
  if (!geoPHP::geosInstalled()) return;
171
172
  $methods = array(
173
    //'boundary', //@@TODO: Uncomment this and fix errors
174
    'envelope',   //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON
175
    'getBBox',
176
    'x',
177
    'y',
178
    'startPoint',
179
    'endPoint',
180
    'isRing',
181
    'isClosed',
182
    'numPoints',
183
  );
184
185
  foreach ($methods as $method) {
186
    // Turn GEOS on
187
    geoPHP::geosInstalled(TRUE);
188
    $geos_result = $geometry->$method();
189
190
    // Turn GEOS off
191
    geoPHP::geosInstalled(FALSE);
192
    $norm_result = $geometry->$method();
193
194
    // Turn GEOS back On
195
    geoPHP::geosInstalled(TRUE);
196
197
    $geos_type = gettype($geos_result);
198
    $norm_type = gettype($norm_result);
199
200
    if ($geos_type != $norm_type) {
201
      print 'Type mismatch on '.$method."\n";
202
      continue;
203
    }
204
205
    // Now check base on type
206
    if ($geos_type == 'object') {
207
      $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt'));
208
209
      // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance
210
      // Using Pythagorean theorem
211
      $bb = $geos_result->getBBox();
212
      $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2));
213
214
      // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units
215
      if ($haus_dist / $scale > 0.5) {
216
        print 'Output mismatch on '.$method.":\n";
217
        print 'GEOS : '.$geos_result->out('wkt')."\n";
218
        print 'NORM : '.$norm_result->out('wkt')."\n";
219
        continue;
220
      }
221
    }
222
223
    if ($geos_type == 'boolean' || $geos_type == 'string') {
224
      if ($geos_result !== $norm_result) {
225
        print 'Output mismatch on '.$method.":\n";
226
        print 'GEOS : '.(string) $geos_result."\n";
227
        print 'NORM : '.(string) $norm_result."\n";
228
        continue;
229
      }
230
    }
231
232
    //@@TODO: Run tests for output of types arrays and float
233
    //@@TODO: centroid function is non-compliant for collections and strings
234
  }
235
}
236
237
function test_detection($value, $format, $file) {
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
238
  $detected = geoPHP::detectFormat($value);
239
  if ($detected != $format) {
240
    if ($detected) print 'detected as ' . $detected . "\n";
241
    else print "not detected\n";
242
  }
243
  // Make sure it loads using auto-detect
244
  geoPHP::load($value);
245
}
246