Skip to content

Commit

Permalink
Upgrade mediawiki-codesniffer to latest version (45.0.0)
Browse files Browse the repository at this point in the history
This repository is currently using version 34 of the Mediawiki coding
style phpcs rules. If the code aims to comply with Mediawiki style
guidelines, it makes sense that it continues to comply at current
versions of the rules.

Besides being simply different, the newer versions of the rules
introduce, for example,
MediaWiki.Usage.NullableType.ExplicitNullableTypes, which enforces
compliance with coming deprecation rules in PHP 8.4 concerning nullable
types. If the code does not remove the soon-to-be-deprecated form of
nullable type declarations, the library will no longer be usable in
Mediawiki for PHP 8.4 deployments.

Note that mediawiki-codesniffer 45.0.0 requires PHP version 7.4 or
later - PHP 7.3 support will be dropped with this change. PHP 7.3
has been out of support since 6. Dec 2021.

Resolves #222

Bug: T379481
  • Loading branch information
codders committed Dec 6, 2024
1 parent cce979e commit f84a4db
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 69 deletions.
1 change: 0 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
fail-fast: false
matrix:
php:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
"docs": "https://github.com/DataValues/Geo/#usage"
},
"require": {
"php": ">=7.3",
"php": ">=7.4",
"data-values/data-values": "^3.0|^2.0|^1.0|~0.1",
"data-values/interfaces": "^1.0.0|^0.2.0",
"symfony/polyfill-php80": "^1.18.1"
},
"require-dev": {
"phpunit/phpunit": "^9.4.1",
"mediawiki/mediawiki-codesniffer": "^34 || ^35 || ^36 || ^38",
"mediawiki/mediawiki-codesniffer": "^45",
"ockcyp/covers-validator": "^1.3.3",
"phpstan/phpstan": "^0.12.68 || ^1.0.0",
"phpmd/phpmd": "^2.9.1",
Expand Down Expand Up @@ -83,5 +83,10 @@
"@test",
"@cs"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<rule ref="Generic.Metrics.CyclomaticComplexity" />
<rule ref="Generic.Metrics.NestingLevel" />
<rule ref="Squiz.Operators.ValidLogicalOperators" />
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/>
</ruleset>
2 changes: 1 addition & 1 deletion src/Formatters/GlobeCoordinateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GlobeCoordinateFormatter implements ValueFormatter {
*/
private $formatter;

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

Expand Down
6 changes: 2 additions & 4 deletions src/Formatters/LatLongFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class LatLongFormatter implements ValueFormatter {

private const DEFAULT_PRECISION = 1 / 3600;

private $options;
private FormatterOptions $options;

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

$this->defaultOption( self::OPT_NORTH_SYMBOL, 'N' );
Expand Down Expand Up @@ -211,7 +211,6 @@ private function formatLongitude( float $longitude, float $precision ): string {

private function makeDirectionalIfNeeded( string $coordinate, string $positiveSymbol,
string $negativeSymbol ): string {

if ( $this->options->getOption( self::OPT_DIRECTIONAL ) ) {
return $this->makeDirectional( $coordinate, $positiveSymbol, $negativeSymbol );
}
Expand All @@ -221,7 +220,6 @@ private function makeDirectionalIfNeeded( string $coordinate, string $positiveSy

private function makeDirectional( string $coordinate, string $positiveSymbol,
string $negativeSymbol ): string {

$isNegative = substr( $coordinate, 0, 1 ) === '-';

if ( $isNegative ) {
Expand Down
2 changes: 1 addition & 1 deletion src/GlobeMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function normalizeGlobeCoordinate( GlobeCoordinateValue $value ): GlobeCo
*
* @return LatLongValue
*/
public function normalizeGlobeLatLong( LatLongValue $value, string $globe = null ): LatLongValue {
public function normalizeGlobeLatLong( LatLongValue $value, ?string $globe = null ): LatLongValue {
switch ( $this->normalizeGlobe( $globe ) ) {
case GlobeCoordinateValue::GLOBE_EARTH:
case self::GLOBE_MOON:
Expand Down
2 changes: 1 addition & 1 deletion src/PackagePrivate/DmPrecisionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class DmPrecisionDetector extends PrecisionDetector {

private $dmsPrecisionDetector;
private DmsPrecisionDetector $dmsPrecisionDetector;

public function __construct() {
$this->dmsPrecisionDetector = new DmsPrecisionDetector();
Expand Down
6 changes: 3 additions & 3 deletions src/PackagePrivate/LatLongPrecisionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

class LatLongPrecisionParser {

private $options;
private $parsers;
private ?ParserOptions $options;
private ?array $parsers = null;

public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$this->options = $options;
}

Expand Down
4 changes: 2 additions & 2 deletions src/PackagePrivate/PreciseLatLong.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

class PreciseLatLong {

private $latLong;
private $precision;
private LatLongValue $latLong;
private Precision $precision;

public function __construct( LatLongValue $latLong, Precision $precision ) {
$this->latLong = $latLong;
Expand Down
2 changes: 1 addition & 1 deletion src/PackagePrivate/Precision.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Precision {

private $precision;
private float $precision;

public function __construct( float $precisionInDegrees ) {
if ( $precisionInDegrees < -360 || $precisionInDegrees > 360 ) {
Expand Down
4 changes: 2 additions & 2 deletions src/PackagePrivate/PrecisionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class PrecisionParser {

private $latLongParser;
private $precisionDetector;
private ValueParser $latLongParser;
private PrecisionDetector $precisionDetector;

public function __construct( ValueParser $latLongParser, PrecisionDetector $precisionDetector ) {
$this->latLongParser = $latLongParser;
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/DdCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DdCoordinateParser extends LatLongParserBase {
/**
* @param ParserOptions|null $options
*/
public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$options = $options ?: new ParserOptions();
$options->defaultOption( self::OPT_DEGREE_SYMBOL, '°' );

Expand Down Expand Up @@ -107,7 +107,7 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
}
}

return ( 1 === $match );
return ( $match === 1 );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/DmCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DmCoordinateParser extends DdCoordinateParser {
/**
* @param ParserOptions|null $options
*/
public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$options = $options ?: new ParserOptions();
$options->defaultOption( self::OPT_MINUTE_SYMBOL, "'" );

Expand Down Expand Up @@ -151,7 +151,7 @@ protected function parseCoordinate( string $coordinateSegment ): float {
);
}

list( $degrees, $minutes ) = $exploded;
[ $degrees, $minutes ] = $exploded;

$minutes = substr( $minutes, 0, -1 );

Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/DmsCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class DmsCoordinateParser extends DmCoordinateParser {
public const OPT_SECOND_SYMBOL = 'second';

/**
* @param ParserOptions|null $options
* @param ?ParserOptions $options
*/
public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$options = $options ?: new ParserOptions();
$options->defaultOption( self::OPT_SECOND_SYMBOL, '"' );

Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/FloatCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function areValidCoordinates( array $normalizedCoordinateSegments ): b
}
}

return ( 1 === $match );
return ( $match === 1 );
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Parsers/GlobeCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class GlobeCoordinateParser implements ValueParser {
*/
public const OPT_GLOBE = 'globe';

private $options;
private $latLongPrecisionParser;
private ParserOptions $options;
private ?LatLongPrecisionParser $latLongPrecisionParser = null;

public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$this->options = $options ?: new ParserOptions();

$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
Expand Down Expand Up @@ -71,6 +71,9 @@ public function parse( $value ): GlobeCoordinateValue {
);
}

/**
* @return LatLongPrecisionParser
*/
private function getParser() {
if ( $this->latLongPrecisionParser === null ) {
$this->latLongPrecisionParser = new LatLongPrecisionParser( $this->options );
Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/LatLongParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class LatLongParser implements ValueParser {
*/
private $options;

public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$this->options = $options ?: new ParserOptions();
$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
}
Expand Down
8 changes: 6 additions & 2 deletions src/Parsers/LatLongParserBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class LatLongParserBase implements ValueParser {
*/
private $options;

public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
$this->options = $options ?: new ParserOptions();

$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
Expand Down Expand Up @@ -94,7 +94,7 @@ public function parse( $value ): LatLongValue {
throw new ParseException( 'Not a valid geographical coordinate', $rawValue, static::FORMAT_NAME );
}

list( $latitude, $longitude ) = $normalizedCoordinateSegments;
[ $latitude, $longitude ] = $normalizedCoordinateSegments;

return new LatLongValue(
$this->getParsedCoordinate( $latitude ),
Expand Down Expand Up @@ -183,6 +183,10 @@ protected function resolveDirection( string $coordinateSegment ): string {
return $coordinateSegment;
}

/**
* @param string $optionName
* @return mixed
*/
protected function getOption( string $optionName ) {
return $this->options->getOption( $optionName );
}
Expand Down
20 changes: 7 additions & 13 deletions src/Values/GlobeCoordinateValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@
*/
class GlobeCoordinateValue implements DataValue {

private $latLong;

/**
* @var float|null
*/
private $precision;
private LatLongValue $latLong;
private ?float $precision;

/**
* IRI of the globe on which the location resides.
*
* @var string
*/
private $globe;
private string $globe;

/**
* Wikidata concept URI for the Earth. Used as default value when no other globe was specified.
Expand All @@ -40,12 +34,12 @@ class GlobeCoordinateValue implements DataValue {

/**
* @param LatLongValue $latLong
* @param float|int|null $precision in degrees, e.g. 0.01.
* @param string|null $globe IRI, defaults to 'http://www.wikidata.org/entity/Q2'.
* @param ?float $precision in degrees, e.g. 0.01.
* @param ?string $globe IRI, defaults to 'http://www.wikidata.org/entity/Q2'.
*
* @throws IllegalValueException
*/
public function __construct( LatLongValue $latLong, float $precision = null, string $globe = null ) {
public function __construct( LatLongValue $latLong, ?float $precision = null, ?string $globe = null ) {
$this->assertIsPrecision( $precision );

if ( $globe === null ) {
Expand Down Expand Up @@ -143,7 +137,7 @@ public function __serialize(): array {
* @throws InvalidArgumentException
*/
public function unserialize( $value ) {
$this->__unserialize( json_decode( $value) );
$this->__unserialize( json_decode( $value ) );
}

public function __unserialize( array $data ): void {
Expand Down
6 changes: 3 additions & 3 deletions src/Values/LatLongValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
class LatLongValue implements DataValue {

private $latitude;
private $longitude;
private float $latitude;
private float $longitude;

/**
* @param float|int $latitude Latitude in degrees within the range [-360, 360]
Expand Down Expand Up @@ -82,7 +82,7 @@ public function getHash(): string {
public function getSerializationForHash(): string {
$data = $this->serialize();
return 'C:' . strlen( static::class ) . ':"' . static::class .
'":' . strlen( $data ) . ':{' . $data . '}';
'":' . strlen( $data ) . ':{' . $data . '}';
}

public function getCopy(): self {
Expand Down
42 changes: 21 additions & 21 deletions tests/unit/GlobeMathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public function latLongProvider() {
// west to east. For other globes see http://planetarynames.wr.usgs.gov/TargetCoordinates
return [
// Yes, there really are nine ways to describe the same point
[ 0, 0, 0, 0 ],
[ 0, 0, 0, 360 ],
[ 0, 0, 0, -360 ],
[ 0, 0, 360, 0 ],
[ 0, 0, -360, 0 ],
[ 0, 0, 180, 180 ],
[ 0, 0, 180, -180 ],
[ 0, 0, -180, 180 ],
[ 0, 0, 0, 0 ],
[ 0, 0, 0, 360 ],
[ 0, 0, 0, -360 ],
[ 0, 0, 360, 0 ],
[ 0, 0, -360, 0 ],
[ 0, 0, 180, 180 ],
[ 0, 0, 180, -180 ],
[ 0, 0, -180, 180 ],
[ 0, 0, -180, -180 ],

// Earth (default) vs. other globes
Expand All @@ -68,21 +68,21 @@ public function latLongProvider() {
[ 0, 350, 0, 350, 'Vulcan' ],

// Make sure the methods do not simply return true
[ 0, 0, 0, 180, null, false ],
[ 0, 0, 0, -180, null, false ],
[ 0, 0, 180, 0, null, false ],
[ 0, 0, 180, 360, null, false ],
[ 0, 0, 0, 180, null, false ],
[ 0, 0, 0, -180, null, false ],
[ 0, 0, 180, 0, null, false ],
[ 0, 0, 180, 360, null, false ],

// Dark side of the Moon, erm Earth
[ 0, -180, 0, 180 ],
[ 0, -180, 0, -180 ],
[ 0, -180, 180, 0 ],
[ 0, -180, -180, 0 ],
[ 0, -180, 0, 180 ],
[ 0, -180, 0, -180 ],
[ 0, -180, 180, 0 ],
[ 0, -180, -180, 0 ],
[ 0, -180, -360, -180 ],

// Half way to the north pole
[ 45, 0, 45, -360 ],
[ 45, 0, 135, 180 ],
[ 45, 0, 45, -360 ],
[ 45, 0, 135, 180 ],
[ 45, 0, 135, -180 ],

// North pole is a special case, drop longitude
Expand All @@ -91,9 +91,9 @@ public function latLongProvider() {
[ 90, 0, -270, 180 ],
[ 90, 0, -90, 0, null, false ],
// Same for south pole
[ -90, 0, -90, 123 ],
[ -90, 0, 270, 0 ],
[ -90, 0, 270, -180 ],
[ -90, 0, -90, 123 ],
[ -90, 0, 270, 0 ],
[ -90, 0, 270, -180 ],

// Make sure we cover all cases in the code
[ 10, 10, 10, 10 ],
Expand Down
Loading

0 comments on commit f84a4db

Please sign in to comment.