diff --git a/Monolog/Formatter/TokenCollectionFormatter.php b/Monolog/Formatter/TokenCollectionFormatter.php index 7b30da6..5375e53 100644 --- a/Monolog/Formatter/TokenCollectionFormatter.php +++ b/Monolog/Formatter/TokenCollectionFormatter.php @@ -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); diff --git a/Tests/Tracing/TokenCollectionTest.php b/Tests/Tracing/TokenCollectionTest.php index 88c5862..b177e6e 100644 --- a/Tests/Tracing/TokenCollectionTest.php +++ b/Tests/Tracing/TokenCollectionTest.php @@ -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()); } @@ -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()); } /** diff --git a/Tracing/TokenCollection.php b/Tracing/TokenCollection.php index 939d932..2666a62 100644 --- a/Tracing/TokenCollection.php +++ b/Tracing/TokenCollection.php @@ -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; }