Skip to content

Commit

Permalink
Introduce TCallableInterface
Browse files Browse the repository at this point in the history
`TClosure` does not implement it at this point. It's intentional, to maintain the
separation between callables and closures that our code relies on.
  • Loading branch information
weirdan committed Mar 12, 2024
1 parent 20c7889 commit c9468b6
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 26 deletions.
10 changes: 3 additions & 7 deletions src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Psalm\Type\Atomic\Scalar;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TCallableArray;
use Psalm\Type\Atomic\TCallableInterface;
use Psalm\Type\Atomic\TCallableKeyedArray;
use Psalm\Type\Atomic\TCallableObject;
use Psalm\Type\Atomic\TCallableString;
Expand Down Expand Up @@ -192,12 +192,8 @@ public static function isContainedBy(
}

if (($container_type_part instanceof TCallable
&& ($input_type_part instanceof TCallable
|| $input_type_part instanceof TCallableArray
|| $input_type_part instanceof TCallableObject
|| $input_type_part instanceof TCallableString
|| $input_type_part instanceof TCallableKeyedArray
))
&& $input_type_part instanceof TCallableInterface
)
|| ($container_type_part instanceof TClosure
&& $input_type_part instanceof TClosure)
) {
Expand Down
20 changes: 7 additions & 13 deletions src/Psalm/Internal/Type/Comparator/CallableTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TCallableArray;
use Psalm\Type\Atomic\TCallableKeyedArray;
use Psalm\Type\Atomic\TCallableObject;
use Psalm\Type\Atomic\TCallableString;
use Psalm\Type\Atomic\TCallableInterface;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TClosure;
use Psalm\Type\Atomic\TKeyedArray;
Expand All @@ -44,31 +42,27 @@
final class CallableTypeComparator
{
/**
* @param TCallable|TClosure|TCallableArray|TCallableString|TCallableKeyedArray|TCallableObject $input_type_part
* @param TClosure|TCallableInterface $input_type_part
* @param TCallable|TClosure $container_type_part
*/
public static function isContainedBy(
Codebase $codebase,
Atomic $input_type_part,
$input_type_part,
Atomic $container_type_part,
?TypeComparisonResult $atomic_comparison_result
): bool {
if ($container_type_part instanceof TClosure) {
if ($input_type_part instanceof TCallableArray
|| $input_type_part instanceof TCallableString
|| $input_type_part instanceof TCallableKeyedArray
|| $input_type_part instanceof TCallableObject
if ($input_type_part instanceof TCallableInterface
&& !$input_type_part instanceof TCallable // it has stricter checks below
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
}
if ($input_type_part instanceof TCallableArray
|| $input_type_part instanceof TCallableString
|| $input_type_part instanceof TCallableKeyedArray
|| $input_type_part instanceof TCallableObject
if ($input_type_part instanceof TCallableInterface
&& !$input_type_part instanceof TCallable // it has stricter checks below
) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @psalm-immutable
*/
final class TCallable extends Atomic
final class TCallable extends Atomic implements TCallableInterface
{
use CallableTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TCallableArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @psalm-immutable
*/
final class TCallableArray extends TNonEmptyArray
final class TCallableArray extends TNonEmptyArray implements TCallableInterface
{
/**
* @var string
Expand Down
7 changes: 7 additions & 0 deletions src/Psalm/Type/Atomic/TCallableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Psalm\Type\Atomic;

interface TCallableInterface
{
}
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TCallableKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @psalm-immutable
*/
final class TCallableKeyedArray extends TKeyedArray
final class TCallableKeyedArray extends TKeyedArray implements TCallableInterface
{
protected const NAME_ARRAY = 'callable-array';
protected const NAME_LIST = 'callable-list';
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TCallableList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @deprecated Will be removed in Psalm v6, please use TCallableKeyedArrays with is_list=true instead.
* @psalm-immutable
*/
final class TCallableList extends TNonEmptyList
final class TCallableList extends TNonEmptyList implements TCallableInterface
{
public const KEY = 'callable-list';
public function getKeyedArray(): TKeyedArray
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TCallableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @psalm-immutable
*/
final class TCallableObject extends TObject
final class TCallableObject extends TObject implements TCallableInterface
{
use HasIntersectionTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TCallableString.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @psalm-immutable
*/
final class TCallableString extends TNonFalsyString
final class TCallableString extends TNonFalsyString implements TCallableInterface
{

public function getKey(bool $include_extra = true): string
Expand Down

0 comments on commit c9468b6

Please sign in to comment.