Skip to content

Commit

Permalink
Simplify redactSensitiveParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 23, 2024
1 parent b497829 commit 816bdab
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions uri/BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;

use function array_map;
use function array_pop;
use function array_reduce;
use function count;
Expand Down Expand Up @@ -278,33 +279,31 @@ public function hasIdn(): bool
* All redacted values will be replaced by the 5-star mask.
* The return value MAY not be a valid URI
*
* @param string ...$name
* @param string ...$names
*/
public function redactSensitiveParameters(string ...$name): string
public function redactSensitiveParameters(string ...$names): string
{
$components = UriString::parse($this);
if ($components['pass'] !== null) {
$components['pass'] = self::COMPONENT_MASK;
}

$currentQuery = $components['query'];
if (null === $currentQuery || !str_contains($currentQuery, '=')) {
if ([] === $names || null === $currentQuery || !str_contains($currentQuery, '=')) {
return UriString::build($components);
}

$name = array_map(Encoder::decodeAll(...), $name);
$names = array_map(Encoder::decodeAll(...), $names);
$pairs = [];
foreach (explode('&', $currentQuery) as $part) {
[$key, ] = explode('=', $part, 2) + [1 => null];
$pairs[] = (in_array(Encoder::decodeAll($key), $name, true)) ? $key.'='.self::COMPONENT_MASK : $part;
}

$query = implode('&', $pairs);
if ($currentQuery === $query) {
return UriString::build($components);
$pairs[] = match (in_array(Encoder::decodeAll($key), $names, true)) {
true => $key.'='.self::COMPONENT_MASK,
false => $part,
};
}

$components['query'] = $query;
$components['query'] = implode('&', $pairs);

return UriString::build($components);
}
Expand Down

0 comments on commit 816bdab

Please sign in to comment.