From cab0731170a07e5b893312d8ae70a4e93caa5f31 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Wed, 30 Nov 2022 15:03:08 +0100 Subject: [PATCH] Unions are non-clonable immutable types in psalm5 --- README.md | 9 +++++---- composer.json | 2 +- .../Iter/First/FunctionReturnTypeProvider.php | 8 ++++---- .../Iter/FirstKey/FunctionReturnTypeProvider.php | 6 +++--- .../Iter/Last/FunctionReturnTypeProvider.php | 8 ++++---- .../Iter/LastKey/FunctionReturnTypeProvider.php | 6 +++--- .../Type/Optional/FunctionReturnTypeProvider.php | 5 +---- .../Type/Shape/FunctionReturnTypeProvider.php | 4 ++-- 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0558deb..8cf856f 100644 --- a/README.md +++ b/README.md @@ -51,10 +51,11 @@ and correct type of `array{name: string, age: int, location?: array{city: string ## Compatibility -| PSL | Psalm plugin | -|-----|--------------| -| 2.x | 2.x | -| 1.x | 1.x | +| PSL | Psalm plugin | Psalm | +|-----|------------|-------| +| 2.x | ^2.1 | v5 | +| 2.x | ~2.0.0 | v4 | +| 1.x | 1.x | v4 | ## Sponsors diff --git a/composer.json b/composer.json index f60727c..be89fe4 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": "^8.1", - "vimeo/psalm": "^4.20 || ^5.0" + "vimeo/psalm": "^5.0" }, "conflict": { "azjezz/psl": "<2.0" diff --git a/src/EventHandler/Iter/First/FunctionReturnTypeProvider.php b/src/EventHandler/Iter/First/FunctionReturnTypeProvider.php index 7168824..dfd0a8a 100644 --- a/src/EventHandler/Iter/First/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Iter/First/FunctionReturnTypeProvider.php @@ -34,20 +34,20 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev } if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { - return clone $array_argument_type->type_params[1]; + return $array_argument_type->type_params[1]; } if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) { - return clone $array_argument_type->type_param; + return $array_argument_type->type_param; } if ($array_argument_type instanceof Type\Atomic\TKeyedArray) { // TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ). // // foreach ($properties as $property) { - // return clone $property; + // return $property; // } - return clone $array_argument_type->getGenericValueType(); + return $array_argument_type->getGenericValueType(); } return null; diff --git a/src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php b/src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php index f95ebfd..d276046 100644 --- a/src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php @@ -34,7 +34,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev } if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { - return clone $array_argument_type->type_params[0]; + return $array_argument_type->type_params[0]; } if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) { @@ -45,9 +45,9 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev // TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ). // // foreach ($properties as $property) { - // return clone $property; + // return $property; // } - return clone $array_argument_type->getGenericKeyType(); + return $array_argument_type->getGenericKeyType(); } return null; diff --git a/src/EventHandler/Iter/Last/FunctionReturnTypeProvider.php b/src/EventHandler/Iter/Last/FunctionReturnTypeProvider.php index 6a7a8c8..5f79bb7 100644 --- a/src/EventHandler/Iter/Last/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Iter/Last/FunctionReturnTypeProvider.php @@ -34,11 +34,11 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev } if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { - return clone $array_argument_type->type_params[1]; + return $array_argument_type->type_params[1]; } if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) { - return clone $array_argument_type->type_param; + return $array_argument_type->type_param; } if ($array_argument_type instanceof Type\Atomic\TKeyedArray) { @@ -50,8 +50,8 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev // $last_property = $property; // } // - // return clone $last_property; - return clone $array_argument_type->getGenericValueType(); + // return $last_property; + return $array_argument_type->getGenericValueType(); } return null; diff --git a/src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php b/src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php index 7e98609..19abf16 100644 --- a/src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php @@ -34,7 +34,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev } if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { - return clone $array_argument_type->type_params[0]; + return $array_argument_type->type_params[0]; } if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) { @@ -50,8 +50,8 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev // $last_property = $property; // } // - // return clone $last_property; - return clone $array_argument_type->getGenericKeyType(); + // return $last_property; + return $array_argument_type->getGenericKeyType(); } return null; diff --git a/src/EventHandler/Type/Optional/FunctionReturnTypeProvider.php b/src/EventHandler/Type/Optional/FunctionReturnTypeProvider.php index 2c0fbb3..896f434 100644 --- a/src/EventHandler/Type/Optional/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Type/Optional/FunctionReturnTypeProvider.php @@ -28,9 +28,6 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev return null; } - $clone = clone $argument_type; - $clone->possibly_undefined = true; - - return $clone; + return $argument_type->setPossiblyUndefined(true); } } diff --git a/src/EventHandler/Type/Shape/FunctionReturnTypeProvider.php b/src/EventHandler/Type/Shape/FunctionReturnTypeProvider.php index 3650a1a..ea45db6 100644 --- a/src/EventHandler/Type/Shape/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Type/Shape/FunctionReturnTypeProvider.php @@ -57,8 +57,8 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev return null; } - $property_type = clone $type->type_params[0]; - $property_type->possibly_undefined = $value->possibly_undefined; + $property_type = $type->type_params[0]; + $property_type = $property_type->setPossiblyUndefined($value->possibly_undefined); $properties[$name] = $property_type; }