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

Complex array shape not working properly #6364

Closed
sakarikl opened this issue Jan 14, 2022 · 7 comments
Closed

Complex array shape not working properly #6364

sakarikl opened this issue Jan 14, 2022 · 7 comments

Comments

@sakarikl
Copy link

Bug report

Complex array shape doesn't work with switch that uses different shapes

<?php declare(strict_types = 1);

/**
 * @phpstan-var array<string,
 *     array{
 *          type: 'Type1',
 *          id: string,
 *          jobs: array<string, string>
 *     } | array{
 *          type: 'Type2',
 *          id: string,
 *          job?: string,
 *          extractor?: int
 *     } | array{
 *          type: 'Type3',
 *          id: string,
 *          jobs: array<string, int>
 *     } | array{
 *          type: 'Type4',
 *          id: string,
 *          job?: string
 *     }>
 */
$array = [
	'id1' => [
		'type' => 'Type1',
		'id' => 'subid1',
		'jobs' => [
			'key' => 'value',
			'key2' => 'value2',
		],
	],
	'id2' => [
		'type' => 'Type2',
		'id' => 'subid2',
		'job' => 'value',
	],
	'id3' => [
		'type' => 'Type2',
		'id' => 'subid2',
	],	
	'id4' => [
		'type' => 'Type3',
		'id' => 'subid4',
		'jobs' => [
			'key1' => 1,
			'key2' => 2,			
		],
	],	
	'id5' => [
		'type' => 'Type4',
		'id' => 'subid5',
		'job' => 'value',
	],
	'id6' => [
		'type' => 'Type4',
		'id' => 'subid6',
	],	
];

foreach ($array as $key => $data) {	
	switch ($data['type']) {
		case 'Type1':
			echo $data['id'];
			print_r($data['jobs']);			
			break;
		case 'Type3':
			$jobs = [];
			foreach ($data['jobs'] as $job => $extractor) {
				echo $job;
				echo $extractor;
			}			
			break;
		case 'Type2':
			echo $data['id'];
			echo $data['job'] ?? 'default';
			echo $data['extractor'] ?? 0;			
			break;
		case 'Type4':
			echo $data['id'];
			echo $data['job'] ?? 'default';			
			break;
		default:
			throw new \RuntimeException('unknown type: ' . $data['type']);
	}
}

https://phpstan.org/r/a80de5c7-cec9-4a55-a3cc-059dd6e5bf60

Expected output

Expected no errors

@ondrejmirtes
Copy link
Member

Needs @template-covariant: https://phpstan.org/r/5edec49b-84fe-4f3a-9ee9-49c5d5160ce1

Explained here: #5780 (comment)

@canvural
Copy link
Contributor

@ondrejmirtes Wrong issue 😄

@ondrejmirtes ondrejmirtes reopened this Jan 14, 2022
@ondrejmirtes
Copy link
Member

Thanks :D

@phpstan-bot
Copy link
Contributor

@sakarikl After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-65: Offset 'jobs' does not exist on array{type: 'Type1'|'Type3', id: string, jobs: array<string, int|string>}|array{type: 'Type2'|'Type4', id: string, job?: string, extractor?: int}.
-69: Offset 'jobs' does not exist on array{type: 'Type1'|'Type3', id: string, jobs: array<string, int|string>}|array{type: 'Type2'|'Type4', id: string, job?: string, extractor?: int}.
+65: Offset 'jobs' does not exist on array{type: 'Type1', id: string, job?: string, extractor?: int}|array{type: 'Type1', id: string, jobs?: array<string, int|string>}.
+69: Offset 'jobs' does not exist on array{type: 'Type3', id: string, job?: string, extractor?: int}|array{type: 'Type3', id: string, jobs: array<string, int|string>}.
Full report
Line Error
65 `Offset 'jobs' does not exist on array{type: 'Type1', id: string, job?: string, extractor?: int}
69 `Offset 'jobs' does not exist on array{type: 'Type3', id: string, job?: string, extractor?: int}

@phpstan-bot
Copy link
Contributor

@sakarikl After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

@@ @@
-65: Offset 'jobs' does not exist on array{type: 'Type1'|'Type3', id: string, jobs: array<string, int|string>}|array{type: 'Type2'|'Type4', id: string, job?: string, extractor?: int}.
-69: Offset 'jobs' does not exist on array{type: 'Type1'|'Type3', id: string, jobs: array<string, int|string>}|array{type: 'Type2'|'Type4', id: string, job?: string, extractor?: int}.
+65: Offset 'jobs' does not exist on array{type: 'Type1', id: string, job?: string, extractor?: int}|array{type: 'Type1', id: string, jobs: array<string, int|string>}.
+69: Offset 'jobs' does not exist on array{type: 'Type3', id: string, job?: string, extractor?: int}|array{type: 'Type3', id: string, jobs: array<string, int|string>}.
Full report
Line Error
65 `Offset 'jobs' does not exist on array{type: 'Type1', id: string, job?: string, extractor?: int}
69 `Offset 'jobs' does not exist on array{type: 'Type3', id: string, job?: string, extractor?: int}

@phpstan-bot
Copy link
Contributor

@sakarikl After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-65: Offset 'jobs' does not exist on array{type: 'Type1'|'Type3', id: string, jobs: array<string, int|string>}|array{type: 'Type2'|'Type4', id: string, job?: string, extractor?: int}.
-69: Offset 'jobs' does not exist on array{type: 'Type1'|'Type3', id: string, jobs: array<string, int|string>}|array{type: 'Type2'|'Type4', id: string, job?: string, extractor?: int}.
+No errors

@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 Aug 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants