123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- // Fake
- use Some\ClassName as ClassAlias;
- use Tests\Fixtures\RegularClass;
- 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);
- });
- 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);
- });
- 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);
- });
- test('named arguments with switch cases and instanceof', function () {
- $f1 = function ($a) {
- switch (true) {
- case (new RegularClass(a2: $a))->a2 instanceof RegularClass:
- return (new RegularClass(a2: $a))->a2;
- default:
- return new RegularClass(a2: RegularClass::C);
- }
- };
- $e1 = 'function ($a) {
- switch (true) {
- case (new \Tests\Fixtures\RegularClass(a2: $a))->a2 instanceof \Tests\Fixtures\RegularClass:
- return (new \Tests\Fixtures\RegularClass(a2: $a))->a2;
- default:
- return new \Tests\Fixtures\RegularClass(a2: \Tests\Fixtures\RegularClass::C);
- }
- }';
- expect($f1)->toBeCode($e1);
- });
- 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;
- }
- }
- function reflection_closure_php_80_switch_statement_test_is_two($a)
- {
- return $a === 2;
- }
- class ReflectionClosurePhp80InstanceOfTest
- {
- };
- class ReflectionClosurePhp80SwitchStatementTest
- {
- }
- test('instanceof', function () {
- $f1 = function (object $a): array {
- $b = $a instanceof DateTime || $a instanceof ReflectionClosurePhp80InstanceOfTest || $a instanceof RegularClass;
- return [
- $b,
- ($a instanceof DateTime || $a instanceof ReflectionClosurePhp80InstanceOfTest || $a instanceof RegularClass),
- (function (object $a): bool {
- return ($a instanceof DateTime || $a instanceof ReflectionClosurePhp80InstanceOfTest || $a instanceof RegularClass) === true;
- })(a: $a),
- ];
- };
- $e1 = 'function (object $a): array {
- $b = $a instanceof \DateTime || $a instanceof \ReflectionClosurePhp80InstanceOfTest || $a instanceof \Tests\Fixtures\RegularClass;
- return [
- $b,
- ($a instanceof \DateTime || $a instanceof \ReflectionClosurePhp80InstanceOfTest || $a instanceof \Tests\Fixtures\RegularClass),
- (function (object $a): bool {
- return ($a instanceof \DateTime || $a instanceof \ReflectionClosurePhp80InstanceOfTest || $a instanceof \Tests\Fixtures\RegularClass) === true;
- })(a: $a),
- ];
- }';
- expect($f1)->toBeCode($e1);
- });
- test('switch statement', function () {
- $f1 = function ($a) {
- switch (true) {
- case $a === 1:
- return 'one';
- case reflection_closure_php_80_switch_statement_test_is_two(a: $a):
- return 'two';
- case ReflectionClosurePhp80SwitchStatementTest::isThree(a: $a):
- return 'three';
- case (new ReflectionClosurePhp80SwitchStatementTest)->isFour(a: $a):
- return 'four';
- case ($a instanceof ReflectionClosurePhp80SwitchStatementTest):
- return 'five';
- case ($a instanceof DateTime):
- return 'six';
- case ($a instanceof RegularClass):
- return 'seven';
- default:
- return 'other';
- }
- };
- $e1 = 'function ($a) {
- switch (true) {
- case $a === 1:
- return \'one\';
- case \reflection_closure_php_80_switch_statement_test_is_two(a: $a):
- return \'two\';
- case \ReflectionClosurePhp80SwitchStatementTest::isThree(a: $a):
- return \'three\';
- case (new \ReflectionClosurePhp80SwitchStatementTest)->isFour(a: $a):
- return \'four\';
- case ($a instanceof \ReflectionClosurePhp80SwitchStatementTest):
- return \'five\';
- case ($a instanceof \DateTime):
- return \'six\';
- case ($a instanceof \Tests\Fixtures\RegularClass):
- return \'seven\';
- default:
- return \'other\';
- }
- }';
- expect($f1)->toBeCode($e1);
- });
- function reflection_closure_php_80_match_statement_test_is_two($a)
- {
- return $a === 2;
- }
- class ReflectionClosurePhp80MatchStatementTest
- {
- public static function isThree($a)
- {
- return $a === 3;
- }
- public function isFour($a)
- {
- return $a === 4;
- }
- }
- test('match statement', function () {
- $f1 = function ($a) {
- return match (true) {
- $a === 1 => 'one',
- reflection_closure_php_80_match_statement_test_is_two($a) => 'two',
- ReflectionClosurePhp80MatchStatementTest::isThree(a: $a) => 'three',
- (new ReflectionClosurePhp80MatchStatementTest)->isFour($a) => 'four',
- $a instanceof ReflectionClosurePhp80MatchStatementTest => 'five',
- $a instanceof DateTime => 'six',
- $a instanceof RegularClass => 'seven',
- default => 'other',
- };
- };
- $e1 = 'function ($a) {
- return match (true) {
- $a === 1 => \'one\',
- \reflection_closure_php_80_match_statement_test_is_two($a) => \'two\',
- \ReflectionClosurePhp80MatchStatementTest::isThree(a: $a) => \'three\',
- (new \ReflectionClosurePhp80MatchStatementTest)->isFour($a) => \'four\',
- $a instanceof \ReflectionClosurePhp80MatchStatementTest => \'five\',
- $a instanceof \DateTime => \'six\',
- $a instanceof \Tests\Fixtures\RegularClass => \'seven\',
- default => \'other\',
- };
- }';
- expect($f1)->toBeCode($e1);
- });
|