|
8 | 8 | use Roave\BackwardCompatibility\Change;
|
9 | 9 | use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterDefaultValueChanged;
|
10 | 10 | use Roave\BetterReflection\BetterReflection;
|
| 11 | +use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode; |
11 | 12 | use Roave\BetterReflection\Reflection\ReflectionClass;
|
12 | 13 | use Roave\BetterReflection\Reflection\ReflectionFunction;
|
13 | 14 | use Roave\BetterReflection\Reflection\ReflectionMethod;
|
14 | 15 | use Roave\BetterReflection\Reflector\DefaultReflector;
|
15 | 16 | use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
|
16 | 17 | use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
|
17 | 18 | use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
|
| 19 | +use Throwable; |
18 | 20 |
|
19 | 21 | use function array_combine;
|
20 | 22 | use function array_keys;
|
@@ -46,6 +48,46 @@ public function testDiffs(
|
46 | 48 | );
|
47 | 49 | }
|
48 | 50 |
|
| 51 | + public function testDefaultValueChangeCausesUnableToCompileNodeException(): void |
| 52 | + { |
| 53 | + $originalSource = <<<'PHP' |
| 54 | + <?php |
| 55 | +
|
| 56 | + class OriginalClass { |
| 57 | + public function methodWithDefaultValue($param = SOME_CONSTANT) {} |
| 58 | + } |
| 59 | + PHP; |
| 60 | + |
| 61 | + $modifiedSource = <<<'PHP' |
| 62 | + <?php |
| 63 | +
|
| 64 | + class ModifiedClass { |
| 65 | + // Introducing a minor change that still relies on an undefined constant |
| 66 | + public function methodWithDefaultValue($param = SOME_CONSTANT + 1) {} |
| 67 | + } |
| 68 | + PHP; |
| 69 | + |
| 70 | + $astLocator = (new BetterReflection())->astLocator(); |
| 71 | + $originalReflector = new DefaultReflector(new StringSourceLocator($originalSource, $astLocator)); |
| 72 | + $modifiedReflector = new DefaultReflector(new StringSourceLocator($modifiedSource, $astLocator)); |
| 73 | + |
| 74 | + $default = ReflectionMethod::createFromName(Throwable::class, 'getMessage'); |
| 75 | + |
| 76 | + $fromMethod = $originalReflector |
| 77 | + ->reflectClass('OriginalClass') |
| 78 | + ->getMethod('methodWithDefaultValue') ?? $default; |
| 79 | + |
| 80 | + $toMethod = $modifiedReflector |
| 81 | + ->reflectClass('ModifiedClass') |
| 82 | + ->getMethod('methodWithDefaultValue') ?? $default; |
| 83 | + |
| 84 | + $checker = new ParameterDefaultValueChanged(); |
| 85 | + |
| 86 | + $this->expectException(UnableToCompileNode::class); |
| 87 | + |
| 88 | + $checker($fromMethod, $toMethod); |
| 89 | + } |
| 90 | + |
49 | 91 | /**
|
50 | 92 | * @return array<string, array{
|
51 | 93 | * 0: ReflectionMethod|ReflectionFunction,
|
|
0 commit comments