Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AnonymousFunctionCreationExpression extends Expression {

// FunctionReturnType
'colonToken',
'questionToken',
'returnType',

// FunctionBody
Expand Down
2 changes: 2 additions & 0 deletions src/Node/FunctionReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
trait FunctionReturnType {
/** @var Token */
public $colonToken;
/** @var Token|null */
public $questionToken;
/** @var Token | QualifiedName */
public $returnType;
}
1 change: 1 addition & 0 deletions src/Node/MethodDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MethodDeclaration extends Node {

// FunctionReturnType
'colonToken',
'questionToken',
'returnType',

// FunctionBody
Expand Down
6 changes: 3 additions & 3 deletions src/Node/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
use Microsoft\PhpParser\Token;

class Parameter extends Node {
/** @var Token|null */
public $questionToken;
/** @var QualifiedName | Token | null */
public $typeDeclaration;
/** @var Token | null */
public $byRefToken;
/** @var Token | null */
public $dotDotDotToken;

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

/** @var Token | null */
public $equalsToken;

/** @var null | Expression */
public $default;

const CHILD_NAMES = [
'questionToken',
'typeDeclaration',
'byRefToken',
'dotDotDotToken',
Expand Down
1 change: 1 addition & 0 deletions src/Node/Statement/FunctionDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class FunctionDeclaration extends StatementNode implements NamespacedNameInterfa

// FunctionReturnType
'colonToken',
'questionToken',
'returnType',

// FunctionBody
Expand Down
42 changes: 24 additions & 18 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ private function parseParameterFn() {
return function ($parentNode) {
$parameter = new Parameter();
$parameter->parent = $parentNode;
$parameter->questionToken = $this->eatOptional(TokenKind::QuestionToken);
$parameter->typeDeclaration = $this->tryParseParameterTypeDeclaration($parameter);
$parameter->byRefToken = $this->eatOptional(TokenKind::AmpersandToken);
// TODO add post-parse rule that prevents assignment
Expand Down Expand Up @@ -1086,6 +1087,10 @@ private function isParameterStartFn() {

case TokenKind::VariableName:
return true;

// nullable-type
case TokenKind::QuestionToken:
return true;
}

// scalar-type
Expand Down Expand Up @@ -1195,45 +1200,46 @@ private function parseRelativeSpecifier($parentNode) {
return null;
}

private function parseFunctionType(Node $functionDefinition, $canBeAbstract = false, $isAnonymous = false) {
$functionDefinition->functionKeyword = $this->eat(TokenKind::FunctionKeyword);
$functionDefinition->byRefToken = $this->eatOptional(TokenKind::AmpersandToken);
$functionDefinition->name = $isAnonymous
private function parseFunctionType(Node $functionDeclaration, $canBeAbstract = false, $isAnonymous = false) {
$functionDeclaration->functionKeyword = $this->eat(TokenKind::FunctionKeyword);
$functionDeclaration->byRefToken = $this->eatOptional(TokenKind::AmpersandToken);
$functionDeclaration->name = $isAnonymous
? $this->eatOptional($this->nameOrKeywordOrReservedWordTokens)
: $this->eat($this->nameOrKeywordOrReservedWordTokens);

if (isset($functionDefinition->name)) {
$functionDefinition->name->kind = TokenKind::Name;
if (isset($functionDeclaration->name)) {
$functionDeclaration->name->kind = TokenKind::Name;
}

if ($isAnonymous && isset($functionDefinition->name)) {
if ($isAnonymous && isset($functionDeclaration->name)) {
// Anonymous functions should not have names
$functionDefinition->name = new SkippedToken($functionDefinition->name); // TODO instaed handle this during post-walk
$functionDeclaration->name = new SkippedToken($functionDeclaration->name); // TODO instaed handle this during post-walk
}

$functionDefinition->openParen = $this->eat(TokenKind::OpenParenToken);
$functionDefinition->parameters = $this->parseDelimitedList(
$functionDeclaration->openParen = $this->eat(TokenKind::OpenParenToken);
$functionDeclaration->parameters = $this->parseDelimitedList(
DelimitedList\ParameterDeclarationList::class,
TokenKind::CommaToken,
$this->isParameterStartFn(),
$this->parseParameterFn(),
$functionDefinition);
$functionDefinition->closeParen = $this->eat(TokenKind::CloseParenToken);
$functionDeclaration);
$functionDeclaration->closeParen = $this->eat(TokenKind::CloseParenToken);
if ($isAnonymous) {
$functionDefinition->anonymousFunctionUseClause = $this->parseAnonymousFunctionUseClause($functionDefinition);
$functionDeclaration->anonymousFunctionUseClause = $this->parseAnonymousFunctionUseClause($functionDeclaration);
}

if ($this->checkToken(TokenKind::ColonToken)) {
$functionDefinition->colonToken = $this->eat(TokenKind::ColonToken);
$functionDefinition->returnType = $this->parseReturnTypeDeclaration($functionDefinition);
$functionDeclaration->colonToken = $this->eat(TokenKind::ColonToken);
$functionDeclaration->questionToken = $this->eatOptional(TokenKind::QuestionToken);
$functionDeclaration->returnType = $this->parseReturnTypeDeclaration($functionDeclaration);
}

if ($canBeAbstract) {
$functionDefinition->compoundStatementOrSemicolon = $this->eatOptional(TokenKind::SemicolonToken);
$functionDeclaration->compoundStatementOrSemicolon = $this->eatOptional(TokenKind::SemicolonToken);
}

if (!isset($functionDefinition->compoundStatementOrSemicolon)) {
$functionDefinition->compoundStatementOrSemicolon = $this->parseCompoundStatement($functionDefinition);
if (!isset($functionDeclaration->compoundStatementOrSemicolon)) {
$functionDeclaration->compoundStatementOrSemicolon = $this->parseCompoundStatement($functionDeclaration);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"kind": "SemicolonToken",
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"kind": "SemicolonToken",
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"kind": "ColonToken",
"textLength": 1
},
"questionToken": null,
"returnType": {
"kind": "BoolReservedWord",
"textLength": 4
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"kind": "ColonToken",
"textLength": 1
},
"questionToken": null,
"returnType": {
"kind": "BoolReservedWord",
"textLength": 4
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration7.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"anonymousFunctionUseClause": null,
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"kind": "ColonToken",
"textLength": 1
},
"questionToken": null,
"returnType": {
"kind": "VoidReservedWord",
"textLength": 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"anonymousFunctionUseClause": null,
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"anonymousFunctionUseClause": null,
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"anonymousFunctionUseClause": null,
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"anonymousFunctionUseClause": null,
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"anonymousFunctionUseClause": null,
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
}
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classMethods1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/classMethods3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"children": [
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": null,
"dotDotDotToken": null,
Expand All @@ -74,6 +75,7 @@
},
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": null,
"dotDotDotToken": null,
Expand All @@ -93,6 +95,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/classMethods4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"children": [
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": null,
"dotDotDotToken": null,
Expand All @@ -74,6 +75,7 @@
},
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": null,
"dotDotDotToken": null,
Expand All @@ -96,6 +98,7 @@
"kind": "ColonToken",
"textLength": 1
},
"questionToken": null,
"returnType": {
"kind": "IntReservedWord",
"textLength": 3
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/classMethods5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down Expand Up @@ -102,6 +103,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/constructorDeclaration1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/functions1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/functions10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"children": [
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": null,
"dotDotDotToken": null,
Expand All @@ -48,6 +49,7 @@
},
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": {
"kind": "AmpersandToken",
Expand All @@ -73,6 +75,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/functions11.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"children": [
{
"Parameter": {
"questionToken": null,
"typeDeclaration": null,
"byRefToken": null,
"dotDotDotToken": null,
Expand All @@ -48,6 +49,7 @@
},
{
"Parameter": {
"questionToken": null,
"typeDeclaration": {
"QualifiedName": {
"globalSpecifier": null,
Expand Down Expand Up @@ -84,6 +86,7 @@
"textLength": 1
},
"colonToken": null,
"questionToken": null,
"returnType": null,
"compoundStatementOrSemicolon": {
"CompoundStatementNode": {
Expand Down
Loading