Skip to content

Commit 57a6ec5

Browse files
committed
Cover the UnableToCompileNode exception throw
1 parent 7e77f0a commit 57a6ec5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/DetectChanges/BCBreak/FunctionBased/ParameterDefaultValueChanged.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function __invoke(
6363
continue;
6464
}
6565

66-
/** @infection-ignore-all */
6766
throw $unableToCompileNode;
6867
}
6968

test/unit/DetectChanges/BCBreak/FunctionBased/ParameterDefaultValueChangedTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
use Roave\BackwardCompatibility\Change;
99
use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterDefaultValueChanged;
1010
use Roave\BetterReflection\BetterReflection;
11+
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
1112
use Roave\BetterReflection\Reflection\ReflectionClass;
1213
use Roave\BetterReflection\Reflection\ReflectionFunction;
1314
use Roave\BetterReflection\Reflection\ReflectionMethod;
1415
use Roave\BetterReflection\Reflector\DefaultReflector;
1516
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
1617
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
1718
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
19+
use Throwable;
1820

1921
use function array_combine;
2022
use function array_keys;
@@ -46,6 +48,46 @@ public function testDiffs(
4648
);
4749
}
4850

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+
4991
/**
5092
* @return array<string, array{
5193
* 0: ReflectionMethod|ReflectionFunction,

0 commit comments

Comments
 (0)