Skip to content

Commit a4be875

Browse files
committed
Various cleanup and PHP 8 syntax
1 parent 6928146 commit a4be875

22 files changed

+70
-129
lines changed

src/Formatters/GlobeCoordinateFormatter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
*/
2727
class GlobeCoordinateFormatter implements ValueFormatter {
2828

29-
/**
30-
* @var LatLongFormatter
31-
*/
32-
private $formatter;
29+
private LatLongFormatter $formatter;
3330

3431
public function __construct( ?FormatterOptions $options = null ) {
3532
$this->formatter = new LatLongFormatter( $options );

src/Formatters/LatLongFormatter.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private function getPrecisionFromOptions(): float {
161161
* @since 0.5
162162
*
163163
* @param LatLongValue $value
164-
* @param float|int $precision The desired precision, given as fractional degrees.
164+
* @param ?float $precision The desired precision, given as fractional degrees.
165165
*
166166
* @return string Plain text
167167
* @throws InvalidArgumentException
@@ -171,15 +171,13 @@ public function formatLatLongValue( LatLongValue $value, ?float $precision ): st
171171
$precision = self::DEFAULT_PRECISION;
172172
}
173173

174-
$formatted = implode(
174+
return implode(
175175
$this->options->getOption( self::OPT_SEPARATOR_SYMBOL ) . $this->getSpacing( self::OPT_SPACE_LATLONG ),
176176
[
177177
$this->formatLatitude( $value->getLatitude(), $precision ),
178178
$this->formatLongitude( $value->getLongitude(), $precision )
179179
]
180180
);
181-
182-
return $formatted;
183181
}
184182

185183
/**
@@ -188,10 +186,7 @@ public function formatLatLongValue( LatLongValue $value, ?float $precision ): st
188186
* @return string
189187
*/
190188
private function getSpacing( string $spacingLevel ): string {
191-
if ( in_array( $spacingLevel, $this->options->getOption( self::OPT_SPACING_LEVEL ) ) ) {
192-
return ' ';
193-
}
194-
return '';
189+
return in_array( $spacingLevel, $this->options->getOption( self::OPT_SPACING_LEVEL ) ) ? ' ' : '';
195190
}
196191

197192
private function formatLatitude( float $latitude, float $precision ): string {
@@ -347,9 +342,9 @@ private function getInDecimalMinuteFormat( float $floatDegrees, float $precision
347342
}
348343

349344
/**
350-
* @param float|int $unitsPerDegree The number of target units per degree
345+
* @param float $unitsPerDegree The number of target units per degree
351346
* (60 for minutes, 3600 for seconds)
352-
* @param float|int $degreePrecision
347+
* @param float $degreePrecision
353348
*
354349
* @return int The number of digits to show after the decimal point
355350
* (resp. before, if the result is negative).
@@ -366,7 +361,7 @@ private function getSignificantDigits( float $unitsPerDegree, float $degreePreci
366361
*/
367362
private function formatNumber( float $number, int $digits = 0 ): string {
368363
// TODO: use NumberLocalizer
369-
return sprintf( '%.' . ( $digits > 0 ? $digits : 0 ) . 'F', $number );
364+
return sprintf( '%.' . ( max( $digits, 0 ) ) . 'F', $number );
370365
}
371366

372367
private function defaultOption( string $option, mixed $default ): void {

src/GlobeMath.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,10 @@ public function normalizeGlobeCoordinate( GlobeCoordinateValue $value ): GlobeCo
6161
* @return LatLongValue
6262
*/
6363
public function normalizeGlobeLatLong( LatLongValue $value, ?string $globe = null ): LatLongValue {
64-
switch ( $this->normalizeGlobe( $globe ) ) {
65-
case GlobeCoordinateValue::GLOBE_EARTH:
66-
case self::GLOBE_MOON:
67-
$minimumLongitude = -180;
68-
break;
69-
default:
70-
$minimumLongitude = 0;
71-
}
64+
$minimumLongitude = match ( $this->normalizeGlobe( $globe ) ) {
65+
GlobeCoordinateValue::GLOBE_EARTH, self::GLOBE_MOON => -180,
66+
default => 0,
67+
};
7268

7369
return $this->normalizeLatLong( $value, $minimumLongitude );
7470
}

src/PackagePrivate/LatLongPrecisionParser.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@
1313
* @api
1414
*/
1515
class LatLongPrecisionParser {
16-
17-
private ?ParserOptions $options;
1816
private ?array $parsers = null;
1917

20-
public function __construct( ?ParserOptions $options = null ) {
21-
$this->options = $options;
18+
public function __construct( private ?ParserOptions $options = null ) {
2219
}
2320

2421
public function parse( string $coordinate ): PreciseLatLong {
2522
foreach ( $this->getParsers() as $parser ) {
2623
try {
2724
$latLongPrecision = $parser->parse( $coordinate );
28-
} catch ( ParseException $parseException ) {
25+
} catch ( ParseException ) {
2926
continue;
3027
}
3128

src/PackagePrivate/PreciseLatLong.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@
1010
* @api
1111
*/
1212
class PreciseLatLong {
13-
14-
private LatLongValue $latLong;
15-
private Precision $precision;
16-
17-
public function __construct( LatLongValue $latLong, Precision $precision ) {
18-
$this->latLong = $latLong;
19-
$this->precision = $precision;
13+
public function __construct( private readonly LatLongValue $latLong, private readonly Precision $precision ) {
2014
}
2115

2216
public function getLatLong(): LatLongValue {

src/PackagePrivate/Precision.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace DataValues\Geo\PackagePrivate;
66

7+
use InvalidArgumentException;
8+
79
/**
810
* @api
911
*/
@@ -13,7 +15,7 @@ class Precision {
1315

1416
public function __construct( float $precisionInDegrees ) {
1517
if ( $precisionInDegrees < -360 || $precisionInDegrees > 360 ) {
16-
throw new \InvalidArgumentException( '$precision needs to be between -360 and 360' );
18+
throw new InvalidArgumentException( '$precision needs to be between -360 and 360' );
1719
}
1820

1921
$this->precision = $precisionInDegrees;

src/PackagePrivate/PrecisionParser.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
* @api
99
*/
1010
class PrecisionParser {
11-
12-
private ValueParser $latLongParser;
13-
private PrecisionDetector $precisionDetector;
14-
15-
public function __construct( ValueParser $latLongParser, PrecisionDetector $precisionDetector ) {
16-
$this->latLongParser = $latLongParser;
17-
$this->precisionDetector = $precisionDetector;
11+
public function __construct( private readonly ValueParser $latLongParser, private readonly PrecisionDetector $precisionDetector ) {
1812
}
1913

2014
public function parse( string $coordinate ): PreciseLatLong {

src/Parsers/DdCoordinateParser.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public function __construct( ?ParserOptions $options = null ) {
4545

4646
/**
4747
* @see LatLongParserBase::getParsedCoordinate
48-
*
49-
* @param string $coordinateSegment
50-
*
51-
* @return float
5248
*/
5349
protected function getParsedCoordinate( string $coordinateSegment ): float {
5450
$coordinateSegment = $this->resolveDirection( $coordinateSegment );
@@ -73,14 +69,14 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
7369
$match = false;
7470

7571
foreach ( $normalizedCoordinateSegments as $i => $segment ) {
76-
$direction = '('
77-
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
78-
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
79-
8072
if ( $i === 1 ) {
8173
$direction = '('
8274
. $this->getOption( self::OPT_EAST_SYMBOL ) . '|'
8375
. $this->getOption( self::OPT_WEST_SYMBOL ) . ')';
76+
} else {
77+
$direction = '('
78+
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
79+
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
8480
}
8581

8682
$match = preg_match(
@@ -90,10 +86,12 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
9086

9187
if ( $directional ) {
9288
// Directionality is only set after parsing latitude: When the latitude is
93-
// is directional, the longitude needs to be as well. Therefore we break here since
89+
// directional, the longitude needs to be as well. Therefore, we break here since
9490
// checking for directionality is the only check needed for longitude.
9591
break;
96-
} elseif ( $match ) {
92+
}
93+
94+
if ( $match ) {
9795
// Latitude is directional, no need to check for non-directionality.
9896
$directional = true;
9997
continue;
@@ -139,9 +137,7 @@ protected function getNormalizedNotation( string $coordinates ): string {
139137
$this->getOption( self::OPT_DEGREE_SYMBOL ), $coordinates
140138
);
141139

142-
$coordinates = $this->removeInvalidChars( $coordinates );
143-
144-
return $coordinates;
140+
return $this->removeInvalidChars( $coordinates );
145141
}
146142

147143
/**
@@ -160,10 +156,6 @@ protected function removeInvalidChars( string $string ): string {
160156

161157
/**
162158
* Converts a coordinate segment to float representation.
163-
*
164-
* @param string $coordinateSegment
165-
*
166-
* @return float
167159
*/
168160
protected function parseCoordinate( string $coordinateSegment ): float {
169161
return (float)str_replace(
@@ -186,7 +178,7 @@ protected function splitString( string $normalizedCoordinateString ): array {
186178
$normalizedCoordinateSegments = explode( $separator, $normalizedCoordinateString );
187179

188180
if ( count( $normalizedCoordinateSegments ) !== 2 ) {
189-
// Separator not present within the string, trying to figure out the segments by
181+
// Separator is not present within the string, trying to figure out the segments by
190182
// splitting after the first direction character or degree symbol:
191183
$delimiters = $this->defaultDelimiters;
192184

src/Parsers/DmCoordinateParser.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,13 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
109109

110110
/**
111111
* @see DdCoordinateParser::getNormalizedNotation
112-
*
113-
* @param string $coordinates
114-
*
115-
* @return string
116112
*/
117113
protected function getNormalizedNotation( string $coordinates ): string {
118-
$minute = $this->getOption( self::OPT_MINUTE_SYMBOL );
119-
120-
$coordinates = str_replace( [ '&#8242;', '&prime;', '´', '' ], $minute, $coordinates );
114+
$coordinates = str_replace( [ '&#8242;', '&prime;', '´', '' ], $this->getOption( self::OPT_MINUTE_SYMBOL ), $coordinates );
121115

122-
$coordinates = parent::getNormalizedNotation( $coordinates );
123-
124-
$coordinates = $this->removeInvalidChars( $coordinates );
125-
126-
return $coordinates;
116+
return $this->removeInvalidChars(
117+
parent::getNormalizedNotation( $coordinates )
118+
);
127119
}
128120

129121
/**

src/Parsers/DmsCoordinateParser.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
6868
$directional = false;
6969

7070
foreach ( $normalizedCoordinateSegments as $i => $segment ) {
71-
$direction = '('
72-
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
73-
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
74-
7571
if ( $i === 1 ) {
7672
$direction = '('
7773
. $this->getOption( self::OPT_EAST_SYMBOL ) . '|'
7874
. $this->getOption( self::OPT_WEST_SYMBOL ) . ')';
75+
} else {
76+
$direction = '('
77+
. $this->getOption( self::OPT_NORTH_SYMBOL ) . '|'
78+
. $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')';
7979
}
8080

8181
$match = preg_match(
@@ -130,11 +130,7 @@ protected function getNormalizedNotation( string $coordinates ): string {
130130
);
131131
$coordinates = str_replace( [ '&acute;', '&#180;' ], $second, $coordinates );
132132

133-
$coordinates = parent::getNormalizedNotation( $coordinates );
134-
135-
$coordinates = $this->removeInvalidChars( $coordinates );
136-
137-
return $coordinates;
133+
return $this->removeInvalidChars( parent::getNormalizedNotation( $coordinates ) );
138134
}
139135

140136
/**

0 commit comments

Comments
 (0)