Type Inference Issue with Named Parameters in PHPUnit Assertions Leading to Mixed Type Error in PHPStan

Open
#198 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
devtools, testing

Research direction

Start by reproducing the PHPStan error with the shown request data and PHPUnit assertions, comparing the positional and named-parameter forms of assertIsString. Trace how the assertion affects the inferred type of $data['result']['data']['id']; done means the named-parameter form preserves a string type for Uuid::fromString() without the mixed-type error.

Written by the indexing model from the issue text.

Description

Hello,

I've encountered a type inference issue when using PHPStan with PHPUnit assertions on a piece of code that utilizes named parameters.

Given the following code snippet:

$data = $this->requestAndGetData(method: 'segment.create', parameters: [ 'name' => 'Test segment' ]);

self::assertArrayHasKey(key: 'id', array: $data['result']['data']);
self::assertIsString($data['result']['data']['id']);

$segmentId = $data['result']['data']['id'];
$segmentUuid = Symfony\Component\Uid\Uuid::fromString(uuid: $segmentId);

The package operates correctly, and I am confident that the $segmentId variable is a string. However, after modifying the assertIsString assertion to use a named parameter:

self::assertIsString(actual: $data['result']['data']['id']);

I start receiving the following error from PHPStan:

Parameter $uuid of static method Symfony\Component\Uid\Uuid::fromString() expects string, mixed given.

This issue does not occur when the named parameter is not used in the assertion.

Thank you for your time and assistance.

Dominant language
PHP
Stars
544
Forks
58
Avg merge
2d 1h
Merged PRs (30d)
5

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-phpunit

All issues in phpstan/phpstan-phpunit

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.