Skip to content

Commit 14f802a

Browse files
czoselmreiden
andauthored
feat: add support for PHP 8.3 typed class constants (#2433)
* Support PHP 8.3 typed constants. Depends on Kenneth-Sills/php-parser#1 * chore: bump php-parser * chore: add tests for PHP 8.3 typed class constants --------- Co-authored-by: Mark Reidenbach <mark@digitalmanagementteam.com>
1 parent 54532a3 commit 14f802a

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"dependencies": {
2929
"linguist-languages": "^8.0.0",
30-
"php-parser": "^3.1.5"
30+
"php-parser": "^3.2.4"
3131
},
3232
"devDependencies": {
3333
"@babel/preset-env": "^7.27.2",

src/options.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default {
2323
{ value: "8.0" },
2424
{ value: "8.1" },
2525
{ value: "8.2" },
26+
{ value: "8.3" },
2627
],
2728
},
2829
trailingCommaPHP: {

src/printer.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,7 @@ function printNode(path, options, print) {
23082308
node.final ? "final " : "",
23092309
node.visibility ? [node.visibility, " "] : "",
23102310
"const",
2311+
node.type ? [node.nullable ? " ?" : " ", print("type")] : "",
23112312
firstVariable ? [" ", firstVariable] : "",
23122313
indent(printed.slice(1).map((p) => [",", hardline, p])),
23132314
]);

tests/class/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`anonymous.php 1`] = `
44
====================================options=====================================
@@ -918,6 +918,31 @@ readonly class ReadOnlyCls {
918918
919919
class FinalCost { final public const FOO = 'foo'; }
920920
921+
// PHP 8.3 typed class constants
922+
enum E {
923+
const string TEST = "Test1"; // E::TEST is a string
924+
}
925+
926+
trait T {
927+
const string TEST = E::TEST; // T::TEST is a string too
928+
}
929+
930+
interface I {
931+
const string TEST = E::TEST; // I::TEST is a string as well
932+
}
933+
934+
class Foo implements I {
935+
use T;
936+
937+
const string TEST = E::TEST; // Foo::TEST must also be a string
938+
}
939+
940+
class Bar extends Foo {
941+
const string TEST = "Test2"; // Bar::TEST must also be a string, but the value can change
942+
public const int|null I = null;
943+
public const ?int J = null;
944+
}
945+
921946
=====================================output=====================================
922947
<?php
923948
@@ -1368,5 +1393,35 @@ class FinalCost
13681393
final public const FOO = "foo";
13691394
}
13701395
1396+
// PHP 8.3 typed class constants
1397+
enum E
1398+
{
1399+
const string TEST = "Test1"; // E::TEST is a string
1400+
}
1401+
1402+
trait T
1403+
{
1404+
const string TEST = E::TEST; // T::TEST is a string too
1405+
}
1406+
1407+
interface I
1408+
{
1409+
const string TEST = E::TEST; // I::TEST is a string as well
1410+
}
1411+
1412+
class Foo implements I
1413+
{
1414+
use T;
1415+
1416+
const string TEST = E::TEST; // Foo::TEST must also be a string
1417+
}
1418+
1419+
class Bar extends Foo
1420+
{
1421+
const string TEST = "Test2"; // Bar::TEST must also be a string, but the value can change
1422+
public const int|null I = null;
1423+
public const ?int J = null;
1424+
}
1425+
13711426
================================================================================
13721427
`;

tests/class/class.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,28 @@ public function __construct( public int $a, private float $b, protected stri
314314
}
315315

316316
class FinalCost { final public const FOO = 'foo'; }
317+
318+
// PHP 8.3 typed class constants
319+
enum E {
320+
const string TEST = "Test1"; // E::TEST is a string
321+
}
322+
323+
trait T {
324+
const string TEST = E::TEST; // T::TEST is a string too
325+
}
326+
327+
interface I {
328+
const string TEST = E::TEST; // I::TEST is a string as well
329+
}
330+
331+
class Foo implements I {
332+
use T;
333+
334+
const string TEST = E::TEST; // Foo::TEST must also be a string
335+
}
336+
337+
class Bar extends Foo {
338+
const string TEST = "Test2"; // Bar::TEST must also be a string, but the value can change
339+
public const int|null I = null;
340+
public const ?int J = null;
341+
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,10 +4701,10 @@ path-type@^4.0.0:
47014701
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
47024702
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
47034703

4704-
php-parser@^3.1.5:
4705-
version "3.2.1"
4706-
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.2.1.tgz#960916dc03e4979a59435f9dd9eb03f2e86d8126"
4707-
integrity sha512-WT5AMqe39ZdqAxp9Yp7uR6e3clBWlT1dxHHs49GmnDx2d+975NEiLSVy2tRGLdSC9tgdQOLiN1Yz54g1d2cZDQ==
4704+
php-parser@^3.2.4:
4705+
version "3.2.4"
4706+
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.2.4.tgz#56f97438a46c54ce0e78a8357782402f9530f892"
4707+
integrity sha512-X1HIaSHCb9aAReEqb+U/AAB3evE1iKD6tAY/lw9+wT4koCkWhg22m4LNtFGwd3Qv4PYIL2+zl+oXh00ALKya0Q==
47084708

47094709
picocolors@^1.0.0:
47104710
version "1.0.0"

0 commit comments

Comments
 (0)