Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callback types for array_uintersect etc #3282

Open
wants to merge 6 commits into
base: 1.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Type/TypehintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ public static function decideType(
}

if (
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
&& $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes()
($type->isCallable()->yes() && $phpDocType->isCallable()->yes())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the problem encountered in the original PR (at least when rebased on the current 1.11.x branch). $type is callable(mixed, mixed): int and TemplateTypeHelper::resolveToBounds($phpDocType) is callable(array-key, array-key): int. $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType)) returns maybe (IMO it should return no, but I didn't look into that).

As I understand it the goal of decideType is to allow phpdocs to narrow down the native type (or in case of the native PHP code reflection also the signature/stub/jetbrains stuff...). The narrow-down rule probably works fine in most cases, but for callables, chances are that the $type may be incorrectly narrowed down too much.

I'm not sure about the consequences of this change. I'd feel better if it could only be applied to native PHP reflections, not parsed user code.

|| (
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
&& $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes()
)
) {
$resultType = $phpDocType;
} else {
Expand Down
153 changes: 148 additions & 5 deletions stubs/arrayFunctions.stub
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,163 @@ function uksort(array &$array, callable $callback): bool
}

/**
* @template T of mixed
* @template TV of mixed
* @template TK of mixed
*
* @param array<T> $one
* @param array<T> $two
* @param callable(T, T): int $three
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TV, TV): int $three
* @return array<TK, TV>
*/
function array_udiff(
array $one,
array $two,
callable $three
): int {}
): array {}

/**
* @param array<array-key, mixed> $value
* @return ($value is __always-list ? true : false)
*/
function array_is_list(array $value): bool {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TK, TK): int $three
* @return array<TK, TV>
*/
function array_diff_uassoc(
array $one,
array $two,
callable $three
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TK, TK): int $three
* @return array<TK, TV>
*/
function array_diff_ukey(
array $one,
array $two,
callable $three
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TK, TK): int $three
* @return array<TK, TV>
*/
function array_intersect_uassoc(
array $one,
array $two,
callable $three
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TK, TK): int $three
*
* @return array<TK, TV>
*/
function array_intersect_ukey(
array $one,
array $two,
callable $three
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TV, TV): int $three
*
* @return array<TK, TV>
*/
function array_udiff_assoc(
array $one,
array $two,
callable $three
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TV, TV): int $three
* @param callable(TK, TK): int $four
* @return array<TK, TV>
*/
function array_udiff_uassoc(
array $one,
array $two,
callable $three,
callable $four
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TV, TV): int $three
* @return array<TK, TV>
*/
function array_uintersect_assoc(
array $one,
array $two,
callable $three,
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TV, TV): int $three
* @param callable(TK, TK): int $four
* @return array<TK, TV>
*/
function array_uintersect_uassoc(
array $one,
array $two,
callable $three,
callable $four
): array {}

/**
* @template TK of array-key
* @template TV of mixed
*
* @param array<TK, TV> $one
* @param array<TK, TV> $two
* @param callable(TV, TV): int $three
* @return array<TK, TV>
*/
function array_uintersect(
array $one,
array $two,
callable $three,
): array {}
127 changes: 127 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array_diff_intersect_callbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php declare(strict_types = 1);

namespace ArrayDiffIntersectCallbacks;

use function PHPStan\Testing\assertType;

array_diff_uassoc(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('0|1', $a);
assertType('0|1', $b);

return 0;
},
);

array_diff_ukey(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('0|1', $a);
assertType('0|1', $b);

return 0;
},
);

array_intersect_uassoc(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('0|1', $a);
assertType('0|1', $b);

return 0;
},
);

array_intersect_ukey(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('0|1', $a);
assertType('0|1', $b);

return 0;
},
);

array_udiff(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('1|2|3|4', $a);
assertType('1|2|3|4', $b);

return 0;
},
);

array_udiff_assoc(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('1|2|3|4', $a);
assertType('1|2|3|4', $b);

return 0;
},
);

array_udiff_uassoc(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('1|2|3|4', $a);
assertType('1|2|3|4', $b);

return 0;
},
static function ($a, $b) {
assertType('0|1', $a);
assertType('0|1', $b);

return 0;
},
);

array_uintersect(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('1|2|3|4', $a);
assertType('1|2|3|4', $b);

return 0;
},
);

array_uintersect_assoc(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('1|2|3|4', $a);
assertType('1|2|3|4', $b);

return 0;
},
);

array_uintersect_uassoc(
[1, 2],
[3, 4],
static function ($a, $b) {
assertType('1|2|3|4', $a);
assertType('1|2|3|4', $b);

return 0;
},
static function ($a, $b) {
assertType('0|1', $a);
assertType('0|1', $b);

return 0;
},
);
Loading
Loading