Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using deprecated defaultOption method #228

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4'
- '8.0'
- '8.1'
- '8.2'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ employees for the [Wikidata project](https://wikidata.org/).

### 4.5.0 (2024-12-10)

* Dropped support for PHP 7.3
* Dropped support for PHP 7.3 and 7.4
* Improved support for PHP 8.4 by explicitly declaring nullable types
* Upgraded `mediawiki/mediawiki-codesniffer` rules to version 45.0.0
* Added testing with PHP 8.2 and 8.3
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"docs": "https://github.com/DataValues/Geo/#usage"
},
"require": {
"php": ">=7.4",
"php": ">=8.0",
"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"
Expand Down
12 changes: 5 additions & 7 deletions src/Formatters/LatLongFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function __construct( ?FormatterOptions $options = null ) {
$this->defaultOption( self::OPT_PRECISION, 0 );

// Not used in this component, only here for downstream compatibility reasons
$this->options->defaultOption( ValueFormatter::OPT_LANG, 'en' );
$this->defaultOption( ValueFormatter::OPT_LANG, 'en' );
}

/**
Expand Down Expand Up @@ -368,12 +368,10 @@ private function formatNumber( float $number, int $digits = 0 ): string {
return sprintf( '%.' . ( $digits > 0 ? $digits : 0 ) . 'F', $number );
}

/**
* @param string $option
* @param mixed $default
*/
private function defaultOption( $option, $default ) {
$this->options->defaultOption( $option, $default );
private function defaultOption( string $option, mixed $default ): void {
if ( !$this->options->hasOption( $option ) ) {
$this->options->setOption( $option, $default );
}
}

}
7 changes: 3 additions & 4 deletions src/Parsers/DdCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ class DdCoordinateParser extends LatLongParserBase {
* @param ParserOptions|null $options
*/
public function __construct( ?ParserOptions $options = null ) {
$options = $options ?: new ParserOptions();
$options->defaultOption( self::OPT_DEGREE_SYMBOL, '°' );

parent::__construct( $options );
parent::__construct(
( $options ?: new ParserOptions() )->withDefaultOption( self::OPT_DEGREE_SYMBOL, '°' )
);

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

parent::__construct( $options );
parent::__construct(
( $options ?: new ParserOptions() )->withDefaultOption( self::OPT_MINUTE_SYMBOL, "'" )
);

$this->defaultDelimiters = [ $this->getOption( self::OPT_MINUTE_SYMBOL ) ];
}
Expand Down
7 changes: 3 additions & 4 deletions src/Parsers/DmsCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ class DmsCoordinateParser extends DmCoordinateParser {
* @param ?ParserOptions $options
*/
public function __construct( ?ParserOptions $options = null ) {
$options = $options ?: new ParserOptions();
$options->defaultOption( self::OPT_SECOND_SYMBOL, '"' );

parent::__construct( $options );
parent::__construct(
( $options ?: new ParserOptions() )->withDefaultOption( self::OPT_SECOND_SYMBOL, '"' )
);

$this->defaultDelimiters = [ $this->getOption( self::OPT_SECOND_SYMBOL ) ];
}
Expand Down
5 changes: 2 additions & 3 deletions src/Parsers/GlobeCoordinateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class GlobeCoordinateParser implements ValueParser {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

$this->options = ($options ?: new ParserOptions())
   ->withDefaultOption(...)
   ->withDefaultOption(...);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also works, but I couldn't be bothered in this case


$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
$this->options->defaultOption( self::OPT_GLOBE, 'http://www.wikidata.org/entity/Q2' );
$this->options = $this->options->withDefaultOption( ValueParser::OPT_LANG, 'en' );
$this->options = $this->options->withDefaultOption( self::OPT_GLOBE, 'http://www.wikidata.org/entity/Q2' );
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Parsers/LatLongParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ class LatLongParser implements ValueParser {
private $options;

public function __construct( ?ParserOptions $options = null ) {
$this->options = $options ?: new ParserOptions();
$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
$this->options = ( $options ?: new ParserOptions() )->withDefaultOption( ValueParser::OPT_LANG, 'en' );
}

/**
Expand Down
23 changes: 13 additions & 10 deletions src/Parsers/LatLongParserBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ abstract class LatLongParserBase implements ValueParser {
*/
public const OPT_SEPARATOR_SYMBOL = 'separator';

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

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

$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
$this->defaultOption( ValueParser::OPT_LANG, 'en' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for preferring the private defaultOption method here to repeated calls to withDefaultOption? Is it a wish to avoid repeated re-allocation of the options object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the quickest fix


$this->options->defaultOption( self::OPT_NORTH_SYMBOL, 'N' );
$this->options->defaultOption( self::OPT_EAST_SYMBOL, 'E' );
$this->options->defaultOption( self::OPT_SOUTH_SYMBOL, 'S' );
$this->options->defaultOption( self::OPT_WEST_SYMBOL, 'W' );
$this->defaultOption( self::OPT_NORTH_SYMBOL, 'N' );
$this->defaultOption( self::OPT_EAST_SYMBOL, 'E' );
$this->defaultOption( self::OPT_SOUTH_SYMBOL, 'S' );
$this->defaultOption( self::OPT_WEST_SYMBOL, 'W' );

$this->options->defaultOption( self::OPT_SEPARATOR_SYMBOL, ',' );
$this->defaultOption( self::OPT_SEPARATOR_SYMBOL, ',' );
}

private function defaultOption( string $option, mixed $default ): void {
if ( !$this->options->hasOption( $option ) ) {
$this->options->setOption( $option, $default );
}
}

/**
Expand Down
Loading