Skip to content

Commit

Permalink
Handle yield termination depending on context
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 28, 2023
1 parent 3e059c3 commit f653780
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@ protected function parseOptionalExpression()
$expressions[] = $expr;
break;
case Tokens::T_YIELD:
$expressions[] = $this->parseYield();
$expressions[] = $this->parseYield(false);
break;
default:
$expressions[] = $this->parseOptionalExpressionForVersion();
Expand Down Expand Up @@ -6706,7 +6706,7 @@ private function parseOptionalStatement()

return $class;
case Tokens::T_YIELD:
return $this->parseYield();
return $this->parseYield(true);
}

$this->tokenStack->push();
Expand Down Expand Up @@ -7908,9 +7908,12 @@ private function parseFieldDeclarationClassOrInterfaceReference()
/**
* This method parses a yield-statement node.
*
* @param bool $standalone Either yield is the statement (true), or nested in
* an expression (false).
*
* @return ASTYieldStatement
*/
private function parseYield()
private function parseYield($standalone)
{
$this->tokenStack->push();

Expand All @@ -7932,12 +7935,10 @@ private function parseYield()

$this->consumeComments();

if (Tokens::T_PARENTHESIS_CLOSE === $this->tokenizer->peek()) {
return $this->setNodePositionsAndReturn($yield);
if ($standalone) {
$this->parseStatementTermination();
}

$this->parseStatementTermination();

return $this->setNodePositionsAndReturn($yield);
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTYieldStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ public function testYield()
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTYieldStatement', $stmt);
}

/**
* testYieldAssignment
*
* @return void
*/
public function testYieldAssignment()
{
$stmt = $this->getFirstYieldStatementInFunction(__METHOD__);

$this->assertInstanceOf('PDepend\\Source\\AST\\ASTYieldStatement', $stmt);
$assignment = $stmt->getParent();
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTAssignmentExpression', $assignment);
$this->assertSame('$result', $assignment->getChild(0)->getImage());

$this->assertSame(array(
'PDepend\\Source\\AST\\ASTLiteral',
), array_map('get_class', $stmt->getChildren()));
$this->assertSame('23', $stmt->getChild(0)->getImage());
}

/**
* testYieldWithLiteral
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function testYieldAssignment()
{
$result = yield 23;
}

0 comments on commit f653780

Please sign in to comment.