Skip to content

Commit 6b96629

Browse files
✨ Normalize "typed class constants" by removing them (#299)
1 parent 97e3450 commit 6b96629

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

phpunit-tests/ClassTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests;
6+
7+
class ClassTest extends RepresenterTestCase
8+
{
9+
public function testClassConstantType(): void
10+
{
11+
$this->assertSameRepresentation(
12+
<<<'CODE'
13+
<?php
14+
interface I {
15+
const string PHP = 'PHP 8.3';
16+
}
17+
18+
class Php84 implements I {
19+
const string PHP = 'PHP 8.4';
20+
}
21+
CODE,
22+
<<<'CODE'
23+
<?php
24+
interface I {
25+
const PHP = 'PHP 8.3';
26+
}
27+
28+
class Php84 implements I {
29+
const PHP = 'PHP 8.4';
30+
}
31+
CODE,
32+
);
33+
}
34+
}

src/NormalizeNodeVisitor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpParser\Node\Scalar\InterpolatedString;
2222
use PhpParser\Node\Scalar\String_;
2323
use PhpParser\Node\Stmt\Class_;
24+
use PhpParser\Node\Stmt\ClassConst;
2425
use PhpParser\Node\Stmt\ClassMethod;
2526
use PhpParser\Node\Stmt\Function_;
2627
use PhpParser\Node\Stmt\InlineHTML;
@@ -249,6 +250,14 @@ private function normalizeCastDouble(Double $node): void
249250
$node->setAttribute('kind', Double::KIND_DOUBLE);
250251
}
251252

253+
/**
254+
* TRANSFORM: remove class const type
255+
*/
256+
private function removeClassConstType(ClassConst $node): void
257+
{
258+
$node->type = null;
259+
}
260+
252261
/**
253262
* {@inheritDoc}
254263
*/
@@ -283,6 +292,8 @@ public function enterNode(Node $node)
283292
$this->replaceStaticCallName($node);
284293
} elseif ($node instanceof MethodCall) {
285294
$this->replaceMethodCallName($node);
295+
} elseif ($node instanceof ClassConst) {
296+
$this->removeClassConstType($node);
286297
}
287298

288299
return null;

0 commit comments

Comments
 (0)