Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ expr:
| T_DOUBLE_CAST expr
{ $attrs = attributes();
$attrs['kind'] = $this->getFloatCastKind($1);
$$ = new Expr\Cast\Double($2, $attrs); }
$$ = new Expr\Cast\Float_($2, $attrs); }
| T_STRING_CAST expr
{ $attrs = attributes();
$attrs['kind'] = $this->getStringCastKind($1);
Expand Down
20 changes: 11 additions & 9 deletions lib/PhpParser/Node/Expr/Cast/Double.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace PhpParser\Node\Expr\Cast;

use PhpParser\Node\Expr\Cast;
require __DIR__ . '/Float_.php';

class Double extends Cast {
// For use in "kind" attribute
public const KIND_DOUBLE = 1; // "double" syntax
public const KIND_FLOAT = 2; // "float" syntax
public const KIND_REAL = 3; // "real" syntax

public function getType(): string {
return 'Expr_Cast_Double';
if (false) {
/**
* For classmap-authoritative support.
*
* @deprecated use \PhpParser\Node\Expr\Cast\Float_ instead.
*/
class Double extends Float_ {
public function getType(): string {
return 'Expr_Cast_Double';
}
}
}
19 changes: 19 additions & 0 deletions lib/PhpParser/Node/Expr/Cast/Float_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace PhpParser\Node\Expr\Cast;

use PhpParser\Node\Expr\Cast;

class Float_ extends Cast {
// For use in "kind" attribute
public const KIND_DOUBLE = 1; // "double" syntax
public const KIND_FLOAT = 2; // "float" syntax
public const KIND_REAL = 3; // "real" syntax

public function getType(): string {
return 'Expr_Cast_Float';
}
}

// @deprecated compatibility alias
class_alias(Float_::class, Double::class);
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2485,7 +2485,7 @@ protected function initReduceCallbacks(): void {
488 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);
$attrs['kind'] = $self->getFloatCastKind($self->semStack[$stackPos-(2-1)]);
$self->semValue = new Expr\Cast\Double($self->semStack[$stackPos-(2-2)], $attrs);
$self->semValue = new Expr\Cast\Float_($self->semStack[$stackPos-(2-2)], $attrs);
},
489 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ protected function initReduceCallbacks(): void {
491 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);
$attrs['kind'] = $self->getFloatCastKind($self->semStack[$stackPos-(2-1)]);
$self->semValue = new Expr\Cast\Double($self->semStack[$stackPos-(2-2)], $attrs);
$self->semValue = new Expr\Cast\Float_($self->semStack[$stackPos-(2-2)], $attrs);
},
492 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);
Expand Down
8 changes: 4 additions & 4 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Expr\Cast\Float_;
use PhpParser\Node\Identifier;
use PhpParser\Node\InterpolatedStringPart;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -726,14 +726,14 @@ protected function getAttributesAt(int $stackPos): array {
protected function getFloatCastKind(string $cast): int {
$cast = strtolower($cast);
if (strpos($cast, 'float') !== false) {
return Double::KIND_FLOAT;
return Float_::KIND_FLOAT;
}

if (strpos($cast, 'real') !== false) {
return Double::KIND_REAL;
return Float_::KIND_REAL;
}

return Double::KIND_DOUBLE;
return Float_::KIND_DOUBLE;
}

protected function getIntCastKind(string $cast): int {
Expand Down
16 changes: 10 additions & 6 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,21 @@ protected function pExpr_Cast_Int(Cast\Int_ $node, int $precedence, int $lhsPrec
return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr, $precedence, $lhsPrecedence);
}

protected function pExpr_Cast_Double(Cast\Double $node, int $precedence, int $lhsPrecedence): string {
$kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE);
if ($kind === Cast\Double::KIND_DOUBLE) {
protected function pExpr_Cast_Double(Cast\Float_ $node, int $precedence, int $lhsPrecedence): string {
$kind = $node->getAttribute('kind', Cast\Float_::KIND_DOUBLE);
if ($kind === Cast\Float_::KIND_DOUBLE) {
$cast = '(double)';
} elseif ($kind === Cast\Double::KIND_FLOAT) {
} elseif ($kind === Cast\Float_::KIND_FLOAT) {
$cast = '(float)';
} else {
assert($kind === Cast\Double::KIND_REAL);
assert($kind === Cast\Float_::KIND_REAL);
$cast = '(real)';
}
return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr, $precedence, $lhsPrecedence);
return $this->pPrefixOp(Cast\Float_::class, $cast . ' ', $node->expr, $precedence, $lhsPrecedence);
}

protected function pExpr_Cast_Float(Cast\Float_ $node, int $precedence, int $lhsPrecedence): string {
return $this->pExpr_Cast_Double($node, $precedence, $lhsPrecedence);
}

protected function pExpr_Cast_String(Cast\String_ $node, int $precedence, int $lhsPrecedence): string {
Expand Down
3 changes: 2 additions & 1 deletion lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
Expr\UnaryMinus::class => [ 10, -1, -1],
Cast\Int_::class => [ 10, -1, -1],
Cast\Double::class => [ 10, -1, -1],
Cast\Float_::class => [ 10, -1, -1],
Cast\String_::class => [ 10, -1, -1],
Cast\Array_::class => [ 10, -1, -1],
Cast\Object_::class => [ 10, -1, -1],
Expand Down Expand Up @@ -1386,7 +1387,7 @@ protected function initializeFixupMap(): void {

$prefixOps = [
Expr\Clone_::class, Expr\BitwiseNot::class, Expr\BooleanNot::class, Expr\UnaryPlus::class, Expr\UnaryMinus::class,
Cast\Int_::class, Cast\Double::class, Cast\String_::class, Cast\Array_::class,
Cast\Int_::class, Cast\Double::class, Cast\Float_::class, Cast\String_::class, Cast\Array_::class,
Cast\Object_::class, Cast\Bool_::class, Cast\Unset_::class, Expr\ErrorSuppress::class,
Expr\YieldFrom::class, Expr\Print_::class, Expr\Include_::class,
Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class,
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ parameters:
count: 1
path: lib/PhpParser/Node/Expr/ArrayItem.php

-
message: '#^If condition is always false\.$#'
identifier: if.alwaysFalse
count: 1
path: lib/PhpParser/Node/Expr/Cast/Double.php

-
message: '#^If condition is always false\.$#'
identifier: if.alwaysFalse
Expand Down
8 changes: 4 additions & 4 deletions test/PhpParser/ParserTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public static function provideTestExtraAttributes() {
["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]],
["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
["(float) 5.0", ['kind' => Expr\Cast\Double::KIND_FLOAT]],
["(double) 5.0", ['kind' => Expr\Cast\Double::KIND_DOUBLE]],
["(real) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
[" ( REAL ) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
["(float) 5.0", ['kind' => Expr\Cast\Float_::KIND_FLOAT]],
["(double) 5.0", ['kind' => Expr\Cast\Float_::KIND_DOUBLE]],
["(real) 5.0", ['kind' => Expr\Cast\Float_::KIND_REAL]],
[" ( REAL ) 5.0", ['kind' => Expr\Cast\Float_::KIND_REAL]],
];
}

Expand Down
6 changes: 3 additions & 3 deletions test/code/parser/expr/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ array(
)
)
3: Stmt_Expression(
expr: Expr_Cast_Double(
expr: Expr_Cast_Float(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is covered by BC?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is.

expr: Expr_Variable(
name: a
)
)
)
4: Stmt_Expression(
expr: Expr_Cast_Double(
expr: Expr_Cast_Float(
expr: Expr_Variable(
name: a
)
)
)
5: Stmt_Expression(
expr: Expr_Cast_Double(
expr: Expr_Cast_Float(
expr: Expr_Variable(
name: a
)
Expand Down