Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/main/php/lang/ast/emit/PHP85.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace lang\ast\emit;

use lang\ast\types\{
IsArray,
IsFunction,
IsGeneric,
IsIntersection,
IsLiteral,
IsMap,
IsNullable,
IsUnion,
IsValue
};

/**
* PHP 8.5 syntax
*
* @test lang.ast.unittest.emit.PHP85Test
* @see https://wiki.php.net/rfc#php_85
*/
class PHP85 extends PHP {
use RewriteBlockLambdaExpressions;

public $targetVersion= 80500;

/** Sets up type => 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; }
];
}
}
26 changes: 26 additions & 0 deletions src/test/php/lang/ast/unittest/emit/PHP85Test.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php namespace lang\ast\unittest\emit;

use lang\ast\emit\Type;
use test\{Assert, Test};

class PHP85Test extends EmittingTest {

/** @return string */
protected function runtime() { return 'php:8.5.0'; }

#[Test]
public function pipe_operator() {
Assert::equals(
'"test"|>strtoupper(...);',
$this->emit('"test" |> strtoupper(...);')
);
}

#[Test]
public function nullsafepipe_operator() {
Assert::equals(
'null===($_0="test")?null:$_0|>strtoupper(...);',
$this->emit('"test" ?|> strtoupper(...);')
);
}
}
Loading