Skip to content

Commit

Permalink
Unions are non-clonable immutable types in psalm5
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Nov 30, 2022
1 parent 5fd88ef commit f5b4aa5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/EventHandler/Fun/Pipe/PipeArgumentsProvider.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Psalm\Tests\Config\Plugin\Hook;
namespace Psl\Psalm\EventHandler\Fun\Pipe;

use Psalm\Plugin\DynamicFunctionStorage;
use Psalm\Plugin\DynamicTemplateProvider;
Expand All @@ -16,7 +16,7 @@
use function count;
use function range;

final class PipeFunctionPlugin implements DynamicFunctionStorageProviderInterface
final class PipeArgumentsProvider implements DynamicFunctionStorageProviderInterface
{
/**
* @return array<lowercase-string>
Expand All @@ -37,7 +37,7 @@ public static function getFunctionStorage(DynamicFunctionStorageProviderEvent $e

// All expected callables for pipe
$pipe_callables = array_map(
fn($callable_offset) => self::createABCallable($callable_offset, $template_provider),
static fn ($callable_offset) => self::createABCallable($callable_offset, $template_provider),
range(1, $callable_args_count)
);

Expand Down
8 changes: 4 additions & 4 deletions src/EventHandler/Iter/First/FunctionReturnTypeProvider.php
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/EventHandler/Iter/FirstKey/FunctionReturnTypeProvider.php
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/EventHandler/Iter/Last/FunctionReturnTypeProvider.php
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/EventHandler/Iter/LastKey/FunctionReturnTypeProvider.php
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/EventHandler/Type/Shape/FunctionReturnTypeProvider.php
Expand Up @@ -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;
}
Expand Down

0 comments on commit f5b4aa5

Please sign in to comment.