Skip to content

Commit 62a0917

Browse files
committed
✨ Normalize boolean, integer and binary cast. Upgrade indirect dependencies.
1 parent 852e762 commit 62a0917

File tree

3 files changed

+135
-33
lines changed

3 files changed

+135
-33
lines changed

composer.lock

Lines changed: 50 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit-tests/CastTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,55 @@ public function testRealCast(): void
3939

4040
$this->assertSameRepresentation($codeA, $codeB);
4141
}
42+
43+
public function testBooleanCast(): void
44+
{
45+
$codeA = <<<'CODE'
46+
<?php
47+
48+
$a = (boolean) true;
49+
CODE;
50+
51+
$codeB = <<<'CODE'
52+
<?php
53+
54+
$a = (bool) true;
55+
CODE;
56+
57+
$this->assertSameRepresentation($codeA, $codeB);
58+
}
59+
60+
public function testIntegerCast(): void
61+
{
62+
$codeA = <<<'CODE'
63+
<?php
64+
65+
$a = (integer) true;
66+
CODE;
67+
68+
$codeB = <<<'CODE'
69+
<?php
70+
71+
$a = (int) true;
72+
CODE;
73+
74+
$this->assertSameRepresentation($codeA, $codeB);
75+
}
76+
77+
public function testBinaryCast(): void
78+
{
79+
$codeA = <<<'CODE'
80+
<?php
81+
82+
$a = (binary) true;
83+
CODE;
84+
85+
$codeB = <<<'CODE'
86+
<?php
87+
88+
$a = (string) true;
89+
CODE;
90+
91+
$this->assertSameRepresentation($codeA, $codeB);
92+
}
4293
}

src/NormalizeNodeVisitor.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use PhpParser\Node;
99
use PhpParser\Node\Expr\Array_;
1010
use PhpParser\Node\Expr\BinaryOp\Concat;
11+
use PhpParser\Node\Expr\Cast\Bool_;
1112
use PhpParser\Node\Expr\Cast\Double;
13+
use PhpParser\Node\Expr\Cast\Int_;
14+
use PhpParser\Node\Expr\Cast\String_ as CastString_;
1215
use PhpParser\Node\Expr\Exit_;
1316
use PhpParser\Node\Expr\FuncCall;
1417
use PhpParser\Node\Expr\MethodCall;
@@ -243,13 +246,37 @@ private function normalizeExit(Exit_ $node): void
243246
}
244247

245248
/**
246-
* TRANSFORM: `(double)`, `(float)` and `(real)` are all aliases for `(double)`
249+
* TRANSFORM: `(double)` and `(real)` are aliases of the `(float)` cast
247250
*/
248251
private function normalizeCastDouble(Double $node): void
249252
{
250253
$node->setAttribute('kind', Double::KIND_DOUBLE);
251254
}
252255

256+
/**
257+
* TRANSFORM: `(boolean)` is an alias of the `(bool)` cast
258+
*/
259+
private function normalizeCastBool(Bool_ $node): void
260+
{
261+
$node->setAttribute('kind', Bool_::KIND_BOOL);
262+
}
263+
264+
/**
265+
* TRANSFORM: `(integer)` is an alias of the `(int)` cast
266+
*/
267+
private function normalizeCastInt(Int_ $node): void
268+
{
269+
$node->setAttribute('kind', Int_::KIND_INT);
270+
}
271+
272+
/**
273+
* TRANSFORM: `(binary)` is an alias of the `(string)` cast
274+
*/
275+
private function normalizeCastString(CastString_ $node): void
276+
{
277+
$node->setAttribute('kind', CastString_::KIND_STRING);
278+
}
279+
253280
/**
254281
* TRANSFORM: remove class const type
255282
*/
@@ -286,6 +313,12 @@ public function enterNode(Node $node)
286313
$this->normalizeExit($node);
287314
} elseif ($node instanceof Double) {
288315
$this->normalizeCastDouble($node);
316+
} elseif ($node instanceof Bool_) {
317+
$this->normalizeCastBool($node);
318+
} elseif ($node instanceof Int_) {
319+
$this->normalizeCastInt($node);
320+
} elseif ($node instanceof CastString_) {
321+
$this->normalizeCastString($node);
289322
} elseif ($node instanceof ClassMethod) {
290323
$this->replaceMethodName($node);
291324
} elseif ($node instanceof StaticCall) {

0 commit comments

Comments
 (0)