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.

Resolves #222

Bug: T379481
  • Loading branch information
codders committed Dec 6, 2024
1 parent cce979e commit 551cc60
Show file tree
Hide file tree
Showing 21 changed files with 76 additions and 46 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.4.1",
"mediawiki/mediawiki-codesniffer": "^34 || ^35 || ^36 || ^38",
"mediawiki/mediawiki-codesniffer": "45.0.0",
"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
5 changes: 2 additions & 3 deletions src/Formatters/LatLongFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ class LatLongFormatter implements ValueFormatter {

private const DEFAULT_PRECISION = 1 / 3600;

/** @var FormatterOptions */
private $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 +212,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 +221,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
1 change: 1 addition & 0 deletions src/PackagePrivate/DmPrecisionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class DmPrecisionDetector extends PrecisionDetector {

/** @var DmsPrecisionDetector the precision detector */
private $dmsPrecisionDetector;

public function __construct() {
Expand Down
5 changes: 4 additions & 1 deletion src/PackagePrivate/LatLongPrecisionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

class LatLongPrecisionParser {

/** @var ?ParserOptions the parser options */
private $options;

/** @var ?array array of parsers in use */
private $parsers;

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

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

class PreciseLatLong {

/** @var LatLongValue the latitude/longitude value */
private $latLong;
/** @var Precision the precision */
private $precision;

public function __construct( LatLongValue $latLong, Precision $precision ) {
Expand Down
1 change: 1 addition & 0 deletions src/PackagePrivate/Precision.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Precision {

/** @var float the precision in degrees */
private $precision;

public function __construct( float $precisionInDegrees ) {
Expand Down
3 changes: 3 additions & 0 deletions src/PackagePrivate/PrecisionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

class PrecisionParser {

/** @var ValueParser Parser for latitude/longitude */
private $latLongParser;

/** @var PrecisionDetector */
private $precisionDetector;

public function __construct( ValueParser $latLongParser, PrecisionDetector $precisionDetector ) {
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
8 changes: 7 additions & 1 deletion src/Parsers/GlobeCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ class GlobeCoordinateParser implements ValueParser {
*/
public const OPT_GLOBE = 'globe';

/** @var ParserOptions the parser options */
private $options;

/** @var ?LatLongPrecisionParser the latitude/longitude parser */
private $latLongPrecisionParser;

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 +74,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
13 changes: 8 additions & 5 deletions src/Values/GlobeCoordinateValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
*/
class GlobeCoordinateValue implements DataValue {

/**
* @var LatLongValue
*/
private $latLong;

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

Expand All @@ -40,12 +43,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 +146,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
5 changes: 4 additions & 1 deletion src/Values/LatLongValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
*/
class LatLongValue implements DataValue {

/** @var float */
private $latitude;

/** @var float */
private $longitude;

/**
Expand Down Expand Up @@ -82,7 +85,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
1 change: 0 additions & 1 deletion tests/unit/Values/GlobeCoordinateValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ public function nonNumericStringProvider() {
yield [ [ 'latitude' => '1.23a', 'longitude' => '3.45b' ] ];
}


/**
* @dataProvider withPrecisionProvider
*/
Expand Down

0 comments on commit 551cc60

Please sign in to comment.