-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcode.php
38 lines (30 loc) · 1.17 KB
/
code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
namespace EnforceIteratorToArrayPreserveKeysRule;
/**
* Isolate the generator, so that PHPStan is not aware of the keys.
*/
function passThru(\Generator $iterable): \Generator {
yield from $iterable;
}
$objectAsKey = function () {
yield new \stdClass => 1;
};
$dataLoss = function () {
yield 1 => 1;
yield 1 => 2;
};
$noKeys = function () {
yield 1;
yield 2;
};
iterator_to_array(passThru($objectAsKey())); // error: Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss.
iterator_to_array(passThru($dataLoss())); // error: Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss.
iterator_to_array(passThru($noKeys())); // error: Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss.
iterator_to_array(passThru($objectAsKey()), false);
iterator_to_array(passThru($dataLoss()), false);
iterator_to_array(passThru($noKeys()), true);
iterator_to_array(... [passThru($noKeys()), true]);
iterator_to_array(
preserve_keys: true,
iterator: passThru($noKeys())
);