Skip to content

Commit

Permalink
Merge pull request #700 from DanielBadura/handle-new-and-enum
Browse files Browse the repository at this point in the history
Handle reflection error in retrieving default values
  • Loading branch information
Ocramius authored Feb 11, 2023
2 parents 47df2c2 + 483cd59 commit 40956d5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 26 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ext-json": "*",
"azjezz/psl": "^2.3.1",
"composer/composer": "^2.5.1",
"nikic/php-parser": "^4.15.3",
"nikolaposa/version": "^4.1.0",
"ocramius/package-versions": "^2.7.0",
"roave/better-reflection": "^6.5.0",
Expand Down
63 changes: 42 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased;

use PhpParser\Node\Expr;
use PhpParser\PrettyPrinter\Standard;
use PhpParser\PrettyPrinterAbstract;
use Psl\Dict;
use Psl\Str;
use Roave\BackwardCompatibility\Change;
use Roave\BackwardCompatibility\Changes;
use Roave\BackwardCompatibility\Formatter\FunctionName;
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
use Roave\BetterReflection\Reflection\ReflectionFunction;
use Roave\BetterReflection\Reflection\ReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionParameter;
Expand All @@ -22,10 +26,12 @@
final class ParameterDefaultValueChanged implements FunctionBased
{
private FunctionName $formatFunction;
private PrettyPrinterAbstract $prettyPrinter;

public function __construct()
{
$this->formatFunction = new FunctionName();
$this->prettyPrinter = new Standard();
}

public function __invoke(
Expand All @@ -38,8 +44,27 @@ public function __invoke(
$changes = Changes::empty();

foreach (Dict\intersect_by_key($fromParametersWithDefaults, $toParametersWithDefaults) as $parameterIndex => $parameter) {
$defaultValueFrom = $parameter->getDefaultValue();
$defaultValueTo = $toParametersWithDefaults[$parameterIndex]->getDefaultValue();
// add default value to null to help psalm
$defaultValueFrom = null;
$defaultValueTo = null;

try {
$defaultValueFrom = $parameter->getDefaultValue();
$defaultValueTo = $toParametersWithDefaults[$parameterIndex]->getDefaultValue();
} catch (UnableToCompileNode $unableToCompileNode) {
$parameterDefaultExpression = $parameter->getDefaultValueExpression();
$toParameterDefaultExpression = $toParametersWithDefaults[$parameterIndex]->getDefaultValueExpression();

if (
$toParameterDefaultExpression instanceof Expr &&
$parameterDefaultExpression instanceof Expr &&
$this->prettyPrinter->prettyPrintExpr($toParameterDefaultExpression) === $this->prettyPrinter->prettyPrintExpr($parameterDefaultExpression)
) {
continue;
}

throw $unableToCompileNode;
}

if ($defaultValueFrom === $defaultValueTo) {
continue;
Expand Down
Loading

0 comments on commit 40956d5

Please sign in to comment.