Skip to content

Commit e16d86d

Browse files
authored
Merge pull request #1176 from sparklink-pro/master
Ensure that a #[GQL\Arg] on method has a matching parameter
2 parents f990e0b + c45817f commit e16d86d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Config/Parser/MetadataParser/MetadataParser.php

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ReflectionClassConstant;
2020
use ReflectionException;
2121
use ReflectionMethod;
22+
use ReflectionParameter;
2223
use ReflectionProperty;
2324
use Reflector;
2425
use RuntimeException;
@@ -591,7 +592,13 @@ private static function getTypeFieldConfigurationFromReflector(ReflectionClass $
591592
/** @var Metadata\Arg[] $argAnnotations */
592593
$argAnnotations = self::getMetadataMatching($metadatas, Metadata\Arg::class);
593594

595+
$validArgNames = array_map(fn (ReflectionParameter $parameter) => $parameter->getName(), $reflector instanceof ReflectionMethod ? $reflector->getParameters() : []);
596+
594597
foreach ($argAnnotations as $arg) {
598+
if ($reflector instanceof ReflectionMethod && !in_array($arg->name, $validArgNames, true)) {
599+
throw new InvalidArgumentException(sprintf('The argument "%s" defined with #[GQL\Arg] attribute/annotation on method "%s" does not match any parameter name in the method.', $arg->name, $reflector->getName()));
600+
}
601+
595602
$args[$arg->name] = ['type' => $arg->type];
596603

597604
if (isset($arg->description)) {

tests/Config/Parser/MetadataParserTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,18 @@ public function testInvalidParamGuessing(): void
524524
}
525525
}
526526

527+
public function testInvalidArgumentMatching(): void
528+
{
529+
try {
530+
$file = __DIR__.'/fixtures/annotations/Invalid/InvalidArgumentNaming.php';
531+
$this->parser('parse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig);
532+
$this->fail('Missing matching argument should have raise an exception');
533+
} catch (Exception $e) {
534+
$this->assertInstanceOf(InvalidArgumentException::class, $e);
535+
$this->assertMatchesRegularExpression('/The argument "missingParameter" defined/', $e->getPrevious()->getMessage());
536+
}
537+
}
538+
527539
public function testInvalidReturnGuessing(): void
528540
{
529541
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Invalid;
6+
7+
use Overblog\GraphQLBundle\Annotation as GQL;
8+
9+
/**
10+
* @GQL\Type
11+
*/
12+
#[GQL\Type]
13+
final class InvalidArgumentNaming
14+
{
15+
/**
16+
* @GQL\Field(name="guessFailed")
17+
*
18+
* @GQL\Arg(name="missingParameter", type="String")
19+
*/
20+
#[GQL\Field(name: 'guessFailed')]
21+
#[GQL\Arg(name: 'missingParameter', type: 'String')]
22+
public function guessFail(int $test): int
23+
{
24+
return 12;
25+
}
26+
}

0 commit comments

Comments
 (0)