Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@

namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased;

use PhpParser\Node\Expr;
use PhpParser\PrettyPrinter\Standard;
use PhpParser\PrettyPrinterAbstract;
use Psl\Str;
use Roave\BackwardCompatibility\Change;
use Roave\BackwardCompatibility\Changes;
use Roave\BackwardCompatibility\Formatter\ReflectionPropertyName;
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
use Roave\BetterReflection\Reflection\ReflectionProperty;

use function var_export;

final class PropertyDefaultValueChanged implements PropertyBased
{
private ReflectionPropertyName $formatProperty;
private PrettyPrinterAbstract $prettyPrinter;

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

public function __invoke(ReflectionProperty $fromProperty, ReflectionProperty $toProperty): Changes
{
$fromPropertyDefaultValue = $fromProperty->getDefaultValue();
$toPropertyDefaultValue = $toProperty->getDefaultValue();
try {
$fromPropertyDefaultValue = $fromProperty->getDefaultValue();
$toPropertyDefaultValue = $toProperty->getDefaultValue();
} catch (UnableToCompileNode $unableToCompileNode) {
$fromPropertyDefaultExpression = $fromProperty->getDefaultValueExpression();
$toPropertyDefaultExpression = $toProperty->getDefaultValueExpression();

if (
$toPropertyDefaultExpression instanceof Expr &&
$fromPropertyDefaultExpression instanceof Expr &&
$this->prettyPrinter->prettyPrintExpr($toPropertyDefaultExpression) === $this->prettyPrinter->prettyPrintExpr($fromPropertyDefaultExpression)
) {
return Changes::empty();
}

throw $unableToCompileNode;
}

if ($fromPropertyDefaultValue === $toPropertyDefaultValue) {
return Changes::empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Roave\BackwardCompatibility\Change;
use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyDefaultValueChanged;
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
use Roave\BetterReflection\Reflection\ReflectionProperty;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
Expand Down Expand Up @@ -40,6 +41,93 @@ public function testDiffs(
);
}

/** @param non-empty-string $property */
#[DataProvider('propertiesWithUncompilableDefaultValueNotChanged')]
public function testUncompilableDefaultValueNotChanged(string $property): void
{
$source = <<<'PHP'
<?php

enum TheEnum: string {
case A = 'A';
}

class TheClass {
public TheEnum $enum = TheEnum::A;

public function __construct(
public TheEnum $promotedEnum = TheEnum::A,
public stdClass $promotedNewInitializer = new stdClass(),
) {}
}
PHP;

$astLocator = (new BetterReflection())->astLocator();
$fromClass = (new DefaultReflector(new StringSourceLocator($source, $astLocator)))->reflectClass('TheClass');
$toClass = (new DefaultReflector(new StringSourceLocator($source, $astLocator)))->reflectClass('TheClass');

$changes = (new PropertyDefaultValueChanged())(
TypeRestriction::object($fromClass->getProperty($property)),
TypeRestriction::object($toClass->getProperty($property)),
);

self::assertCount(0, $changes);
}

/** @return array<string, array{0: non-empty-string}> */
public static function propertiesWithUncompilableDefaultValueNotChanged(): array
{
return [
'enum' => ['enum'],
'promotedEnum' => ['promotedEnum'],
'promotedNewInitializer' => ['promotedNewInitializer'],
];
}

public function testUncompilableDefaultValueChangedCausesUnableToCompileNodeException(): void
{
$fromSource = <<<'PHP'
<?php

enum TheEnum: string {
case A = 'A';
case B = 'B';
}

class TheClass {
public function __construct(
public TheEnum $promotedEnum = TheEnum::A,
) {}
}
PHP;

$toSource = <<<'PHP'
<?php

enum TheEnum: string {
case A = 'A';
case B = 'B';
}

class TheClass {
public function __construct(
public TheEnum $promotedEnum = TheEnum::B,
) {}
}
PHP;

$astLocator = (new BetterReflection())->astLocator();
$fromClass = (new DefaultReflector(new StringSourceLocator($fromSource, $astLocator)))->reflectClass('TheClass');
$toClass = (new DefaultReflector(new StringSourceLocator($toSource, $astLocator)))->reflectClass('TheClass');

$this->expectException(UnableToCompileNode::class);

(new PropertyDefaultValueChanged())(
TypeRestriction::object($fromClass->getProperty('promotedEnum')),
TypeRestriction::object($toClass->getProperty('promotedEnum')),
);
}

/**
* @return array<string, array<int, ReflectionProperty|array<int, string>>>
* @psalm-return array<string, array{0: ReflectionProperty, 1: ReflectionProperty, 2: list<string>}>
Expand Down
Loading