Skip to content

Commit

Permalink
EB-14610 always remove even if key not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
lepiaf committed Dec 16, 2019
1 parent 4c32b49 commit 9591b13
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Monolog/Formatter/TokenCollectionFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function format(array $record)
public function format(array $record): string
{
$this->format = str_replace('%token_collection%', $this->getFormattedPlaceholder(), $this->originalFormat);

Expand Down
19 changes: 3 additions & 16 deletions Tests/Tracing/TokenCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,9 @@ public function overwrite_with_value(): void
/**
* @test
*/
public function remove_will_throw_invalid_argument_exception(): void
public function remove_do_nothing_and_return_current_instance(): void
{
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('The token "my_fake_name" doesn\'t exists.');

$this->tokenCollection->remove('my_fake_name');
}

/**
* @test
*/
public function remove_silently(): void
{
$this->tokenCollection->remove('my_fake_name', true);
$this->assertArrayNotHasKey('my_fake_name', $this->tokenCollection->getTokens());
}

Expand All @@ -114,12 +103,10 @@ public function remove(): void
/**
* @test
*/
public function replace_will_throw_out_of_bounds_exception(): void
public function replace_will_add_key_if_not_exists(): void
{
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('The token "my_fake_name" doesn\'t exists.');

$this->tokenCollection->replace('my_fake_name');
$this->assertArrayHasKey('my_fake_name', $this->tokenCollection->getTokens());
}

/**
Expand Down
11 changes: 3 additions & 8 deletions Tracing/TokenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,15 @@ public function replace(string $tokenName, ?string $tokenValue = null): self
* Remove a token from the collection.
*
* @param string $tokenName the name of the token to remove
* @param bool $silent whether an exception should be raised if the token does not exist
*
* @throws \OutOfBoundsException If the token does not exist and not in silent mode.
*
* @return TokenCollection
*/
public function remove(string $tokenName, bool $silent = false): self
public function remove(string $tokenName): self
{
if (!$silent && !\array_key_exists($tokenName, $this->tokens)) {
throw new \OutOfBoundsException(sprintf('The token "%s" doesn\'t exists.', $tokenName));
if (isset($this->tokens[$tokenName])) {
unset($this->tokens[$tokenName]);
}

unset($this->tokens[$tokenName]);

return $this;
}

Expand Down

0 comments on commit 9591b13

Please sign in to comment.