Skip to content

Commit

Permalink
Exception when value is null (#9)
Browse files Browse the repository at this point in the history
* fix: 🐛 exception when value is null

* test: ✅ add test for null value
  • Loading branch information
deoomen authored Apr 19, 2024
1 parent 95e0e31 commit e8f5a41
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/RedactSensitiveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function traverseArr(array $arr, array $keys): array
} else {
if (array_key_exists($key, $keys)) {
$arr[$key] = $this->traverse($key, $value, $keys[$key]);
} else {
} elseif (null !== $value) {
$arr[$key] = $this->traverse($key, $value, $keys);
}
}
Expand All @@ -115,7 +115,7 @@ private function traverseObj(object $obj, array $keys): object
} else {
if (array_key_exists($key, $keys)) {
$obj->{$key} = $this->traverse($key, $value, $keys[$key]);
} else {
} elseif (null !== $value) {
$obj->{$key} = $this->traverse($key, $value, $keys);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/RedactSensitiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@
$record = $this->getRecord(context: ['test' => fopen(__FILE__, 'rb')]);
$processor($record);
})->throws(\UnexpectedValueException::class, 'Don\'t know how to traverse value at key test');

it('ignore when null value', function (): void {
$sensitive_keys = ['test' => 3];
$processor = new RedactSensitiveProcessor($sensitive_keys);

$record = $this->getRecord(context: ['test' => 'foobar', 'optionalKey' => null]);
expect($processor($record)->context)->toBe(['test' => 'foo***', 'optionalKey' => null]);
});

0 comments on commit e8f5a41

Please sign in to comment.