Skip to content

Property hook #2

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

Merged
merged 3 commits into from
Feb 5, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ public function getFullWidth() : int {
* Gets string representing Node text (not including leading comment + whitespace trivia)
* @return string
*/
public function getText() : string {
public function getText(?string $fileContents= null) : string {
$start = $this->getStartPosition();
$end = $this->getEndPosition();

$fileContents = $this->getFileContents();
$fileContents = $fileContents ?? $this->getFileContents();
return \substr($fileContents, $start, $end - $start);
}

Expand Down
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'
];
}
45 changes: 45 additions & 0 deletions src/Node/PropertyHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\FunctionLike;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Statement\CompoundStatementNode;
use Microsoft\PhpParser\Token;

class PropertyHook extends Node implements FunctionLike {
/** @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;

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: 115 additions & 2 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 @@ -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 @@ -2186,6 +2192,7 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
// the original operator, and the newly constructed exponentiation-expression as the operand.
$shouldOperatorTakePrecedenceOverUnary = false;
switch ($token->kind) {
case TokenKind::OpenBraceToken:
case TokenKind::AsteriskAsteriskToken:
$shouldOperatorTakePrecedenceOverUnary = $leftOperand instanceof UnaryExpression;
break;
Expand Down Expand Up @@ -3050,6 +3057,9 @@ private function parsePostfixExpressionRest($expression, $allowUpdateExpression
)) {
return $expression;
}
if ($tokenKind === TokenKind::OpenBraceToken) {
return $expression;
}
if ($tokenKind === TokenKind::ColonColonToken) {
$expression = $this->parseScopedPropertyAccessExpression($expression, null);
return $this->parsePostfixExpressionRest($expression);
Expand Down Expand Up @@ -3437,12 +3447,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 $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) {
$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
8 changes: 2 additions & 6 deletions tests/cases/lexical/keyword5.php.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
"textLength": 6
},
{
"kind": "YieldKeyword",
"textLength": 5
},
{
"kind": "Name",
"textLength": 4
"kind": "YieldFromKeyword",
"textLength": 22
},
{
"kind": "EndOfFileToken",
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
Loading
Loading