Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing of PHP 8.4 property hooks #411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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: 4 additions & 1 deletion src/Node/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Parameter extends Node {
public $equalsToken;
/** @var null|Expression */
public $default;
/** @var PropertyHooks|null */
public $propertyHooks;

const CHILD_NAMES = [
'attributes',
Expand All @@ -42,7 +44,8 @@ class Parameter extends Node {
'dotDotDotToken',
'variableName',
'equalsToken',
'default'
'default',
'propertyHooks',
];

public function isVariadic() {
Expand Down
5 changes: 5 additions & 0 deletions src/Node/PropertyDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Microsoft\PhpParser\ModifiedTypeInterface;
use Microsoft\PhpParser\ModifiedTypeTrait;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\PropertyHooks;
use Microsoft\PhpParser\Node\DelimitedList\QualifiedNameList;
use Microsoft\PhpParser\Token;

Expand All @@ -28,6 +29,9 @@ class PropertyDeclaration extends Node implements ModifiedTypeInterface {
/** @var DelimitedList\ExpressionList */
public $propertyElements;

/** @var PropertyHooks|null */
public $propertyHooks;

/** @var Token */
public $semicolon;

Expand All @@ -37,6 +41,7 @@ class PropertyDeclaration extends Node implements ModifiedTypeInterface {
'questionToken',
'typeDeclarationList',
'propertyElements',
'propertyHooks',
'semicolon'
];
}
44 changes: 44 additions & 0 deletions src/Node/PropertyHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

namespace Microsoft\PhpParser\Node;

use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Statement\CompoundStatementNode;
use Microsoft\PhpParser\Token;

class PropertyHook extends Node {
/** @var AttributeGroup[]|null */
public $attributes;
/** @var Token */
public $byRefToken;
/** @var Token */
public $name;
/** @var Token|null */
public $openParen;
/** @var DelimitedList\ParameterDeclarationList|null */
public $parameters;
/** @var Token|null */
public $closeParen;
/** @var Token|null */
public $arrowToken;
/** @var CompoundStatementNode|Expression|Token|null */
public $body;
/** @var Token|null */
public $semicolon;
Copy link
Contributor

@dantleech dantleech Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR but I guess we could consider bumping the minimum version to 8.0 or even 7.4? (nikic parser requires at least 7.4)


const CHILD_NAMES = [
'attributes',
'byRefToken',
'name',
'openParen',
'parameters',
'closeParen',
'arrowToken',
'body',
'semicolon',
];
}
28 changes: 28 additions & 0 deletions src/Node/PropertyHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

namespace Microsoft\PhpParser\Node;

use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\PropertyHooks;
use Microsoft\PhpParser\Token;

class PropertyHooks extends Node {
/** @var Token */
public $openBrace;

/** @var PropertyHook[] */
public $hookDeclarations;

/** @var Token */
public $closeBrace;

const CHILD_NAMES = [
'openBrace',
'hookDeclarations',
'closeBrace'
];
}
117 changes: 113 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
use Microsoft\PhpParser\Node\NumericLiteral;
use Microsoft\PhpParser\Node\ParenthesizedIntersectionType;
use Microsoft\PhpParser\Node\PropertyDeclaration;
use Microsoft\PhpParser\Node\PropertyHooks;
use Microsoft\PhpParser\Node\PropertyHook;
use Microsoft\PhpParser\Node\ReservedWord;
use Microsoft\PhpParser\Node\StringLiteral;
use Microsoft\PhpParser\Node\MethodDeclaration;
Expand Down Expand Up @@ -175,7 +177,7 @@ protected function makeLexer(string $fileContents): TokenStreamProviderInterface
* @param string $fileContents
* @return SourceFileNode
*/
public function parseSourceFile(string $fileContents, string $uri = null) : SourceFileNode {
public function parseSourceFile(string $fileContents, ?string $uri = null) : SourceFileNode {
$this->lexer = $this->makeLexer($fileContents);

$this->reset();
Expand Down Expand Up @@ -877,6 +879,10 @@ private function parseParameterFn() {
// TODO add post-parse rule that checks for invalid assignments
$parameter->default = $this->parseExpression($parameter);
}

if ($this->token->kind === TokenKind::OpenBraceToken) {
$parameter->propertyHooks = $this->parsePropertyHooks($parameter);
}
return $parameter;
};
}
Expand Down Expand Up @@ -1417,7 +1423,7 @@ private function parseStringLiteralExpression2($parentNode): StringLiteral {
case TokenKind::DollarOpenBraceToken:
case TokenKind::OpenBraceDollarToken:
$expression->children[] = $this->eat(TokenKind::DollarOpenBraceToken, TokenKind::OpenBraceDollarToken);
/**
/**
* @phpstan-ignore-next-line "Strict comparison using
* === between 403|404 and 408 will always evaluate to
* false" is wrong because those tokens were eaten above
Expand Down Expand Up @@ -3423,12 +3429,115 @@ private function parsePropertyDeclaration($parentNode, $modifiers, $questionToke
} elseif ($questionToken) {
$propertyDeclaration->typeDeclarationList = new MissingToken(TokenKind::PropertyType, $this->token->fullStart);
}
$propertyDeclaration->propertyElements = $this->parseExpressionList($propertyDeclaration);
$propertyDeclaration->semicolon = $this->eat1(TokenKind::SemicolonToken);
$propertyDeclaration->propertyElements = $this->parsePropertyNameList($propertyDeclaration);
if ($this->token->kind === TokenKind::OpenBraceToken) {
$propertyDeclaration->propertyHooks = $this->parsePropertyHooks($propertyDeclaration);
} else {
$propertyDeclaration->semicolon = $this->eat1(TokenKind::SemicolonToken);
}

return $propertyDeclaration;
}

/**
* @param PropertyDeclaration $parentNode
* @return DelimitedList\VariableNameList
*/
private function parsePropertyNameList($parentNode) {
// XXX this used to be implemented with parseExpressionList so keep the same classes.
return $this->parseDelimitedList(
DelimitedList\ExpressionList::class,
TokenKind::CommaToken,
function ($token) {
return $token->kind === TokenKind::VariableName;
},
$this->parsePropertyVariableNameAndDefault(),
$parentNode
);
}

private function parsePropertyVariableNameAndDefault() {
return function ($parentNode) {
// Imitate the format that parseExpression would have returned from when
// parseExpression was originally used.
// This is more precise and rules out parse errors such as `public $propName + 2;`
// This approach also avoids conflict with the deprecated $x{expr} array access syntax.
$variable = new Variable();
$variable->name = $this->eat1(TokenKind::VariableName);
$equalsToken = $this->eatOptional1(TokenKind::EqualsToken);
if ($equalsToken === null) {
$variable->parent = $parentNode;
return $variable;
}

$byRefToken = $this->eatOptional1(TokenKind::AmpersandToken); // byRef default is nonsense, but this is a compile-time error instead of a parse error.
$rightOperand = $this->parseExpression($parentNode); // gets overridden in makeBinaryExpression
return $this->makeBinaryExpression($variable, $equalsToken, $byRefToken, $rightOperand, $parentNode);
};
}

/**
* @param PropertyDeclaration|Parameter $parent
* @return PropertyHooks
*/
private function parsePropertyHooks(Node $parent) {
$propertyHooks = new PropertyHooks();
$propertyHooks->parent = $parent;
$propertyHooks->openBrace = $this->eat1(TokenKind::OpenBraceToken);
$hooks = [];
while (in_array($this->getCurrentToken()->kind, self::PROPERTY_HOOK_START_TOKENS, true)) {
$hooks[] = $this->parsePropertyHook($propertyHooks);
}
$propertyHooks->hookDeclarations = $hooks;
$propertyHooks->closeBrace = $this->eat1(TokenKind::CloseBraceToken);
return $propertyHooks;
}

const PROPERTY_HOOK_START_TOKENS = [
TokenKind::Name,
TokenKind::AmpersandToken, // by reference
TokenKind::AttributeToken,
];

private function isPropertyHookStart() {
return function ($token) {
return \in_array($token->kind, self::PROPERTY_HOOK_START_TOKENS, true);
};
}

/**
* @param PropertyHooks $parentNode
* @return PropertyHook
*/
private function parsePropertyHook($parentNode) {
Copy link
Contributor

@dantleech dantleech Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can safely add a real type hint here I think and a real return type?

$node = new PropertyHook();
$node->parent = $parentNode;
if ($this->getCurrentToken()->kind === TokenKind::AttributeToken) {
$node->attributes = $this->parseAttributeGroups($node);
}
$node->byRefToken = $this->eatOptional1(TokenKind::AmpersandToken);
$node->name = $this->eat1(TokenKind::Name); // "get" or "set" - other values are compile errors, not parse errors.
$node->openParen = $this->eatOptional1(TokenKind::OpenParenToken);
if ($node->openParen) {
$node->parameters = $this->parseDelimitedList(
DelimitedList\ParameterDeclarationList::class,
TokenKind::CommaToken,
$this->isParameterStartFn(),
$this->parseParameterFn(),
$node);
$node->closeParen = $this->eat1(TokenKind::CloseParenToken);
}
// e.g. `get => expr;` or `get { return $expr; }`
$node->arrowToken = $this->eatOptional1(TokenKind::DoubleArrowToken);
if ($node->arrowToken) {
$node->body = $this->parseExpression($node);
$node->semicolon = $this->eat1(TokenKind::SemicolonToken);
} else {
$node->body = $this->parseCompoundStatement($node);
}
return $node;
}

/**
* Parse a comma separated qualified name list (e.g. interfaces implemented by a class)
*
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/parser/classMethods3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"textLength": 8
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
},
{
Expand All @@ -93,7 +94,8 @@
"textLength": 6
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/parser/classMethods4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"textLength": 8
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
},
{
Expand All @@ -93,7 +94,8 @@
"textLength": 6
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/dnfTypesParameter1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"textLength": 2
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/dnfTypesParameter2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
"textLength": 2
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/dnfTypesParameter3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"textLength": 2
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/dnfTypesParameter4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"textLength": 0
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/dnfTypesParameter5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"textLength": 0
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/dnfTypesProperty1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
]
}
},
"propertyHooks": null,
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/parser/functions10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"textLength": 2
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
},
{
Expand All @@ -71,7 +72,8 @@
"textLength": 2
},
"equalsToken": null,
"default": null
"default": null,
"propertyHooks": null
}
}
]
Expand Down
Loading