Skip to content

Commit

Permalink
[Php72] Handle crash on CreateFunctionToAnonymousFunctionRector on qu…
Browse files Browse the repository at this point in the history
…oted variable arg concat (#3300)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 22, 2023
1 parent d2a553c commit 93b225a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;

class QuotedVariableArgConcat
{
public function pk_gen()
{
$argL = func_get_args();
$strFunc = array_shift($argL);
$strArgs = join(', ',$argL);

return create_function('$v', 'return '.$strFunc.'($v'.$strArgs.');');
}
}

?>
-----
<?php

namespace Rector\Tests\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;

class QuotedVariableArgConcat
{
public function pk_gen()
{
$argL = func_get_args();
$strFunc = array_shift($argL);
$strArgs = join(', ',$argL);

return function ($v) use ($strFunc, $strArgs) {
return $strFunc($v . $strArgs);
};
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;

class QuotedVariableArgConcat2
{
public function pk_gen()
{
$argL = func_get_args();
$strFunc = array_shift($argL);
$strArgs = join(', ',$argL);

return create_function('$v', 'return '.$strFunc.'('.$strArgs.'$v);');
}
}

?>
-----
<?php

namespace Rector\Tests\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;

class QuotedVariableArgConcat2
{
public function pk_gen()
{
$argL = func_get_args();
$strFunc = array_shift($argL);
$strArgs = join(', ',$argL);

return function ($v) use ($strFunc, $strArgs) {
return $strFunc($strArgs . $v);
};
}
}

?>
31 changes: 25 additions & 6 deletions src/PhpParser/Parser/InlineCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Nette\Utils\Strings;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;

final class InlineCodeParser
Expand Down Expand Up @@ -113,14 +115,31 @@ public function stringify(Expr $expr): string
}

if ($expr instanceof Concat) {
$string = $this->stringify($expr->left) . $this->stringify($expr->right);
return Strings::replace(
$string,
self::VARIABLE_IN_SINGLE_QUOTED_REGEX,
static fn (array $match) => $match['variable']
);
return $this->resolveConcatValue($expr);
}

return $this->nodePrinter->print($expr);
}

private function resolveConcatValue(Concat $concat): string
{
if ($concat->left instanceof Concat &&
$concat->right instanceof String_ && str_starts_with($concat->right->value, '$')) {
$concat->right->value = '.' . $concat->right->value;
}

if ($concat->right instanceof String_ && str_starts_with($concat->right->value, '($')) {
$node = $concat->getAttribute(AttributeKey::NEXT_NODE);
if ($node instanceof Variable) {
$concat->right->value .= '.';
}
}

$string = $this->stringify($concat->left) . $this->stringify($concat->right);
return Strings::replace(
$string,
self::VARIABLE_IN_SINGLE_QUOTED_REGEX,
static fn (array $match) => $match['variable']
);
}
}

0 comments on commit 93b225a

Please sign in to comment.