diff --git a/src/Config/Parser/MetadataParser/MetadataParser.php b/src/Config/Parser/MetadataParser/MetadataParser.php index 2df977942..fe906a474 100644 --- a/src/Config/Parser/MetadataParser/MetadataParser.php +++ b/src/Config/Parser/MetadataParser/MetadataParser.php @@ -19,6 +19,7 @@ use ReflectionClassConstant; use ReflectionException; use ReflectionMethod; +use ReflectionParameter; use ReflectionProperty; use Reflector; use RuntimeException; @@ -591,7 +592,13 @@ private static function getTypeFieldConfigurationFromReflector(ReflectionClass $ /** @var Metadata\Arg[] $argAnnotations */ $argAnnotations = self::getMetadataMatching($metadatas, Metadata\Arg::class); + $validArgNames = array_map(fn (ReflectionParameter $parameter) => $parameter->getName(), $reflector instanceof ReflectionMethod ? $reflector->getParameters() : []); + foreach ($argAnnotations as $arg) { + if ($reflector instanceof ReflectionMethod && !in_array($arg->name, $validArgNames, true)) { + 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())); + } + $args[$arg->name] = ['type' => $arg->type]; if (isset($arg->description)) { diff --git a/tests/Config/Parser/MetadataParserTest.php b/tests/Config/Parser/MetadataParserTest.php index 00290fd81..c24695f21 100644 --- a/tests/Config/Parser/MetadataParserTest.php +++ b/tests/Config/Parser/MetadataParserTest.php @@ -524,6 +524,18 @@ public function testInvalidParamGuessing(): void } } + public function testInvalidArgumentMatching(): void + { + try { + $file = __DIR__.'/fixtures/annotations/Invalid/InvalidArgumentNaming.php'; + $this->parser('parse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig); + $this->fail('Missing matching argument should have raise an exception'); + } catch (Exception $e) { + $this->assertInstanceOf(InvalidArgumentException::class, $e); + $this->assertMatchesRegularExpression('/The argument "missingParameter" defined/', $e->getPrevious()->getMessage()); + } + } + public function testInvalidReturnGuessing(): void { try { diff --git a/tests/Config/Parser/fixtures/annotations/Invalid/InvalidArgumentNaming.php b/tests/Config/Parser/fixtures/annotations/Invalid/InvalidArgumentNaming.php new file mode 100644 index 000000000..fe45a3723 --- /dev/null +++ b/tests/Config/Parser/fixtures/annotations/Invalid/InvalidArgumentNaming.php @@ -0,0 +1,26 @@ +