Skip to content

Commit 511af45

Browse files
authored
Fixed code smell and clean ups (#713)
* Fixed suggestions from Scrutinizer * Added final to classes * Bugfixes * Removed risky tests * Updated doc block
1 parent 3b66016 commit 511af45

28 files changed

+170
-192
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"php-http/curl-client": "^1.7",
3131
"php-http/mock-client": "^1.0",
3232
"nyholm/psr7": "^0.2.2",
33+
"nyholm/nsa": "^1.1",
3334
"cache/simple-cache-bridge": "^0.1.1",
3435
"cache/array-adapter": "^0.5.0"
3536
},

src/Common/Assert.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,52 @@ class Assert
2020
* @param float $value
2121
* @param string $message
2222
*/
23-
public static function latitude($value, $message = '')
23+
public static function latitude($value, string $message = '')
2424
{
25-
if (!is_float($value)) {
26-
throw new InvalidArgument(
27-
sprintf($message ?: 'Expected a double. Got: %s', self::typeToString($value))
28-
);
29-
}
30-
25+
self::float($value, $message);
3126
if ($value < -90 || $value > 90) {
32-
throw new InvalidArgument(
33-
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
34-
);
27+
throw new InvalidArgument(sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value));
3528
}
3629
}
3730

3831
/**
3932
* @param float $value
4033
* @param string $message
4134
*/
42-
public static function longitude($value, $message = '')
35+
public static function longitude($value, string $message = '')
4336
{
44-
if (!is_float($value)) {
45-
throw new InvalidArgument(
46-
sprintf($message ?: 'Expected a doable. Got: %s', self::typeToString($value))
47-
);
48-
}
49-
37+
self::float($value, $message);
5038
if ($value < -180 || $value > 180) {
51-
throw new InvalidArgument(
52-
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
53-
);
39+
throw new InvalidArgument(sprintf($message ?: 'Longitude should be between -180 and 180. Got: %s', $value));
5440
}
5541
}
5642

5743
/**
5844
* @param mixed $value
5945
* @param string $message
6046
*/
61-
public static function notNull($value, $message = '')
47+
public static function notNull($value, string $message = '')
6248
{
6349
if (null === $value) {
6450
throw new InvalidArgument(sprintf($message ?: 'Value cannot be null'));
6551
}
6652
}
6753

68-
private static function typeToString($value)
54+
private static function typeToString($value): string
6955
{
7056
return is_object($value) ? get_class($value) : gettype($value);
7157
}
58+
59+
/**
60+
* @param $value
61+
* @param $message
62+
*/
63+
private static function float($value, string $message)
64+
{
65+
if (!is_float($value)) {
66+
throw new InvalidArgument(
67+
sprintf($message ?: 'Expected a float. Got: %s', self::typeToString($value))
68+
);
69+
}
70+
}
7271
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Dumper;
14+
15+
use Geocoder\Location;
16+
17+
/**
18+
* @author Tomas Norkūnas <[email protected]>
19+
*/
20+
abstract class AbstractArrayDumper
21+
{
22+
/**
23+
* @param Location $location
24+
*
25+
* @return array
26+
*/
27+
protected function getArray(Location $location): array
28+
{
29+
$properties = array_filter($location->toArray(), function ($value) {
30+
return !empty($value);
31+
});
32+
33+
unset(
34+
$properties['latitude'],
35+
$properties['longitude'],
36+
$properties['bounds']
37+
);
38+
39+
if (0 === count($properties)) {
40+
$properties = null;
41+
}
42+
43+
$lat = 0;
44+
$lon = 0;
45+
if (null !== $coordinates = $location->getCoordinates()) {
46+
$lat = $coordinates->getLatitude();
47+
$lon = $coordinates->getLongitude();
48+
}
49+
50+
$array = [
51+
'type' => 'Feature',
52+
'geometry' => [
53+
'type' => 'Point',
54+
'coordinates' => [$lon, $lat],
55+
],
56+
'properties' => $properties,
57+
];
58+
59+
if (null !== $bounds = $location->getBounds()) {
60+
$array['bounds'] = $bounds->toArray();
61+
}
62+
63+
return $array;
64+
}
65+
}

src/Common/Dumper/AbstractDumper.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Dumper;
14+
15+
use Geocoder\Location;
16+
17+
abstract class AbstractDumper
18+
{
19+
/**
20+
* @param Location $address
21+
*
22+
* @return string
23+
*/
24+
protected function formatName(Location $address): string
25+
{
26+
$name = [];
27+
$array = $address->toArray();
28+
29+
foreach (['streetNumber', 'streetName', 'postalCode', 'locality'] as $attr) {
30+
$name[] = $array[$attr];
31+
}
32+
33+
if (isset($array['adminLevels'][2])) {
34+
$name[] = $array['adminLevels'][2]['name'];
35+
}
36+
37+
if (isset($array['adminLevels'][1])) {
38+
$name[] = $array['adminLevels'][1]['name'];
39+
}
40+
41+
$name[] = $array['country'];
42+
43+
return implode(', ', array_filter($name));
44+
}
45+
}

