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

[EncapsedStringPart] Add rawValue attribute #837

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions grammar/php5.y
Expand Up @@ -779,8 +779,7 @@ exit_expr:

backticks_expr:
/* empty */ { $$ = array(); }
| T_ENCAPSED_AND_WHITESPACE
{ $$ = array(Scalar\EncapsedStringPart[Scalar\String_::parseEscapeSequences($1, '`', false)]); }
| T_ENCAPSED_AND_WHITESPACE { $$ = array(Scalar\EncapsedStringPart::fromStringParsed($1, attributes(), '`', false)); }
| encaps_list { parseEncapsed($1, '`', false); $$ = $1; }
;

Expand Down Expand Up @@ -1009,7 +1008,7 @@ encaps_list:
;

encaps_string_part:
T_ENCAPSED_AND_WHITESPACE { $$ = Scalar\EncapsedStringPart[$1]; }
T_ENCAPSED_AND_WHITESPACE { $$ = Scalar\EncapsedStringPart::fromString($1, attributes()); }
;

encaps_str_varname:
Expand Down
5 changes: 2 additions & 3 deletions grammar/php7.y
Expand Up @@ -982,8 +982,7 @@ exit_expr:

backticks_expr:
/* empty */ { $$ = array(); }
| T_ENCAPSED_AND_WHITESPACE
{ $$ = array(Scalar\EncapsedStringPart[Scalar\String_::parseEscapeSequences($1, '`')]); }
| T_ENCAPSED_AND_WHITESPACE { $$ = array(Scalar\EncapsedStringPart::fromStringParsed($1, attributes(), '`')); }
| encaps_list { parseEncapsed($1, '`', true); $$ = $1; }
;

Expand Down Expand Up @@ -1173,7 +1172,7 @@ encaps_list:
;

encaps_string_part:
T_ENCAPSED_AND_WHITESPACE { $$ = Scalar\EncapsedStringPart[$1]; }
T_ENCAPSED_AND_WHITESPACE { $$ = Scalar\EncapsedStringPart::fromString($1, attributes()); }
;

encaps_str_varname:
Expand Down
21 changes: 20 additions & 1 deletion lib/PhpParser/Node/Scalar/EncapsedStringPart.php
Expand Up @@ -20,10 +20,29 @@ public function __construct(string $value, array $attributes = []) {
$this->value = $value;
}

public static function fromString(string $value, array $attributes): self
{
$attributes['rawValue'] = $value;

return new self($value, $attributes);
}

/**
* @param null|string $quote Quote type
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
*/
public static function fromStringParsed(string $value, array $attributes, $quote, bool $parseUnicodeEscape = true): self
{
$attributes['rawValue'] = $value;
$parsedValue = String_::parseEscapeSequences($value, $quote, $parseUnicodeEscape);

return new self($parsedValue, $attributes);
}

public function getSubNodeNames() : array {
return ['value'];
}

public function getType() : string {
return 'Scalar_EncapsedStringPart';
}
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/Parser/Php5.php
Expand Up @@ -2259,7 +2259,7 @@ protected function initReduceCallbacks() {
$this->semValue = array();
},
429 => function ($stackPos) {
$this->semValue = array(new Scalar\EncapsedStringPart(Scalar\String_::parseEscapeSequences($this->semStack[$stackPos-(1-1)], '`', false), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes));
$this->semValue = array(Scalar\EncapsedStringPart::fromStringParsed($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, '`', false));
},
430 => function ($stackPos) {
foreach ($this->semStack[$stackPos-(1-1)] as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) { $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, '`', false); } }; $this->semValue = $this->semStack[$stackPos-(1-1)];
Expand Down Expand Up @@ -2632,7 +2632,7 @@ protected function initReduceCallbacks() {
$this->semValue = array($this->semStack[$stackPos-(2-1)], $this->semStack[$stackPos-(2-2)]);
},
553 => function ($stackPos) {
$this->semValue = new Scalar\EncapsedStringPart($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
$this->semValue = Scalar\EncapsedStringPart::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
554 => function ($stackPos) {
$this->semValue = new Expr\Variable($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/Parser/Php7.php
Expand Up @@ -2513,7 +2513,7 @@ protected function initReduceCallbacks() {
$this->semValue = array();
},
498 => function ($stackPos) {
$this->semValue = array(new Scalar\EncapsedStringPart(Scalar\String_::parseEscapeSequences($this->semStack[$stackPos-(1-1)], '`'), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes));
$this->semValue = array(Scalar\EncapsedStringPart::fromStringParsed($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, '`'));
},
499 => function ($stackPos) {
foreach ($this->semStack[$stackPos-(1-1)] as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) { $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, '`', true); } }; $this->semValue = $this->semStack[$stackPos-(1-1)];
Expand Down Expand Up @@ -2783,7 +2783,7 @@ protected function initReduceCallbacks() {
$this->semValue = array($this->semStack[$stackPos-(2-1)], $this->semStack[$stackPos-(2-2)]);
},
587 => function ($stackPos) {
$this->semValue = new Scalar\EncapsedStringPart($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
$this->semValue = Scalar\EncapsedStringPart::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
588 => function ($stackPos) {
$this->semValue = new Expr\Variable($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
Expand Down
37 changes: 37 additions & 0 deletions test/PhpParser/Node/Scalar/EncapsedStringPartTest.php
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PhpParser\Node\Scalar;

use PhpParser\Node\Stmt\Echo_;
use PhpParser\ParserFactory;

final class EncapsedStringPartTest extends \PHPUnit\Framework\TestCase
{
public function testRawValue()
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$nodes = $parser->parse('<?php echo "$value some \\"";');

$echo = $nodes[0];
$this->assertInstanceOf(Echo_::class, $echo);

/** @var Echo_ $echoExprs */
$echoExprs = $echo->exprs;

$this->assertCount(1, $echoExprs);

$firstEchoExprs = $echoExprs[0];
$this->assertInstanceOf(Encapsed::class, $firstEchoExprs);

/** @var Encapsed $firstEchoExprs */
$this->assertCount(2, $firstEchoExprs->parts);

$secondEncapsedPart = $firstEchoExprs->parts[1];
$this->assertInstanceOf(EncapsedStringPart::class, $secondEncapsedPart);

$this->assertSame( ' some "', $secondEncapsedPart->value);
$this->assertSame(' some \\"', $secondEncapsedPart->getAttribute('rawValue'));
}
}