From b0f103b45fd4b53d8e1ae184cba0f5649f1fd3ca Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sat, 27 Nov 2021 08:39:43 +0100 Subject: [PATCH 1/3] Add tests for native property type --- .../PropertyDocumentedTypeChangedTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php b/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php index cb0e3642..6bf139fd 100644 --- a/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php @@ -113,6 +113,13 @@ class TheClass { * @var GenericType */ public $propertyWithComplexDocblockThatCannotBeParsed; + + /** + * @var int + */ + public $propertyWithDocblockTypeHintChangeToNativeTypeHint; + + public int $propertyWithNativeTypeHintChanged; } PHP , @@ -182,6 +189,10 @@ class TheClass { * @var GenericType */ public $propertyWithComplexDocblockThatCannotBeParsed; + + public int $propertyWithDocblockTypeHintChangeToNativeTypeHint; + + public float $propertyWithNativeTypeHintChanged; } PHP , @@ -207,6 +218,8 @@ class TheClass { 'duplicatePropertyTypesBeingDeduplicatedAreNotBcBreaks' => [], 'propertyTypeBeingDuplicatedAreNotBcBreaks' => [], 'propertyWithComplexDocblockThatCannotBeParsed' => [], + 'propertyWithDocblockTypeHintChangeToNativeTypeHint' => [], + 'propertyWithNativeTypeHintChanged' => ['[BC] CHANGED: Type documentation for property TheClass#propertyWithNativeTypeHintChanged changed from int to float'], ]; return array_combine( From c48b1f5c9057661a2b7d9512e0cbe47987fb12f1 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sat, 27 Nov 2021 16:26:54 +0100 Subject: [PATCH 2/3] Switched test case --- .../PropertyBased/PropertyDocumentedTypeChangedTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php b/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php index 6bf139fd..18640a5c 100644 --- a/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php @@ -119,7 +119,10 @@ class TheClass { */ public $propertyWithDocblockTypeHintChangeToNativeTypeHint; - public int $propertyWithNativeTypeHintChanged; + /** + * @var int + */ + public $propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange; } PHP , @@ -192,7 +195,7 @@ class TheClass { public int $propertyWithDocblockTypeHintChangeToNativeTypeHint; - public float $propertyWithNativeTypeHintChanged; + public float $propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange; } PHP , @@ -219,7 +222,7 @@ class TheClass { 'propertyTypeBeingDuplicatedAreNotBcBreaks' => [], 'propertyWithComplexDocblockThatCannotBeParsed' => [], 'propertyWithDocblockTypeHintChangeToNativeTypeHint' => [], - 'propertyWithNativeTypeHintChanged' => ['[BC] CHANGED: Type documentation for property TheClass#propertyWithNativeTypeHintChanged changed from int to float'], + 'propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange' => ['[BC] CHANGED: Type documentation for property TheClass#propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange changed from int to float'], ]; return array_combine( From 31c9c4980f3018e1800ee91979bd1ea3d88cdd7b Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sat, 27 Nov 2021 17:08:18 +0100 Subject: [PATCH 3/3] Add logic for transition from docblock to native types --- .../PropertyBased/PropertyDocumentedTypeChanged.php | 10 +++++++++- .../PropertyDocumentedTypeChangedTest.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChanged.php b/src/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChanged.php index f9a5f4ff..138cc324 100644 --- a/src/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChanged.php +++ b/src/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChanged.php @@ -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 @@ -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(); diff --git a/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php b/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php index 18640a5c..a0c6b908 100644 --- a/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/PropertyBased/PropertyDocumentedTypeChangedTest.php @@ -222,7 +222,7 @@ class TheClass { 'propertyTypeBeingDuplicatedAreNotBcBreaks' => [], 'propertyWithComplexDocblockThatCannotBeParsed' => [], 'propertyWithDocblockTypeHintChangeToNativeTypeHint' => [], - 'propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange' => ['[BC] CHANGED: Type documentation for property TheClass#propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange changed from int to float'], + 'propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange' => ['[BC] CHANGED: Type documentation for property TheClass#$propertyWithDocblockTypeHintChangeToNativeTypeHintAndTypeChange changed from int to float'], ]; return array_combine(