|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace RecursiveBuiltinFunctions; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +class Foo |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * @param array<string, int|array<string, float|array<string, bool>>> $nested |
| 14 | + * @param array<string, int> $flat |
| 15 | + * @param array<string, mixed> $mixedValues |
| 16 | + */ |
| 17 | + public function walk(array $nested, array $flat, array $mixedValues): void |
| 18 | + { |
| 19 | + array_walk_recursive($nested, function ($value, $key): void { |
| 20 | + assertType('bool|float|int', $value); |
| 21 | + assertType('string', $key); |
| 22 | + }); |
| 23 | + array_walk_recursive($flat, function ($value, $key): void { |
| 24 | + assertType('int', $value); |
| 25 | + assertType('string', $key); |
| 26 | + }); |
| 27 | + $constant = [1, [2, ['x' => 'y']]]; |
| 28 | + array_walk_recursive($constant, function ($value, $key): void { |
| 29 | + assertType("1|2|'y'", $value); |
| 30 | + assertType("0|1|'x'", $key); |
| 31 | + }); |
| 32 | + array_walk_recursive($mixedValues, function ($value): void { |
| 33 | + assertType('mixed~array', $value); |
| 34 | + }); |
| 35 | + array_walk_recursive($nested, function ($value, $key, $arg): void { |
| 36 | + assertType("'extra'", $arg); |
| 37 | + }, 'extra'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param array<string, int|array<string, int>> $nested |
| 42 | + * @param array<string, int> $flat |
| 43 | + * @param array<string, mixed> $mixedValues |
| 44 | + */ |
| 45 | + public function filter(array $nested, array $flat, array $mixedValues, mixed $mixed): void |
| 46 | + { |
| 47 | + // nested arrays are filtered at every depth and kept |
| 48 | + assertType('array<string, __recursive<array<int|string, __recursion|int>>|int>', filter_var($nested, FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY)); |
| 49 | + assertType('array<string, __recursive<array<int|string, __recursion|int>>|int>', filter_var($nested, FILTER_VALIDATE_INT, FILTER_FORCE_ARRAY)); |
| 50 | + assertType('array<string, int>', filter_var($flat, FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY)); |
| 51 | + // an array of mixed and mixed are left as they were |
| 52 | + assertType('array<string, int|false>', filter_var($mixedValues, FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY)); |
| 53 | + assertType('array<int|false>|false', filter_var($mixed, FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY)); |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments