Skip to content

Commit 89ae391

Browse files
committed
Add generator param type check
1 parent 3c65ef3 commit 89ae391

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

src/ChainRouter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ private function doMatch($pathinfo, Request $request = null)
217217
*/
218218
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
219219
{
220+
if (!is_string($name)) {
221+
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
222+
}
223+
220224
$debug = [];
221225

222226
foreach ($this->all() as $router) {

src/ContentAwareGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
6565
*/
6666
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
6767
{
68+
if (!is_string($name)) {
69+
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
70+
}
71+
6872
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
6973
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
7074
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute

src/DynamicRouter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ public function getGenerator()
165165
*/
166166
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
167167
{
168+
if (!is_string($name)) {
169+
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
170+
}
171+
168172
if ($this->eventDispatcher) {
169173
$event = new RouterGenerateEvent($name, $parameters, $referenceType);
170174
$this->eventDispatcher->dispatch($event, Events::PRE_DYNAMIC_GENERATE);

src/ProviderBasedGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function __construct(RouteProviderInterface $provider, LoggerInterface $l
4646
*/
4747
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH): string
4848
{
49+
if (!is_string($name)) {
50+
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
51+
}
52+
4953
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
5054
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
5155
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute

tests/Unit/Routing/ChainRouterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,23 @@ public function testRouteCollection()
709709
$this->assertEquals(['high', 'low'], $names);
710710
}
711711

712+
public function testGenerateWithNameParameterObject()
713+
{
714+
$this->expectException(\InvalidArgumentException::class);
715+
716+
$parameters = ['test' => 'value'];
717+
$defaultRouter = $this->createMock(RouterInterface::class);
718+
719+
$defaultRouter
720+
->expects($this->never())
721+
->method('generate')
722+
;
723+
724+
$this->router->add($defaultRouter, 200);
725+
726+
$this->router->generate(new \stdClass(), $parameters);
727+
}
728+
712729
/**
713730
* @group legacy
714731
*/

tests/Unit/Routing/ContentAwareGeneratorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ public function testGetRouteDebugMessageLegacy(): void
507507
$this->assertStringContainsString('Route aware content Symfony\Cmf\Component\Routing\Tests\Routing\RouteAware', $this->generator->getRouteDebugMessage(new RouteAware()));
508508
$this->assertStringContainsString('/some/content', $this->generator->getRouteDebugMessage('/some/content'));
509509
}
510+
511+
public function testGenerateWithNameParameterObject(): void
512+
{
513+
$this->expectException(\InvalidArgumentException::class);
514+
515+
$this->generator->generate(new \stdClass());
516+
}
510517
}
511518

512519
/**

tests/Unit/Routing/ProviderBasedGeneratorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ public function testGenerateByRoute(): void
155155
'number' => 'string',
156156
]);
157157
}
158+
159+
public function testGenerateWithNameParameterObject(): void
160+
{
161+
$this->expectException(\InvalidArgumentException::class);
162+
163+
$this->generator->generate(new \stdClass());
164+
}
158165
}
159166

160167
/**

0 commit comments

Comments
 (0)