Skip to content

Commit

Permalink
add support for nikic/php-parser v5
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Nov 17, 2024
1 parent 9aceaf3 commit d1377b5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^8.1",
"nette/utils": "^3.2 || ^4.0",
"nikic/php-parser": "^4.15",
"nikic/php-parser": "^4.15 || ^5.0",
"phpstan/phpdoc-parser": "^1.18.1"
},
"require-dev": {
Expand Down
15 changes: 7 additions & 8 deletions src/Compiler/Php/PhpCodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Throw_ as ThrowExpr_;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
Expand All @@ -44,7 +44,6 @@
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\Use_;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
Expand Down Expand Up @@ -113,7 +112,7 @@ class PhpCodeBuilder extends BuilderFactory
private array $variables = [];

/**
* @param array<?ArrayItem> $items
* @param array<ArrayItem> $items
*/
public function array(array $items): Array_
{
Expand Down Expand Up @@ -269,19 +268,19 @@ public function plus(Expr $var, Expr $value): Plus
/**
* @param array<int|string, scalar|array<mixed>|Expr|Arg|null> $args
*/
public function throwNew(string $className, array $args): Throw_
public function throwNew(string $className, array $args): Stmt
{
return $this->throw($this->new($className, $args));
}

public function throw(Expr $expr): Throw_
public function throw(Expr $expr): Stmt
{
return new Throw_($expr);
return new Expression($this->throwExpr($expr));
}

public function throwExpr(Expr $expr): ThrowExpr_
public function throwExpr(Expr $expr): Throw_
{
return new ThrowExpr_($expr);
return new Throw_($expr);
}

public function assign(Expr $var, Expr $expr): Expression
Expand Down
15 changes: 9 additions & 6 deletions src/Compiler/Php/PhpCodePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\PrettyPrinter\Standard;
use function addcslashes;
use function count;

/**
Expand All @@ -20,12 +21,9 @@
class PhpCodePrinter extends Standard
{

/**
* @param array<string, mixed> $options
*/
public function __construct(array $options = [])
public function __construct()
{
parent::__construct($options + ['shortArraySyntax' => true]);
parent::__construct(['shortArraySyntax' => true]);
}

/**
Expand Down Expand Up @@ -118,7 +116,12 @@ protected function pExpr_New(New_ $node): string
return 'new ' . $this->pClassCommon($node->class, $argsFormatted);
}

return 'new ' . $this->pNewVariable($node->class) . $argsFormatted;
return 'new ' . $this->pNewOperand($node->class) . $argsFormatted;
}

protected function pSingleQuotedString(string $string): string
{
return '\'' . addcslashes($string, '\'\\') . '\'';
}

}
3 changes: 1 addition & 2 deletions src/Compiler/Validator/Object/AssertDateTimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use DateTimeZone;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
Expand Down Expand Up @@ -101,7 +100,7 @@ private function throwException(
Expr $value,
Expr $path,
PhpCodeBuilder $builder,
): Throw_
): Stmt
{
if ($this->timezone !== null) {
$boundaryValue .= " (in {$this->timezone} timezone)";
Expand Down

0 comments on commit d1377b5

Please sign in to comment.