Unions are non-clonable immutable types in psalm5

This commit is contained in:
Toon Verwerft 2022-11-30 15:03:08 +01:00
parent 2aa6c21bba
commit cab0731170
8 changed files with 23 additions and 25 deletions

View File

@ -51,10 +51,11 @@ and correct type of `array{name: string, age: int, location?: array{city: string
## Compatibility ## Compatibility
| PSL | Psalm plugin | | PSL | Psalm plugin | Psalm |
|-----|--------------| |-----|------------|-------|
| 2.x | 2.x | | 2.x | ^2.1 | v5 |
| 1.x | 1.x | | 2.x | ~2.0.0 | v4 |
| 1.x | 1.x | v4 |
## Sponsors ## Sponsors

View File

@ -11,7 +11,7 @@
], ],
"require": { "require": {
"php": "^8.1", "php": "^8.1",
"vimeo/psalm": "^4.20 || ^5.0" "vimeo/psalm": "^5.0"
}, },
"conflict": { "conflict": {
"azjezz/psl": "<2.0" "azjezz/psl": "<2.0"

View File

@ -34,20 +34,20 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
} }
if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { 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) { 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) { if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
// TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ). // TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ).
// //
// foreach ($properties as $property) { // foreach ($properties as $property) {
// return clone $property; // return $property;
// } // }
return clone $array_argument_type->getGenericValueType(); return $array_argument_type->getGenericValueType();
} }
return null; return null;

View File

@ -34,7 +34,7 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
} }
if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { 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) { if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) {
@ -45,9 +45,9 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
// TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ). // TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ).
// //
// foreach ($properties as $property) { // foreach ($properties as $property) {
// return clone $property; // return $property;
// } // }
return clone $array_argument_type->getGenericKeyType(); return $array_argument_type->getGenericKeyType();
} }
return null; return null;

View File

@ -34,11 +34,11 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
} }
if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { 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) { 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) { if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
@ -50,8 +50,8 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
// $last_property = $property; // $last_property = $property;
// } // }
// //
// return clone $last_property; // return $last_property;
return clone $array_argument_type->getGenericValueType(); return $array_argument_type->getGenericValueType();
} }
return null; return null;

View File

@ -34,7 +34,7 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
} }
if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) { 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) { if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) {
@ -50,8 +50,8 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
// $last_property = $property; // $last_property = $property;
// } // }
// //
// return clone $last_property; // return $last_property;
return clone $array_argument_type->getGenericKeyType(); return $array_argument_type->getGenericKeyType();
} }
return null; return null;

View File

@ -28,9 +28,6 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
return null; return null;
} }
$clone = clone $argument_type; return $argument_type->setPossiblyUndefined(true);
$clone->possibly_undefined = true;
return $clone;
} }
} }

View File

@ -57,8 +57,8 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
return null; return null;
} }
$property_type = clone $type->type_params[0]; $property_type = $type->type_params[0];
$property_type->possibly_undefined = $value->possibly_undefined; $property_type = $property_type->setPossiblyUndefined($value->possibly_undefined);
$properties[$name] = $property_type; $properties[$name] = $property_type;
} }