src/Common/Dumper/GeoArray.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,13 @@
1717
/**
1818
* @author Tomas Norkūnas <[email protected]>
1919
*/
20-
class GeoArray implements Dumper
20+
final class GeoArray extends AbstractArrayDumper implements Dumper
2121
{
2222
/**
2323
* {@inheritdoc}
2424
*/
2525
public function dump(Location $location): array
2626
{
27-
$properties = array_filter($location->toArray(), function ($value) {
28-
return !empty($value);
29-
});
30-
31-
unset(
32-
$properties['latitude'],
33-
$properties['longitude'],
34-
$properties['bounds']
35-
);
36-
37-
if (0 === count($properties)) {
38-
$properties = null;
39-
}
40-
41-
$lat = 0;
42-
$lon = 0;
43-
if (null !== $coordinates = $location->getCoordinates()) {
44-
$lat = $coordinates->getLatitude();
45-
$lon = $coordinates->getLongitude();
46-
}
47-
48-
$array = [
49-
'type' => 'Feature',
50-
'geometry' => [
51-
'type' => 'Point',
52-
'coordinates' => [$lon, $lat],
53-
],
54-
'properties' => $properties,
55-
];
56-
57-
if (null !== $bounds = $location->getBounds()) {
58-
$array['bounds'] = $bounds->toArray();
59-
}
60-
61-
return $array;
27+
return $this->getArray($location);
6228
}
6329
}

src/Common/Dumper/GeoJson.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,13 @@
1717
/**
1818
* @author Jan Sorgalla <[email protected]>
1919
*/
20-
class GeoJson implements Dumper
20+
final class GeoJson extends AbstractArrayDumper
2121
{
2222
/**
2323
* {@inheritdoc}
2424
*/
2525
public function dump(Location $location): string
2626
{
27-
$properties = array_filter($location->toArray(), function ($value) {
28-
return !empty($value);
29-
});
30-
31-
unset(
32-
$properties['latitude'],
33-
$properties['longitude'],
34-
$properties['bounds']
35-
);
36-
37-
if (0 === count($properties)) {
38-
$properties = null;
39-
}
40-
41-
$lat = 0;
42-
$lon = 0;
43-
if (null !== $coordinates = $location->getCoordinates()) {
44-
$lat = $coordinates->getLatitude();
45-
$lon = $coordinates->getLongitude();
46-
}
47-
48-
$json = [
49-
'type' => 'Feature',
50-
'geometry' => [
51-
'type' => 'Point',
52-
'coordinates' => [$lon, $lat],
53-
],
54-
'properties' => $properties,
55-
];
56-
57-
if (null !== $bounds = $location->getBounds()) {
58-
$json['bounds'] = $bounds->toArray();
59-
}
60-
61-
return json_encode($json);
27+
return json_encode($this->getArray($location));
6228
}
6329
}

src/Common/Dumper/Gpx.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author William Durand <[email protected]>
2020
*/
21-
class Gpx implements Dumper
21+
final class Gpx extends AbstractDumper implements Dumper
2222
{
2323
/**
2424
* @param Location $location
@@ -69,31 +69,4 @@ public function dump(Location $location): string
6969

7070
return $gpx;
7171
}
72-
73-
/**
74-
* @param Location $address
75-
*
76-
* @return string
77-
*/
78-
protected function formatName(Location $address): string
79-
{
80-
$name = [];
81-
$array = $address->toArray();
82-
83-
foreach (['streetNumber', 'streetName', 'postalCode', 'locality'] as $attr) {
84-
$name[] = $array[$attr];
85-
}
86-
87-
if (isset($array['adminLevels'][2])) {
88-
$name[] = $array['adminLevels'][2]['name'];
89-
}
90-
91-
if (isset($array['adminLevels'][1])) {
92-
$name[] = $array['adminLevels'][1]['name'];
93-
}
94-
95-
$name[] = $array['country'];
96-
97-
return implode(', ', array_filter($name));
98-
}
9972
}

src/Common/Dumper/Kml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author Jan Sorgalla <[email protected]>
1919
*/
20-
class Kml extends Gpx implements Dumper
20+
final class Kml extends AbstractDumper implements Dumper
2121
{
2222
/**
2323
* {@inheritdoc}

src/Common/Dumper/Wkb.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author Jan Sorgalla <[email protected]>
1919
*/
20-
class Wkb implements Dumper
20+
final class Wkb implements Dumper
2121
{
2222
/**
2323
* {@inheritdoc}

src/Common/Dumper/Wkt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author Jan Sorgalla <[email protected]>
1919
*/
20-
class Wkt implements Dumper
20+
final class Wkt implements Dumper
2121
{
2222
/**
2323
* {@inheritdoc}

0 commit comments

Comments
 (0)