|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace yii2\extensions\phpstan\tests\data\type; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\TestWith; |
| 8 | +use yii\web\HeaderCollection; |
| 9 | + |
| 10 | +use function PHPStan\Testing\assertType; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test suite for dynamic return types of {@see HeaderCollection::get()} method in Yii HTTP header scenarios. |
| 14 | + * |
| 15 | + * Validates type inference and return types for the header collection {@see HeaderCollection::get()} method, covering |
| 16 | + * scenarios with different argument combinations, default values, first parameter variations, and various header |
| 17 | + * retrieval patterns. |
| 18 | + * |
| 19 | + * These tests ensure that PHPStan correctly infers the result types for HeaderCollection lookups, including string |
| 20 | + * returns, array returns, nullable returns, and union types based on method arguments. |
| 21 | + * |
| 22 | + * Key features: |
| 23 | + * - Array return type when first parameter is `false`. |
| 24 | + * - Dynamic return type inference based on the third argument (first parameter). |
| 25 | + * - Nullable return handling based on default value argument. |
| 26 | + * - String return type when first parameter is `true`. |
| 27 | + * - Union types when first parameter is indeterminate. |
| 28 | + * |
| 29 | + * @copyright Copyright (C) 2025 Terabytesoftw. |
| 30 | + * @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License. |
| 31 | + */ |
| 32 | +final class HeaderCollectionDynamicMethodReturnType |
| 33 | +{ |
| 34 | + public function testReturnArrayWhenFirstIsFalse(): void |
| 35 | + { |
| 36 | + $headers = new HeaderCollection(); |
| 37 | + |
| 38 | + assertType('array<int, string>|null', $headers->get('Accept', null, false)); |
| 39 | + assertType('array<int, string>', $headers->get('Accept', [], false)); |
| 40 | + assertType('array<int, string>', $headers->get('Accept', 'default', false)); |
| 41 | + } |
| 42 | + |
| 43 | + public function testReturnStringWhenFirstIsTrue(): void |
| 44 | + { |
| 45 | + $headers = new HeaderCollection(); |
| 46 | + |
| 47 | + assertType('string|null', $headers->get('Content-Type')); |
| 48 | + assertType('string|null', $headers->get('Content-Type', null)); |
| 49 | + assertType('string|null', $headers->get('Content-Type', null, true)); |
| 50 | + assertType('string', $headers->get('Content-Type', 'default')); |
| 51 | + assertType('string', $headers->get('Content-Type', 'default', true)); |
| 52 | + } |
| 53 | + |
| 54 | + #[TestWith([false])] |
| 55 | + #[TestWith([true])] |
| 56 | + public function testReturnUnionWhenFirstIsIndeterminate(bool $first): void |
| 57 | + { |
| 58 | + $headers = new HeaderCollection(); |
| 59 | + |
| 60 | + assertType('array<int, string>|string|null', $headers->get('User-Agent', null, $first)); |
| 61 | + assertType('array<int, string>|string', $headers->get('User-Agent', 'default', $first)); |
| 62 | + } |
| 63 | + |
| 64 | + public function testReturnWithArrayDefaultAndFirstTrue(): void |
| 65 | + { |
| 66 | + $headers = new HeaderCollection(); |
| 67 | + |
| 68 | + // when default is array but first is `true`, still returns `string` |
| 69 | + assertType('string', $headers->get('X-Forwarded-For', ['127.0.0.1'], true)); |
| 70 | + assertType('array<int, string>', $headers->get('X-Forwarded-For', ['127.0.0.1'], false)); |
| 71 | + } |
| 72 | + |
| 73 | + public function testReturnWithBooleanFirstParameter(): void |
| 74 | + { |
| 75 | + $headers = new HeaderCollection(); |
| 76 | + |
| 77 | + // explicit `true` |
| 78 | + assertType('string|null', $headers->get('Content-Length', null, true)); |
| 79 | + assertType('string', $headers->get('Content-Length', '0', true)); |
| 80 | + |
| 81 | + // explicit `false` |
| 82 | + assertType('array<int, string>|null', $headers->get('Accept-Encoding', null, false)); |
| 83 | + assertType('array<int, string>', $headers->get('Accept-Encoding', [], false)); |
| 84 | + } |
| 85 | + |
| 86 | + public function testReturnWithComplexScenarios(): void |
| 87 | + { |
| 88 | + $headers = new HeaderCollection(); |
| 89 | + |
| 90 | + // no arguments beyond name - should default to first=`true` |
| 91 | + assertType('string|null', $headers->get('Host')); |
| 92 | + |
| 93 | + // only name and default |
| 94 | + assertType('string', $headers->get('Referer', 'http://example.com')); |
| 95 | + assertType('string|null', $headers->get('Origin', null)); |
| 96 | + |
| 97 | + // all three arguments with various combinations |
| 98 | + assertType('string', $headers->get('Accept-Language', 'en-US', true)); |
| 99 | + assertType('array<int, string>', $headers->get('Accept-Charset', ['utf-8'], false)); |
| 100 | + } |
| 101 | + |
| 102 | + #[TestWith([false])] |
| 103 | + #[TestWith([true])] |
| 104 | + public function testReturnWithDynamicBooleanFirstParameter(bool $first): void |
| 105 | + { |
| 106 | + $headers = new HeaderCollection(); |
| 107 | + |
| 108 | + assertType('array<int, string>|string|null', $headers->get('Connection', null, $first)); |
| 109 | + assertType('array<int, string>|string', $headers->get('Connection', 'keep-alive', $first)); |
| 110 | + } |
| 111 | + |
| 112 | + public function testReturnWithExplicitNullDefault(): void |
| 113 | + { |
| 114 | + $headers = new HeaderCollection(); |
| 115 | + |
| 116 | + assertType('string|null', $headers->get('Expires', null)); |
| 117 | + assertType('string|null', $headers->get('Expires', null, true)); |
| 118 | + assertType('array<int, string>|null', $headers->get('Expires', null, false)); |
| 119 | + } |
| 120 | + |
| 121 | + #[TestWith(['string-default'])] |
| 122 | + #[TestWith([null])] |
| 123 | + public function testReturnWithMixedTypeDefault(string|null $default): void |
| 124 | + { |
| 125 | + $headers = new HeaderCollection(); |
| 126 | + |
| 127 | + assertType('string|null', $headers->get('X-Request-ID', $default, true)); |
| 128 | + assertType('array<int, string>|null', $headers->get('X-Request-ID', $default, false)); |
| 129 | + } |
| 130 | + |
| 131 | + public function testReturnWithNonNullDefault(): void |
| 132 | + { |
| 133 | + $headers = new HeaderCollection(); |
| 134 | + |
| 135 | + assertType('string', $headers->get('Authorization', 'Bearer token')); |
| 136 | + assertType('string', $headers->get('Authorization', 'Bearer token', true)); |
| 137 | + assertType('array<int, string>', $headers->get('Authorization', ['Bearer token'], false)); |
| 138 | + } |
| 139 | + |
| 140 | + #[TestWith(['fallback'])] |
| 141 | + #[TestWith([null])] |
| 142 | + public function testReturnWithNullableDefault(string|null $default): void |
| 143 | + { |
| 144 | + $headers = new HeaderCollection(); |
| 145 | + |
| 146 | + assertType('string|null', $headers->get('X-Custom-Header', $default)); |
| 147 | + assertType('string|null', $headers->get('X-Custom-Header', $default, true)); |
| 148 | + assertType('array<int, string>|null', $headers->get('X-Custom-Header', $default, false)); |
| 149 | + } |
| 150 | + |
| 151 | + public function testReturnWithVariableDefault(): void |
| 152 | + { |
| 153 | + $headers = new HeaderCollection(); |
| 154 | + |
| 155 | + $defaultValue = 'fallback-value'; |
| 156 | + |
| 157 | + assertType('string', $headers->get('Cache-Control', $defaultValue)); |
| 158 | + assertType('string', $headers->get('Cache-Control', $defaultValue, true)); |
| 159 | + assertType('array<int, string>', $headers->get('Cache-Control', $defaultValue, false)); |
| 160 | + } |
| 161 | +} |
0 commit comments