Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Configuration/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Cdn77\RabbitMQBundle\Exception\InvalidDsn;

use function array_diff_key;
use function array_filter;
use function array_keys;
use function count;
use function http_build_query;
use function parse_str;
Expand Down Expand Up @@ -34,7 +37,7 @@
/** @var string */
private $vhost;

/** @var array<array<mixed>|string> */
/** @var string[] */
private $parameters = [];
Comment on lines +40 to 41

public function __construct(string $dsn)
Expand All @@ -42,7 +45,7 @@
$parts = parse_url($dsn);

if ($parts === false) {
throw InvalidDsn::malformed();

Check warning on line 48 in src/Configuration/Dsn.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ { $parts = parse_url($dsn); if ($parts === false) { - throw InvalidDsn::malformed(); + InvalidDsn::malformed(); } if (!isset($parts['scheme'], $parts['host'], $parts['path'])) { throw InvalidDsn::missingComponents();
}

if (! isset($parts['scheme'], $parts['host'], $parts['path'])) {
Expand All @@ -63,7 +66,15 @@
return;
}

parse_str($parts['query'], $this->parameters);
parse_str($parts['query'], $parameters);

$values = array_filter($parameters, 'is_string');

if (count($values) !== count($parameters)) {
throw InvalidDsn::nestedParameters(array_keys(array_diff_key($parameters, $values)));
}

$this->parameters = $values;
}

public function __toString(): string
Expand Down Expand Up @@ -106,7 +117,7 @@
return $this->vhost;
}

/** @return array<array<mixed>|string> */
/** @return string[] */
public function getParameters(): array
{
return $this->parameters;
Expand Down
10 changes: 10 additions & 0 deletions src/Exception/InvalidDsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use LogicException;

use function implode;
use function sprintf;

final class InvalidDsn extends LogicException implements Exception
Expand All @@ -24,4 +25,13 @@ public static function invalidScheme(string $provided, string $expected): self
{
throw new self(sprintf('The provided scheme "%s" is invalid, expected "%s".', $provided, $expected));
}

/** @param array<int|string> $keys */
public static function nestedParameters(array $keys): self
{
Comment on lines +29 to +31
throw new self(sprintf(
'The DSN query parameters "%s" are nested, expected a single value for each.',
implode('", "', $keys)
));
}
}
8 changes: 8 additions & 0 deletions tests/Configuration/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public function testInvalidScheme(): void
new Dsn('http://example.com//vhost');
}

public function testNestedParameters(): void
{
$this->expectException(InvalidDsn::class);
$this->expectExceptionMessage('The DSN query parameters "heartbeat" are nested, expected a single value');

new Dsn('amqp://host/vhost?heartbeat[]=120&connection_timeout=7');
}

/**
* @param string[] $parameters
*
Expand Down
Loading