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
5 changes: 5 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand Down
15 changes: 15 additions & 0 deletions tests/_data/parser/named-argument-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class Foo
{
public function __construct(public string $class) {}
}

new Foo(class: "bar");


function foo(string $class) {}

foo(class: "bar");

?>