Skip to content

2.x: Fix negation types for Inspector #860

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

Merged
merged 2 commits into from
Apr 15, 2025
Merged

Conversation

Niklan
Copy link
Contributor

@Niklan Niklan commented Apr 14, 2025

As discussed in Slack: https://drupal.slack.com/archives/C033S2JUMLJ/p1744383402099899?thread_ts=1744246451.941499&cid=C033S2JUMLJ, the current Inspector implementation doesn't handle negation at all.

For example:

$array = mixed_function();
assertType('mixed', $array);
Inspector::assertAll(fn (array $i): bool => TRUE, $array);
assertType('iterable', $array);

After passing Inspector::assertAll(fn (array $i): bool => TRUE, $array); line, PHPStan would think it is always iterable, even if it is not passed ::assertAll() and the result was FALSE. This works fine with assert(), but breaks under other conditions, for example:

$array = mixed_function();
assertType('mixed', $array);
if (!Inspector::assertAll(fn (array $i): bool => TRUE, $array)) {
    assertType('mixed~iterable', $array);
}

This leads to a test failure:

--- Expected
+++ Actual
@@ @@
-'mixed~iterable'
+'iterable'

PHPStan thinks it is iterable, but if it's not passed the assertion, it is anything else but not iterable (mixed~iterable).

It looks like PHPStan is using the ~ symbol for type negation, like mixed~string, which reads as: This type is anything (mixed), but definitely not string.

This PR fixes the issue, and previous tests pass properly as before. This PR is required for #858 because it is not possible to provide proper test cases for it unless negation is supported.

@Niklan Niklan changed the title Fix negation types for Inspector 2.x: Fix negation types for Inspector Apr 14, 2025
@Niklan Niklan marked this pull request as draft April 14, 2025 17:36
@Niklan Niklan marked this pull request as ready for review April 15, 2025 04:15
@mglaman mglaman merged commit 7b02221 into mglaman:main Apr 15, 2025
13 of 14 checks passed
@Niklan Niklan deleted the improve-inspector branch April 15, 2025 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants