From cf1183c39664d58a5cae94ba8a6192dfe275fd77 Mon Sep 17 00:00:00 2001 From: Robin Gustafsson Date: Sat, 17 Oct 2026 11:57:53 +0200 Subject: [PATCH] Handle class keyword used as named argument --- src/Parser.php | 5 +++++ tests/ParserTest.php | 9 +++++++++ tests/_data/parser/named-argument-class.php | 15 +++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 tests/_data/parser/named-argument-class.php diff --git a/src/Parser.php b/src/Parser.php index 0a3f480..3c2ccf4 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -589,6 +589,11 @@ private function classTokenNeedsProcessing($position) { return false; } + // PHP 8 allows keywords as named arguments: new Foo(class: $bar) + if ($this->tokenArray[$position + 1] === ':' || $this->tokenArray[$position + 2] === ':') { + return false; + } + return true; } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 6595483..21db020 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -138,6 +138,15 @@ public function testMultipleClasses() { $this->assertContains('demo3', $classes); } + /** + * @requires PHP 8.0 + */ + public function testNamedArgumentReusingClassKeywordGetsIgnored() { + $parser = new Parser(); + $rc = $parser->parse(new SourceFile((__DIR__.'/_data/parser/named-argument-class.php'))); + $this->assertEquals(array('foo'), $rc->getUnits()); + } + public function testExtends() { $parser = new \TheSeer\Autoload\Parser; $rc = $parser->parse(new SourceFile((__DIR__.'/_data/parser/extends.php'))); diff --git a/tests/_data/parser/named-argument-class.php b/tests/_data/parser/named-argument-class.php new file mode 100644 index 0000000..5a947af --- /dev/null +++ b/tests/_data/parser/named-argument-class.php @@ -0,0 +1,15 @@ +