-
Notifications
You must be signed in to change notification settings - Fork 663
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
Psalm does not understand variable initialization order when initialized within array. #6061
Labels
Comments
I found these snippets: https://psalm.dev/r/d4185292b9<?php
[
$timestamp = (new DateTimeImmutable('2017-08-06 00:00:00'))->getTimestamp() => [
'timestamp' => $timestamp,
],
$timestamp = (new DateTimeImmutable('2017-08-06 01:00:00'))->getTimestamp() => [
'timestamp' => $timestamp,
],
$timestamp = (new DateTimeImmutable('2017-08-06 02:00:00'))->getTimestamp() => [
'timestamp' => $timestamp,
]
];
|
Few comments:
|
I found these snippets: https://psalm.dev/r/366293c0e1<?php
$dates = ['2017-08-06 00:00:00', '2017-08-06 01:00:00', '2017-08-06 02:00:00'];
$arr = [];
foreach ($dates as $date) {
$timestamp = (new DateTimeImmutable($date))->getTimestamp();
$arr[$timestamp] = ['timestamp' => $timestamp];
}
|
Definitely agree the code is not perfect though it's still valid or is it not? Stumbled upon it in legacy code base where there's complex array structure in tests and the lowest level array is constructed like this. Hard to squeeze foreach inside. I would have to wrap it in a function, call it and unpack [22=>[
$timestamp = (new DateTimeImmutable('2017-08-06 00:00:00'))->getTimestamp() => [
'timestamp' => $timestamp,
],
$timestamp = (new DateTimeImmutable('2017-08-06 01:00:00'))->getTimestamp() => [
'timestamp' => $timestamp,
],
$timestamp = (new DateTimeImmutable('2017-08-06 02:00:00'))->getTimestamp() => [
'timestamp' => $timestamp,
]
]]; -> <?php
[22=>(function() {
$dates = ['2017-08-06 00:00:00', '2017-08-06 01:00:00', '2017-08-06 02:00:00'];
$arr = [];
foreach ($dates as $date) {
$timestamp = (new DateTimeImmutable($date))->getTimestamp();
$arr[$timestamp] = ['timestamp' => $timestamp];
}
return $arr;
})()] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://psalm.dev/r/d4185292b9
I'd expect not errors about variable
$timestamp
being not found or unused.The text was updated successfully, but these errors were encountered: