Skip to content

Commit

Permalink
Add withDefaultOption() to replace defaultOption()
Browse files Browse the repository at this point in the history
Passing the same mutable options instance around all over the place,
with various parts of the code adding different defaults to it, is a
recipe for confusion and bugs. Discourage it by introducing a new method
that has cloning built into its interface.
  • Loading branch information
lucaswerkmeister committed Dec 6, 2022
1 parent 3c03ea8 commit 3cb96d5
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ via Github Actions.

## Release notes

### 1.1.0 (dev)

* Introduced `ParserOptions::withDefaultOption()` and `FormatterOptions::withDefaultOption()`,
which return a copy of the options with the default applied;
the `defaultOption()` methods, which modify the options in place, are deprecated.

### 1.0.0 (2021-01-20)

* Updated minimum required PHP version from 5.5.9 to 7.2
Expand Down
25 changes: 25 additions & 0 deletions src/ValueFormatters/FormatterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ public function __construct( array $options = [] ) {
$this->options = $options;
}

/**
* Create and return a copy of these options,
* where the value of an option was set to the provided default
* if it had not been set already.
*
* Example usage:
* ```php
* public function __construct( FormatterOptions $options ) {
* $this->options = $options
* ->withDefaultOption( self::OPT_A, 'A' )
* ->withDefaultOption( self::OPT_B, 'B' );
* }
* ```
*
* @param string $option
* @param mixed $default
* @return self
*/
public function withDefaultOption( string $option, $default ): self {
$options = new self( $this->options );
$options->defaultOption( $option, $default );
return $options;
}

/**
* Sets the value of the specified option.
*
Expand Down Expand Up @@ -75,6 +99,7 @@ public function hasOption( string $option ): bool {
/**
* Sets the value of an option to the provided default in case the option is not set yet.
*
* @deprecated Use {@link withDefaultOption()} instead.
* @param string $option
* @param mixed $default
*/
Expand Down
25 changes: 25 additions & 0 deletions src/ValueParsers/ParserOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public function __construct( array $options = [] ) {
$this->options = $options;
}

/**
* Create and return a copy of these options,
* where the value of an option was set to the provided default
* if it had not been set already.
*
* Example usage:
* ```php
* public function __construct( ParserOptions $options ) {
* $this->options = $options
* ->withDefaultOption( self::OPT_A, 'A' )
* ->withDefaultOption( self::OPT_B, 'B' );
* }
* ```
*
* @param string $option
* @param mixed $default
* @return self
*/
public function withDefaultOption( string $option, $default ): self {
$options = new self( $this->options );
$options->defaultOption( $option, $default );
return $options;
}

/**
* Sets the value of the specified option.
*
Expand Down Expand Up @@ -76,6 +100,7 @@ public function hasOption( string $option ): bool {
/**
* Sets the value of an option to the provided default in case the option is not set yet.
*
* @deprecated Use {@link withDefaultOption()} instead.
* @param string $option
* @param mixed $default
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/ValueFormatters/FormatterOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,23 @@ public function testDefaultOption() {
}
}

public function testWithDefaultOption() {
$originalOptions = new FormatterOptions( [ 'foo' => 'foo' ] );

$newOptions = $originalOptions
->withDefaultOption( 'foo', 'FOO' )
->withDefaultOption( 'bar', 'BAR' );

$this->assertNotSame( $originalOptions, $newOptions,
'should be a fresh instance' );
$this->assertSame( 'foo', $originalOptions->getOption( 'foo' ),
'original options should have same non-default option' );
$this->assertFalse( $originalOptions->hasOption( 'bar' ),
'original options should not have default option' );
$this->assertSame( 'foo', $newOptions->getOption( 'foo' ),
'new options should have same non-default option' );
$this->assertSame( 'BAR', $newOptions->getOption( 'bar' ),
'new options should have default option' );
}

}
19 changes: 19 additions & 0 deletions tests/ValueParsers/ParserOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,23 @@ public function testDefaultOption() {
}
}

public function testWithDefaultOption() {
$originalOptions = new ParserOptions( [ 'foo' => 'foo' ] );

$newOptions = $originalOptions
->withDefaultOption( 'foo', 'FOO' )
->withDefaultOption( 'bar', 'BAR' );

$this->assertNotSame( $originalOptions, $newOptions,
'should be a fresh instance' );
$this->assertSame( 'foo', $originalOptions->getOption( 'foo' ),
'original options should have same non-default option' );
$this->assertFalse( $originalOptions->hasOption( 'bar' ),
'original options should not have default option' );
$this->assertSame( 'foo', $newOptions->getOption( 'foo' ),
'new options should have same non-default option' );
$this->assertSame( 'BAR', $newOptions->getOption( 'bar' ),
'new options should have default option' );
}

}

0 comments on commit 3cb96d5

Please sign in to comment.