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

array_merge() and numeric-string keys #6945

Closed
staabm opened this issue Mar 30, 2022 · 4 comments
Closed

array_merge() and numeric-string keys #6945

staabm opened this issue Mar 30, 2022 · 4 comments

Comments

@staabm
Copy link
Contributor

staabm commented Mar 30, 2022

Bug report

from phpstan/phpstan-src#1135 (comment)

there is a interessting test-case, I tried to add.

	/**
	 * @param array<numeric-string&non-empty-string, string> $params1
	 * @param array<numeric-string, string> $params2
	 */
	function foo5(array $params1, array $params2): void
	{
		$params2 = array_merge($params1, $params2);

		assertType('array<numeric-string, string>', $params2);
	}

it errors with

There was 1 failure:

1) PHPStan\Analyser\NodeScopeResolverTest::testFileAsserts with data set "C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser/data/bug-6927.php:61" ('type', 'C:\dvl\Workspace\phpstan-src-...27.php', PHPStan\Type\Constant\ConstantStringType Object (...), PHPStan\Type\ArrayType Object (...), 61)
Expected type array<numeric-string, string>, got type array<int, string> in C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser/data/bug-6927.php on line 61.
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'array<numeric-string, string>'
+'array<int, string>'

C:\dvl\Workspace\phpstan-src-staabm\src\Testing\TypeInferenceTestCase.php:97
C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser\NodeScopeResolverTest.php:846

Code snippet that reproduces the problem

https://phpstan.org/r/7188229f-71b8-4990-a977-5494b1e7d2ee

Expected output

no error

@staabm staabm changed the title array_merge() and numeric-keys array_merge() and numeric-string keys Mar 30, 2022
@staabm
Copy link
Contributor Author

staabm commented Mar 31, 2022

the root cause for this error is, that a numeric-string is getting transformed into an IntegerType in

https://github.com/phpstan/phpstan-src/blob/7b8ebd4bca14d1615f5b6180d9f89f9af208c268/src/Type/ArrayType.php#L348-L350

@ondrejmirtes
Copy link
Member

Please stop editing those issue titles.

@ondrejmirtes
Copy link
Member

This is expected behaviour - that's how arrays in PHP behave.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants