Skip to content

str_replace on array search & replace loses non-empty-string type of subject #13043

Description

@paulbalandan

Bug report

In this code:

<?php declare(strict_types = 1);

class HelloWorld
{
	private string $prefix = '';

	/** @var non-empty-string $name */
	private string $name = 'identifier';

	/** @return non-empty-string */
	public function getPrefixedName(): string
	{
		$name = $this->prefix;

		$search = str_split('()<>@');
		$replace = array_map(rawurlencode(...), $search);
		
		$name .= str_replace($search, $replace, $this->name);

		return $name;
	}
}

$name becomes a string instead of non-empty-string when the search & replace arguments are non-empty arrays.

Code snippet that reproduces the problem

https://phpstan.org/r/bd4e8c23-0ffc-4d13-8050-0780f827f2f0

Expected output

It should retain as non-empty-string

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions