Skip to content

Commit

Permalink
Merge pull request #340 from tomasnorre/feature/support-native-proper…
Browse files Browse the repository at this point in the history
…ty-type

Add testsand implementation for property types changing from docblock-only to native type
  • Loading branch information
Ocramius committed Nov 27, 2021
2 parents d25bf3b + 31c9c49 commit e747565
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Expand Up @@ -11,6 +11,8 @@
use Roave\BackwardCompatibility\Formatter\ReflectionPropertyName;
use Roave\BetterReflection\Reflection\ReflectionProperty;

use function array_merge;

/**
* Type declarations for properties are invariant: you can't restrict the type because the consumer may
* write invalid values to it, and you cannot widen the type because the consumer may expect a specific
Expand All @@ -27,12 +29,18 @@ public function __construct()

public function __invoke(ReflectionProperty $fromProperty, ReflectionProperty $toProperty): Changes
{
$toNativeType = [];
$toType = $toProperty->getType();
if ($toType !== null) {
$toNativeType[] = $toType->getName();
}

if ($fromProperty->getDocComment() === '') {
return Changes::empty();
}

$fromTypes = Vec\sort($fromProperty->getDocBlockTypeStrings());
$toTypes = Vec\sort($toProperty->getDocBlockTypeStrings());
$toTypes = Vec\sort(array_merge($toNativeType, $toProperty->getDocBlockTypeStrings()));

if ($fromTypes === $toTypes) {
return Changes::empty();
Expand Down
Expand Up @@ -113,6 +113,16 @@ class TheClass {
* @var GenericType<T1, T2>
*/
public $propertyWithComplexDocblockThatCannotBeParsed;
/**
* @var int
*/
public $propertyWithDocblockTypeHintChangeToNativeTypeHint;
/**
* @var int
*/
public $propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange;
}
PHP
,
Expand Down Expand Up @@ -182,6 +192,10 @@ class TheClass {
* @var GenericType<T1, T2>
*/
public $propertyWithComplexDocblockThatCannotBeParsed;
public int $propertyWithDocblockTypeHintChangeToNativeTypeHint;
public float $propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange;
}
PHP
,
Expand All @@ -207,6 +221,8 @@ class TheClass {
'duplicatePropertyTypesBeingDeduplicatedAreNotBcBreaks' => [],
'propertyTypeBeingDuplicatedAreNotBcBreaks' => [],
'propertyWithComplexDocblockThatCannotBeParsed' => [],
'propertyWithDocblockTypeHintChangeToNativeTypeHint' => [],
'propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange' => ['[BC] CHANGED: Type documentation for property TheClass#$propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange changed from int to float'],
];

return array_combine(
Expand Down

0 comments on commit e747565

Please sign in to comment.