|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\PHPStan\Rules\Fixtures; |
| 10 | + |
| 11 | +final class RequireClosureReturnTypeFixture |
| 12 | +{ |
| 13 | + public function closureWithoutReturnType(): void |
| 14 | + { |
| 15 | + // Error: Closure without return type |
| 16 | + $closure = static function ($x) { |
| 17 | + return $x * 2; |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + public function closureWithReturnType(): void |
| 22 | + { |
| 23 | + // OK: Closure has return type |
| 24 | + $closure = static function (int $x): int { |
| 25 | + return $x * 2; |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + public function arrowFunctionWithoutReturnType(): void |
| 30 | + { |
| 31 | + // Error: Arrow function without return type |
| 32 | + $arrow = static fn ($x) => $x * 2; |
| 33 | + } |
| 34 | + |
| 35 | + public function arrowFunctionWithReturnType(): void |
| 36 | + { |
| 37 | + // OK: Arrow function has return type |
| 38 | + $arrow = static fn (int $x): int => $x * 2; |
| 39 | + } |
| 40 | + |
| 41 | + public function closureWithVoidReturnType(): void |
| 42 | + { |
| 43 | + // OK: Closure has void return type |
| 44 | + $closure = static function (): void { |
| 45 | + echo 'Hello'; |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + public function arrowFunctionWithMixedReturnType(): void |
| 50 | + { |
| 51 | + // OK: Arrow function has mixed return type |
| 52 | + $arrow = static fn ($x): mixed => $x; |
| 53 | + } |
| 54 | + |
| 55 | + public function nestedClosuresWithoutReturnType(): void |
| 56 | + { |
| 57 | + // Error: Outer closure without return type |
| 58 | + $outer = static function () { |
| 59 | + // Error: Inner closure without return type |
| 60 | + return static function ($x) { |
| 61 | + return $x * 2; |
| 62 | + }; |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + public function arrayMapWithoutReturnType(): void |
| 67 | + { |
| 68 | + // Error: Closure without return type |
| 69 | + $result = array_map(static function ($x) { |
| 70 | + return $x * 2; |
| 71 | + }, [1, 2, 3]); |
| 72 | + } |
| 73 | +} |
0 commit comments