Skip to content

Commit

Permalink
Utility\CaseInsensitiveDictionary: minor bug fix
Browse files Browse the repository at this point in the history
The use of `strtolower()` on non-string keys, is 1) not necessary and 2) had side-effects when non-string keys were used as those were then cast to string, which with the current set of tests would lead to the `false` test case overwritting the `null` test case and potentially "passing null to non-nullable" errors in PHP 8.1.

While this class is intended to be used with requests headers, the class in itself is not limited to this use-case, so should function as per the specifications, independently of the use-case.

Alternatively, a stricter check could be put in place to only allow string keys for the data captured in this class. That would potentially create a bigger breaking change though.
  • Loading branch information
jrfnl committed Oct 4, 2021
1 parent eb27e06 commit 2faecbc
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Utility/CaseInsensitiveDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function __construct(array $data = array()) {
*/
#[ReturnTypeWillChange]
public function offsetExists($offset) {
$offset = strtolower($offset);
if (is_string($offset) {
$offset = strtolower($offset);
}

return isset($this->data[$offset]);
}

Expand All @@ -59,7 +62,10 @@ public function offsetExists($offset) {
*/
#[ReturnTypeWillChange]
public function offsetGet($offset) {
$offset = strtolower($offset);
if (is_string($offset)) {
$offset = strtolower($offset);
}

if (!isset($this->data[$offset])) {
return null;
}
Expand All @@ -81,7 +87,10 @@ public function offsetSet($offset, $value) {
throw new Exception('Object is a dictionary, not a list', 'invalidset');
}

$offset = strtolower($offset);
if (is_string($offset)) {
$offset = strtolower($offset);
}

$this->data[$offset] = $value;
}

Expand All @@ -92,7 +101,11 @@ public function offsetSet($offset, $value) {
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset) {
unset($this->data[strtolower($offset)]);
if (is_string($offset)) {
$offset = strtolower($offset);
}

unset($this->data[$offset]);
}

/**
Expand Down

0 comments on commit 2faecbc

Please sign in to comment.