|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use CzProject\PhpSimpleAst; |
| 6 | +use Tester\Assert; |
| 7 | + |
| 8 | +require __DIR__ . '/../../bootstrap.php'; |
| 9 | + |
| 10 | + |
| 11 | +test('nullable parameter fixer', function () { |
| 12 | + $reflection = PhpSimpleAst\Reflection\FilesReflection::scanFile(Fixtures::path('Refactoring/NullableParameterFixer.php')); |
| 13 | + $classReflection = $reflection->getClass(\Foo\TestClass::class); |
| 14 | + |
| 15 | + PhpSimpleAst\Refactor\NullableParameterFixer::processClass($classReflection); |
| 16 | + |
| 17 | + $method = $classReflection->getMethod('methodWithNullableParams'); |
| 18 | + $parameters = $method->getParameters(); |
| 19 | + |
| 20 | + // param1: string $param1 = null should become nullable |
| 21 | + Assert::true($parameters['param1']->isNullable()); |
| 22 | + |
| 23 | + // param2: int $param2 = 42 should NOT become nullable (default is not null) |
| 24 | + Assert::false($parameters['param2']->isNullable()); |
| 25 | + |
| 26 | + // param3: array $param3 = null should become nullable |
| 27 | + Assert::true($parameters['param3']->isNullable()); |
| 28 | + |
| 29 | + // param4: $param4 = null should NOT be affected (no type declaration) |
| 30 | + Assert::false($parameters['param4']->isNullable()); |
| 31 | + |
| 32 | + // Test method without defaults - should not be affected |
| 33 | + $method2 = $classReflection->getMethod('methodWithoutDefaults'); |
| 34 | + $parameters2 = $method2->getParameters(); |
| 35 | + Assert::false($parameters2['param1']->isNullable()); |
| 36 | + Assert::false($parameters2['param2']->isNullable()); |
| 37 | + |
| 38 | + // Test method with already nullable types - should remain unchanged |
| 39 | + $method3 = $classReflection->getMethod('methodWithNullableTypes'); |
| 40 | + $parameters3 = $method3->getParameters(); |
| 41 | + Assert::true($parameters3['param1']->isNullable()); |
| 42 | + Assert::true($parameters3['param2']->isNullable()); |
| 43 | + |
| 44 | + Assert::same( |
| 45 | + Fixtures::load('Refactoring/NullableParameterFixer.result'), |
| 46 | + $reflection->getFiles()[0]->toString() |
| 47 | + ); |
| 48 | +}); |
0 commit comments