Skip to content

Commit 9d35f1c

Browse files
committed
Deprecate Double cast
With PHP's `(double)` cast being deprecated and already having only been an alias for the `(float)` cast, introduce `Float_` as a replacement for the `Double` cast.
1 parent f7c23a4 commit 9d35f1c

File tree

11 files changed

+62
-30
lines changed

11 files changed

+62
-30
lines changed

grammar/php.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ expr:
11101110
| T_DOUBLE_CAST expr
11111111
{ $attrs = attributes();
11121112
$attrs['kind'] = $this->getFloatCastKind($1);
1113-
$$ = new Expr\Cast\Double($2, $attrs); }
1113+
$$ = new Expr\Cast\Float_($2, $attrs); }
11141114
| T_STRING_CAST expr
11151115
{ $attrs = attributes();
11161116
$attrs['kind'] = $this->getStringCastKind($1);

lib/PhpParser/Node/Expr/Cast/Double.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace PhpParser\Node\Expr\Cast;
44

5-
use PhpParser\Node\Expr\Cast;
5+
require __DIR__ . '/Float_.php';
66

7-
class Double extends Cast {
8-
// For use in "kind" attribute
9-
public const KIND_DOUBLE = 1; // "double" syntax
10-
public const KIND_FLOAT = 2; // "float" syntax
11-
public const KIND_REAL = 3; // "real" syntax
12-
13-
public function getType(): string {
14-
return 'Expr_Cast_Double';
7+
if (false) {
8+
/**
9+
* For classmap-authoritative support.
10+
*
11+
* @deprecated use \PhpParser\Node\Expr\Cast\Float_ instead.
12+
*/
13+
class Double extends Float_ {
14+
public function getType(): string {
15+
return 'Expr_Cast_Double';
16+
}
1517
}
1618
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PhpParser\Node\Expr\Cast;
4+
5+
use PhpParser\Node\Expr\Cast;
6+
7+
class Float_ extends Cast {
8+
// For use in "kind" attribute
9+
public const KIND_DOUBLE = 1; // "double" syntax
10+
public const KIND_FLOAT = 2; // "float" syntax
11+
public const KIND_REAL = 3; // "real" syntax
12+
13+
public function getType(): string {
14+
return 'Expr_Cast_Float';
15+
}
16+
}
17+
18+
// @deprecated compatibility alias
19+
class_alias(Float_::class, Double::class);

lib/PhpParser/Parser/Php7.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,7 @@ protected function initReduceCallbacks(): void {
24852485
488 => static function ($self, $stackPos) {
24862486
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);
24872487
$attrs['kind'] = $self->getFloatCastKind($self->semStack[$stackPos-(2-1)]);
2488-
$self->semValue = new Expr\Cast\Double($self->semStack[$stackPos-(2-2)], $attrs);
2488+
$self->semValue = new Expr\Cast\Float_($self->semStack[$stackPos-(2-2)], $attrs);
24892489
},
24902490
489 => static function ($self, $stackPos) {
24912491
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);

lib/PhpParser/Parser/Php8.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2486,7 +2486,7 @@ protected function initReduceCallbacks(): void {
24862486
491 => static function ($self, $stackPos) {
24872487
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);
24882488
$attrs['kind'] = $self->getFloatCastKind($self->semStack[$stackPos-(2-1)]);
2489-
$self->semValue = new Expr\Cast\Double($self->semStack[$stackPos-(2-2)], $attrs);
2489+
$self->semValue = new Expr\Cast\Float_($self->semStack[$stackPos-(2-2)], $attrs);
24902490
},
24912491
492 => static function ($self, $stackPos) {
24922492
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(2-1)], $self->tokenEndStack[$stackPos]);

lib/PhpParser/ParserAbstract.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpParser\Node\Arg;
1111
use PhpParser\Node\Expr;
1212
use PhpParser\Node\Expr\Array_;
13-
use PhpParser\Node\Expr\Cast\Double;
13+
use PhpParser\Node\Expr\Cast\Float_;
1414
use PhpParser\Node\Identifier;
1515
use PhpParser\Node\InterpolatedStringPart;
1616
use PhpParser\Node\Name;
@@ -726,14 +726,14 @@ protected function getAttributesAt(int $stackPos): array {
726726
protected function getFloatCastKind(string $cast): int {
727727
$cast = strtolower($cast);
728728
if (strpos($cast, 'float') !== false) {
729-
return Double::KIND_FLOAT;
729+
return Float_::KIND_FLOAT;
730730
}
731731

732732
if (strpos($cast, 'real') !== false) {
733-
return Double::KIND_REAL;
733+
return Float_::KIND_REAL;
734734
}
735735

736-
return Double::KIND_DOUBLE;
736+
return Float_::KIND_DOUBLE;
737737
}
738738

739739
protected function getIntCastKind(string $cast): int {

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,17 +488,21 @@ protected function pExpr_Cast_Int(Cast\Int_ $node, int $precedence, int $lhsPrec
488488
return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr, $precedence, $lhsPrecedence);
489489
}
490490

491-
protected function pExpr_Cast_Double(Cast\Double $node, int $precedence, int $lhsPrecedence): string {
492-
$kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE);
493-
if ($kind === Cast\Double::KIND_DOUBLE) {
491+
protected function pExpr_Cast_Double(Cast\Float_ $node, int $precedence, int $lhsPrecedence): string {
492+
$kind = $node->getAttribute('kind', Cast\Float_::KIND_DOUBLE);
493+
if ($kind === Cast\Float_::KIND_DOUBLE) {
494494
$cast = '(double)';
495-
} elseif ($kind === Cast\Double::KIND_FLOAT) {
495+
} elseif ($kind === Cast\Float_::KIND_FLOAT) {
496496
$cast = '(float)';
497497
} else {
498-
assert($kind === Cast\Double::KIND_REAL);
498+
assert($kind === Cast\Float_::KIND_REAL);
499499
$cast = '(real)';
500500
}
501-
return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr, $precedence, $lhsPrecedence);
501+
return $this->pPrefixOp(Cast\Float_::class, $cast . ' ', $node->expr, $precedence, $lhsPrecedence);
502+
}
503+
504+
protected function pExpr_Cast_Float(Cast\Float_ $node, int $precedence, int $lhsPrecedence): string {
505+
return $this->pExpr_Cast_Double($node, $precedence, $lhsPrecedence);
502506
}
503507

504508
protected function pExpr_Cast_String(Cast\String_ $node, int $precedence, int $lhsPrecedence): string {

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
4646
Expr\UnaryMinus::class => [ 10, -1, -1],
4747
Cast\Int_::class => [ 10, -1, -1],
4848
Cast\Double::class => [ 10, -1, -1],
49+
Cast\Float_::class => [ 10, -1, -1],
4950
Cast\String_::class => [ 10, -1, -1],
5051
Cast\Array_::class => [ 10, -1, -1],
5152
Cast\Object_::class => [ 10, -1, -1],
@@ -1386,7 +1387,7 @@ protected function initializeFixupMap(): void {
13861387

13871388
$prefixOps = [
13881389
Expr\Clone_::class, Expr\BitwiseNot::class, Expr\BooleanNot::class, Expr\UnaryPlus::class, Expr\UnaryMinus::class,
1389-
Cast\Int_::class, Cast\Double::class, Cast\String_::class, Cast\Array_::class,
1390+
Cast\Int_::class, Cast\Float_::class, Cast\String_::class, Cast\Array_::class,
13901391
Cast\Object_::class, Cast\Bool_::class, Cast\Unset_::class, Expr\ErrorSuppress::class,
13911392
Expr\YieldFrom::class, Expr\Print_::class, Expr\Include_::class,
13921393
Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class,

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ parameters:
108108
count: 1
109109
path: lib/PhpParser/Node/Expr/ArrayItem.php
110110

111+
-
112+
message: '#^If condition is always false\.$#'
113+
identifier: if.alwaysFalse
114+
count: 1
115+
path: lib/PhpParser/Node/Expr/Cast/Double.php
116+
111117
-
112118
message: '#^If condition is always false\.$#'
113119
identifier: if.alwaysFalse

test/PhpParser/ParserTestAbstract.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ public static function provideTestExtraAttributes() {
173173
["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]],
174174
["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
175175
["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
176-
["(float) 5.0", ['kind' => Expr\Cast\Double::KIND_FLOAT]],
177-
["(double) 5.0", ['kind' => Expr\Cast\Double::KIND_DOUBLE]],
178-
["(real) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
179-
[" ( REAL ) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
176+
["(float) 5.0", ['kind' => Expr\Cast\Float_::KIND_FLOAT]],
177+
["(double) 5.0", ['kind' => Expr\Cast\Float_::KIND_DOUBLE]],
178+
["(real) 5.0", ['kind' => Expr\Cast\Float_::KIND_REAL]],
179+
[" ( REAL ) 5.0", ['kind' => Expr\Cast\Float_::KIND_REAL]],
180180
];
181181
}
182182

0 commit comments

Comments
 (0)