1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<?php
// Fake
use Some\ClassName as ClassAlias;
test('union types', function () {
$f1 = fn (): string|int|false|Bar|null => 1;
$e1 = 'fn (): string|int|false|\Bar|null => 1';
$f2 = fn (): \Foo|\Bar => 1;
$e2 = 'fn (): \Foo|\Bar => 1';
$f3 = fn (): int|false => false;
$e3 = 'fn (): int|false => false';
$f4 = function (): null|MyClass|ClassAlias|Relative\Ns\ClassName|\Absolute\Ns\ClassName {
return null;
};
$e4 = 'function (): null|\MyClass|\Some\ClassName|\Relative\Ns\ClassName|\Absolute\Ns\ClassName {
return null;
}';
expect($f1)->toBeCode($e1);
expect($f2)->toBeCode($e2);
expect($f3)->toBeCode($e3);
expect($f4)->toBeCode($e4);
});
test('mixed type', function () {
$f1 = function (): mixed {
return 42;
};
$e1 = 'function (): mixed {
return 42;
}';
expect($f1)->toBeCode($e1);
});
test('null safe operator with methods', function () {
$f1 = function () {
$obj = new \stdClass();
return $obj?->invalid();
};
$e1 = 'function () {
$obj = new \stdClass();
return $obj?->invalid();
}';
expect($f1)->toBeCode($e1);
});
test('null safe operator with properties', function () {
$f1 = function () {
$obj = new \stdClass();
return $obj?->invalid;
};
$e1 = 'function () {
$obj = new \stdClass();
return $obj?->invalid;
}';
expect($f1)->toBeCode($e1);
});
test('trailing comma', function () {
$f1 = function (string $param, ) {
};
$e1 = 'function (string $param, ) {
}';
expect($f1)->toBeCode($e1);
});
test('named arguments', function () {
$f1 = function (string $firstName, string $lastName) {
return $firstName.' '.$lastName;
};
$e1 = "function (string \$firstName, string \$lastName) {
return \$firstName.' '.\$lastName;
}";
expect($f1)->toBeCode($e1);
})->with('serializers');
test('single named argument within closures', function () {
$f1 = function () {
return (new ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string');
};
$e1 = "function () {
return (new \ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string');
}";
expect($f1)->toBeCode($e1);
})->with('serializers');
test('multiple named arguments within closures', function () {
$f1 = function () {
return (new ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string', namedArgumentB: 1);
};
$e1 = "function () {
return (new \ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string', namedArgumentB: 1);
}";
expect($f1)->toBeCode($e1);
})->with('serializers');
test('multiple named arguments within nested closures', function () {
$f1 = function () {
$fn = fn ($namedArgument, $namedArgumentB) => (
new ReflectionClosurePhp80NamedArguments
)->publicMethod(namedArgument: $namedArgument, namedArgumentB: $namedArgumentB);
return $fn(namedArgument: 'string', namedArgumentB: 1);
};
$e1 = "function () {
\$fn = fn (\$namedArgument, \$namedArgumentB) => (
new \ReflectionClosurePhp80NamedArguments
)->publicMethod(namedArgument: \$namedArgument, namedArgumentB: \$namedArgumentB);
return \$fn(namedArgument: 'string', namedArgumentB: 1);
}";
expect($f1)->toBeCode($e1);
})->with('serializers');
class ReflectionClosurePhp80NamedArguments
{
public function publicMethod(string $namedArgument, $namedArgumentB = null)
{
return $namedArgument.(string) $namedArgumentB;
}
}
class PropertyPromotion
{
public function __construct(
public string $public,
protected string $protected,
private string $private,
) {
}
public function getProtected(): string
{
return $this->protected;
}
public function getPrivate(): string
{
return $this->private;
}
}
|