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. It should be upgraded to the latest version.

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.

Bug: T379481
  • Loading branch information
codders committed Dec 9, 2024
1 parent a2bc08e commit 8552423
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 51 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ language: php
dist: bionic

php:
- 7.2
- 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 @@ -21,14 +21,14 @@
"irc": "irc://irc.freenode.net/wikidata"
},
"require": {
"php": ">=7.2.0",
"php": ">=7.4.0",
"data-values/data-values": "~3.0|~2.0|~1.0|~0.1",
"data-values/interfaces": "~1.0|~0.2.0",
"data-values/common": "~1.0|~0.4.0|~0.3.0"
},
"require-dev": {
"phpunit/phpunit": "~8.5",
"mediawiki/mediawiki-codesniffer": "^39"
"mediawiki/mediawiki-codesniffer": "^45"
},
"autoload": {
"psr-0": {
Expand All @@ -54,5 +54,10 @@
"phpcs -p -s",
"phpunit"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
4 changes: 2 additions & 2 deletions src/DataValues/TimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private function normalizeIsoTimestamp( $timestamp ) {
throw new IllegalValueException( '$timestamp must resemble ISO 8601, given ' . $timestamp );
}

list( , $sign, $year, $month, $day, $hour, $minute, $second ) = $matches;
[ , $sign, $year, $month, $day, $hour, $minute, $second ] = $matches;

if ( $month > 12 ) {
throw new IllegalValueException( 'Month out of allowed bounds' );
Expand Down Expand Up @@ -373,7 +373,7 @@ public function unserialize( $value ) {
}

public function __unserialize( array $data ): void {
list( $timestamp, $timezone, $before, $after, $precision, $calendarModel ) = $data;
[ $timestamp, $timezone, $before, $after, $precision, $calendarModel ] = $data;
$this->__construct( $timestamp, $timezone, $before, $after, $precision, $calendarModel );
}

Expand Down
6 changes: 3 additions & 3 deletions src/DataValues/TimeValueCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) {
) {
throw new InvalidArgumentException( "Failed to parse time value $time." );
}
list( , $fullYear, $month, $day, $hour, $minute, $second ) = $matches;
[ , $fullYear, $month, $day, $hour, $minute, $second ] = $matches;

// We use mktime only for the month, day and time calculation. Set the year to the smallest
// possible in the 1970-2038 range to be safe, even if it's 1901-2038 since PHP 5.1.0.
Expand Down Expand Up @@ -78,10 +78,10 @@ private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) {
*/
public function isLeapYear( $year ) {
$year = $year < 0 ? ceil( $year ) + 1 : floor( $year );
$isMultipleOf4 = fmod( $year, 4 ) === 0.0;
$isMultipleOf4 = fmod( $year, 4 ) === 0.0;
$isMultipleOf100 = fmod( $year, 100 ) === 0.0;
$isMultipleOf400 = fmod( $year, 400 ) === 0.0;
return $isMultipleOf4 && !$isMultipleOf100 || $isMultipleOf400;
return ( $isMultipleOf4 && !$isMultipleOf100 ) || $isMultipleOf400;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ValueFormatters/TimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TimeFormatter implements ValueFormatter {
*
* @param FormatterOptions|null $options
*/
public function __construct( FormatterOptions $options = null ) {
public function __construct( ?FormatterOptions $options = null ) {
$this->options = $options ?: new FormatterOptions();

$this->options->defaultOption( self::OPT_CALENDARNAMES, array() );
Expand Down
2 changes: 1 addition & 1 deletion src/ValueParsers/CalendarModelParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CalendarModelParser extends StringValueParser {
/**
* @param ParserOptions|null $options
*/
public function __construct( ParserOptions $options = null ) {
public function __construct( ?ParserOptions $options = null ) {
parent::__construct( $options );

$this->defaultOption( self::OPT_CALENDAR_MODEL_URIS, array() );
Expand Down
8 changes: 4 additions & 4 deletions src/ValueParsers/IsoTimestampParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class IsoTimestampParser extends StringValueParser {
* @param ParserOptions|null $options
*/
public function __construct(
CalendarModelParser $calendarModelParser = null,
ParserOptions $options = null
?CalendarModelParser $calendarModelParser = null,
?ParserOptions $options = null
) {
parent::__construct( $options );

Expand Down Expand Up @@ -114,7 +114,7 @@ private function splitTimeString( $value ) {
throw new ParseException( 'Malformed time' );
}

list( , $sign, $year, $month, $day, $hour, $minute, $second, $calendarModel ) = $matches;
[ , $sign, $year, $month, $day, $hour, $minute, $second, $calendarModel ] = $matches;

if ( $sign === ''
&& strlen( $year ) < 3
Expand Down Expand Up @@ -228,7 +228,7 @@ private function getPrecisionFromYear( $unsignedYear ) {
* @return string URI
*/
private function getCalendarModel( array $timeParts ) {
list( $sign, $unsignedYear, , , , , , $calendarModel ) = $timeParts;
[ $sign, $unsignedYear, , , , , , $calendarModel ] = $timeParts;

if ( !empty( $calendarModel ) ) {
return $this->calendarModelParser->parse( $calendarModel );
Expand Down
14 changes: 7 additions & 7 deletions src/ValueParsers/PhpDateTimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function stringParse( $value ) {
$rawValue = $value;

try {
list( $sign, $value ) = $this->eraParser->parse( $value );
[ $sign, $value ] = $this->eraParser->parse( $value );

$value = trim( $value );
$value = $this->monthNameUnlocalizer->unlocalize( $value );
Expand Down Expand Up @@ -167,19 +167,19 @@ private function fetchAndNormalizeYear( &$value ) {
// documentation of the extraction heuristics up to date!
$patterns = array(
// Check if the string contains a number longer than 2 digits or bigger than 59.
'/(?<!\d)(' // cannot be prepended by a digit
. '\d{3,}|' // any number longer than 2 digits, or
. '[6-9]\d' // any number bigger than 59
. ')(?!\d)/', // cannot be followed by a digit
'/(?<!\d)(' // cannot be prepended by a digit
. '\d{3,}|' // any number longer than 2 digits, or
. '[6-9]\d' // any number bigger than 59
. ')(?!\d)/', // cannot be followed by a digit

// Check if the first number in the string is bigger than 31.
'/^\D*(3[2-9]|[4-9]\d)/',

// Check if the string starts with three space-separated parts or three numbers.
'/^(?:'
. '\S+\s+\S+\s+|' // e.g. "July<SPACE>4th<SPACE>", or
. '\d+\D+\d+\D+' // e.g. "4.7."
. ')(\d+)/', // followed by a number
. '\d+\D+\d+\D+' // e.g. "4.7."
. ')(\d+)/', // followed by a number

// Check if the string ends with a number.
'/(\d+)\D*$/',
Expand Down
14 changes: 7 additions & 7 deletions src/ValueParsers/YearMonthDayTimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class YearMonthDayTimeParser extends StringValueParser {
* returns an array with the detected sign character and the remaining value.
* @param ParserOptions|null $options
*/
public function __construct( ValueParser $eraParser = null, ParserOptions $options = null ) {
public function __construct( ?ValueParser $eraParser = null, ?ParserOptions $options = null ) {
parent::__construct( $options );

$this->eraParser = $eraParser ?: new EraParser();
Expand All @@ -47,8 +47,8 @@ public function __construct( ValueParser $eraParser = null, ParserOptions $optio
*/
protected function stringParse( $value ) {
try {
list( $sign, $preparsedValue ) = $this->eraParser->parse( $value );
list( $signedYear, $month, $day ) = $this->parseYearMonthDay( $preparsedValue );
[ $sign, $preparsedValue ] = $this->eraParser->parse( $value );
[ $signedYear, $month, $day ] = $this->parseYearMonthDay( $preparsedValue );

if ( substr( $signedYear, 0, 1 ) !== '-' ) {
$signedYear = $sign . $signedYear;
Expand Down Expand Up @@ -76,9 +76,9 @@ private function parseYearMonthDay( $value ) {
// A 32 in the first spot cannot be confused with anything.
if ( $matches[1] < 1 || $matches[1] > 31 ) {
if ( $matches[3] > 12 || $matches[2] == $matches[3] ) {
list( , $signedYear, $month, $day ) = $matches;
[ , $signedYear, $month, $day ] = $matches;
} elseif ( $matches[2] > 12 ) {
list( , $signedYear, $day, $month ) = $matches;
[ , $signedYear, $day, $month ] = $matches;
} else {
throw new ParseException( 'Cannot distinguish YDM and YMD' );
}
Expand All @@ -88,9 +88,9 @@ private function parseYearMonthDay( $value ) {
|| ( abs( $matches[1] ) > 24 && $matches[3] > 31 )
) {
if ( $matches[1] > 12 || $matches[1] == $matches[2] ) {
list( , $day, $month, $signedYear ) = $matches;
[ , $day, $month, $signedYear ] = $matches;
} elseif ( $matches[2] > 12 ) {
list( , $month, $day, $signedYear ) = $matches;
[ , $month, $day, $signedYear ] = $matches;
} else {
throw new ParseException( 'Cannot distinguish DMY and MDY' );
}
Expand Down
10 changes: 5 additions & 5 deletions src/ValueParsers/YearMonthTimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class YearMonthTimeParser extends StringValueParser {
*/
public function __construct(
MonthNameProvider $monthNameProvider,
ParserOptions $options = null,
EraParser $eraParser = null
?ParserOptions $options = null,
?EraParser $eraParser = null
) {
parent::__construct( $options );

Expand All @@ -64,8 +64,8 @@ public function __construct(
* @return TimeValue
*/
protected function stringParse( $value ) {
list( $newValue, $sign ) = $this->splitBySignAndEra( $value );
list( $a, $b ) = $this->splitByYearMonth( $value, $newValue );
[ $newValue, $sign ] = $this->splitBySignAndEra( $value );
[ $a, $b ] = $this->splitByYearMonth( $value, $newValue );

// non-empty sign indicates the era (e.g. "BCE") was specified
// don't accept a negative number as the year
Expand Down Expand Up @@ -131,7 +131,7 @@ private function splitBySignAndEra( $value ) {
$newValue = $trimmedValue;
$sign = '';
} else {
list( $sign, $newValue ) = $this->eraParser->parse( $trimmedValue );
[ $sign, $newValue ] = $this->eraParser->parse( $trimmedValue );
if ( $newValue === $trimmedValue ) {
// EraParser defaults to "+" but we need to indicate "unspecified era"
$sign = '';
Expand Down
4 changes: 2 additions & 2 deletions src/ValueParsers/YearTimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class YearTimeParser extends StringValueParser {
* @param ValueParser|null $eraParser
* @param ParserOptions|null $options
*/
public function __construct( ValueParser $eraParser = null, ParserOptions $options = null ) {
public function __construct( ?ValueParser $eraParser = null, ?ParserOptions $options = null ) {
parent::__construct( $options );

$this->defaultOption(
Expand All @@ -68,7 +68,7 @@ public function __construct( ValueParser $eraParser = null, ParserOptions $optio
* @return TimeValue
*/
protected function stringParse( $value ) {
list( $sign, $year ) = $this->eraParser->parse( $value );
[ $sign, $year ] = $this->eraParser->parse( $value );
$year = trim( $year );

// Negative dates usually don't have a month, assume non-digits are thousands separators
Expand Down
8 changes: 4 additions & 4 deletions tests/DataValues/TimeValueCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ private function getTimeValueMock( $time, $timezone = 0 ) {

$timeValue->expects( $this->any() )
->method( 'getTime' )
->will( $this->returnValue( $time ) );
->willReturn( $time );
$timeValue->expects( $this->any() )
->method( 'getTimezone' )
->will( $this->returnValue( $timezone ) );
->willReturn( $timezone );
$timeValue->expects( $this->any() )
->method( 'getPrecision' )
->will( $this->returnValue( TimeValue::PRECISION_DAY ) );
->willReturn( TimeValue::PRECISION_DAY );
$timeValue->expects( $this->any() )
->method( 'getCalendarModel' )
->will( $this->returnValue( 'Stardate' ) );
->willReturn( 'Stardate' );

return $timeValue;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ValueFormatters/TimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TimeFormatterTest extends TestCase {
*
* @return TimeFormatter
*/
protected function getInstance( FormatterOptions $options = null ) {
protected function getInstance( ?FormatterOptions $options = null ) {
return new TimeFormatter( $options );
}

Expand Down Expand Up @@ -109,8 +109,8 @@ public function validProvider() {
public function testValidFormat(
$value,
$expected,
FormatterOptions $options = null,
ValueFormatter $formatter = null
?FormatterOptions $options = null,
?ValueFormatter $formatter = null
) {
if ( $formatter === null ) {
$formatter = $this->getInstance( $options );
Expand Down
4 changes: 2 additions & 2 deletions tests/ValueParsers/PhpDateTimeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function getEraParser() {
$mock->expects( $this->any() )
->method( 'parse' )
->with( $this->isType( 'string' ) )
->will( $this->returnCallback(
->willReturnCallback(
static function ( $value ) {
$sign = '+';
// Tiny parser that supports a single negative sign only
Expand All @@ -57,7 +57,7 @@ static function ( $value ) {
}
return array( $sign, $value );
}
) );
);

return $mock;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ValueParsers/ValueParserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract protected function getInstance();
* @param mixed $expected
* @param ValueParser|null $parser
*/
public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) {
public function testParseWithValidInputs( $value, $expected, ?ValueParser $parser = null ) {
if ( $parser === null ) {
$parser = $this->getInstance();
}
Expand Down Expand Up @@ -94,7 +94,7 @@ private function assertSmartEquals( $expected, $actual ) {
* @param mixed $value
* @param ValueParser|null $parser
*/
public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) {
public function testParseWithInvalidInputs( $value, ?ValueParser $parser = null ) {
if ( $parser === null ) {
$parser = $this->getInstance();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ValueParsers/YearMonthTimeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ protected function getInstance() {
$monthNameProvider->expects( $this->once() )
->method( 'getMonthNumbers' )
->with( 'en' )
->will( $this->returnValue( array(
->willReturn( array(
'January' => 1,
'Jan' => 1,
// to test Unicode (it's Czech)
'Březen' => 3,
'April' => 4,
'June' => 6,
) ) );
) );

return new YearMonthTimeParser( $monthNameProvider );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ValueParsers/YearTimeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function getMockEraParser() {
$mock->expects( $this->any() )
->method( 'parse' )
->with( $this->isType( 'string' ) )
->will( $this->returnCallback(
->willReturnCallback(
static function ( $value ) {
$sign = '+';
// Tiny parser that supports a single negative sign only
Expand All @@ -51,7 +51,7 @@ static function ( $value ) {
}
return array( $sign, $value );
}
) );
);
return $mock;
}

Expand Down

0 comments on commit 8552423

Please sign in to comment.