From e37e9e13a3bd6fa7b1fea688ceeec4ad43895534 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Jun 2025 11:41:13 +0200 Subject: [PATCH] Add PHP 8.5 emitter --- src/main/php/lang/ast/emit/PHP85.class.php | 57 +++++++++++++++++++ .../ast/unittest/emit/PHP85Test.class.php | 26 +++++++++ 2 files changed, 83 insertions(+) create mode 100755 src/main/php/lang/ast/emit/PHP85.class.php create mode 100755 src/test/php/lang/ast/unittest/emit/PHP85Test.class.php diff --git a/src/main/php/lang/ast/emit/PHP85.class.php b/src/main/php/lang/ast/emit/PHP85.class.php new file mode 100755 index 00000000..4bb3e5e9 --- /dev/null +++ b/src/main/php/lang/ast/emit/PHP85.class.php @@ -0,0 +1,57 @@ + literal mappings */ + public function __construct() { + $this->literals= [ + IsArray::class => function($t) { return 'array'; }, + IsMap::class => function($t) { return 'array'; }, + IsFunction::class => function($t) { return 'callable'; }, + IsValue::class => function($t) { return $t->literal(); }, + IsNullable::class => function($t) { + if (null === ($l= $this->literal($t->element))) return null; + return $t->element instanceof IsUnion ? $l.'|null' : '?'.$l; + }, + IsIntersection::class => function($t) { + $i= ''; + foreach ($t->components as $component) { + if (null === ($l= $this->literal($component))) return null; + $i.= '&'.$l; + } + return substr($i, 1); + }, + IsUnion::class => function($t) { + $u= ''; + foreach ($t->components as $component) { + if (null === ($l= $this->literal($component))) return null; + $u.= '|'.$l; + } + return substr($u, 1); + }, + IsLiteral::class => function($t) { return $t->literal(); }, + IsGeneric::class => function($t) { return null; } + ]; + } +} \ No newline at end of file diff --git a/src/test/php/lang/ast/unittest/emit/PHP85Test.class.php b/src/test/php/lang/ast/unittest/emit/PHP85Test.class.php new file mode 100755 index 00000000..5010b7e3 --- /dev/null +++ b/src/test/php/lang/ast/unittest/emit/PHP85Test.class.php @@ -0,0 +1,26 @@ +strtoupper(...);', + $this->emit('"test" |> strtoupper(...);') + ); + } + + #[Test] + public function nullsafepipe_operator() { + Assert::equals( + 'null===($_0="test")?null:$_0|>strtoupper(...);', + $this->emit('"test" ?|> strtoupper(...);') + ); + } +} \ No newline at end of file