|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug15404; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertNativeType; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +/** @param array{projects?: list<string>} $s */ |
| 9 | +function a(array $s): void |
| 10 | +{ |
| 11 | + $has = isset($s['projects']); |
| 12 | + if (!$has) { |
| 13 | + return; |
| 14 | + } |
| 15 | + foreach ($s['projects'] as $p) { |
| 16 | + assertType('string', $p); |
| 17 | + assertNativeType('mixed', $p); |
| 18 | + if (!is_string($p)) { |
| 19 | + throw new \InvalidArgumentException(); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/** @param array{projects?: list<string>} $s */ |
| 25 | +function inlineIsset(array $s): void |
| 26 | +{ |
| 27 | + if (!isset($s['projects'])) { |
| 28 | + return; |
| 29 | + } |
| 30 | + foreach ($s['projects'] as $p) { |
| 31 | + assertType('string', $p); |
| 32 | + assertNativeType('mixed', $p); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** @param array{projects?: list<string>} $s */ |
| 37 | +function arrayKeyExists(array $s): void |
| 38 | +{ |
| 39 | + $has = array_key_exists('projects', $s); |
| 40 | + if (!$has) { |
| 41 | + return; |
| 42 | + } |
| 43 | + foreach ($s['projects'] as $p) { |
| 44 | + assertType('string', $p); |
| 45 | + assertNativeType('mixed', $p); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** @param array{projects?: list<string>} $s */ |
| 50 | +function notEmptyArray(array $s): void |
| 51 | +{ |
| 52 | + $has = isset($s['projects']); |
| 53 | + if (!$has) { |
| 54 | + return; |
| 55 | + } |
| 56 | + if ($s['projects'] !== []) { |
| 57 | + assertType('non-empty-list<string>', $s['projects']); |
| 58 | + assertNativeType('mixed~array{}', $s['projects']); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** @param array{projects?: list<string>} $s */ |
| 63 | +function isArrayCheck(array $s): void |
| 64 | +{ |
| 65 | + $has = isset($s['projects']); |
| 66 | + if (!$has) { |
| 67 | + return; |
| 68 | + } |
| 69 | + if (is_array($s['projects'])) { |
| 70 | + assertType('list<string>', $s['projects']); |
| 71 | + assertNativeType('array<mixed, mixed>', $s['projects']); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** @param array{projects?: list<string>} $s */ |
| 76 | +function notIsArrayCheck(array $s): void |
| 77 | +{ |
| 78 | + $has = isset($s['projects']); |
| 79 | + if (!$has) { |
| 80 | + return; |
| 81 | + } |
| 82 | + if (!is_int($s['projects'])) { |
| 83 | + assertType('list<string>', $s['projects']); |
| 84 | + assertNativeType('mixed~int', $s['projects']); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class Foo |
| 89 | +{ |
| 90 | + |
| 91 | + /** @var list<string>|null */ |
| 92 | + public $projects; |
| 93 | + |
| 94 | + public function doFoo(): void |
| 95 | + { |
| 96 | + $has = $this->projects !== null; |
| 97 | + if (!$has) { |
| 98 | + return; |
| 99 | + } |
| 100 | + foreach ($this->projects as $p) { |
| 101 | + assertType('string', $p); |
| 102 | + assertNativeType('mixed', $p); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments