|
5 | 5 | use PHPStan\Php\PhpVersion; |
6 | 6 | use PHPStan\Rules\Properties\PropertyReflectionFinder; |
7 | 7 | use PHPStan\Rules\Rule; |
| 8 | +use PHPStan\Rules\RuleLevelHelper; |
8 | 9 | use PHPStan\Testing\RuleTestCase; |
9 | 10 | use PHPUnit\Framework\Attributes\RequiresPhp; |
10 | 11 | use function array_merge; |
|
16 | 17 | class UnsetRuleTest extends RuleTestCase |
17 | 18 | { |
18 | 19 |
|
| 20 | + private bool $reportMaybes = false; |
| 21 | + |
| 22 | + private bool $unsetOffsetOnMaybeAccessible = false; |
| 23 | + |
19 | 24 | protected function getRule(): Rule |
20 | 25 | { |
21 | 26 | $container = self::getContainer(); |
22 | 27 | return new UnsetRule( |
23 | 28 | $container->getByType(PropertyReflectionFinder::class), |
24 | 29 | $container->getByType(PhpVersion::class), |
| 30 | + $container->getByType(RuleLevelHelper::class), |
| 31 | + $this->reportMaybes, |
| 32 | + $this->unsetOffsetOnMaybeAccessible, |
25 | 33 | ); |
26 | 34 | } |
27 | 35 |
|
@@ -229,4 +237,52 @@ public function testNullCoalesceAssignRightSideScope(): void |
229 | 237 | $this->analyse([__DIR__ . '/data/null-coalesce-assign-right-side-scope.php'], []); |
230 | 238 | } |
231 | 239 |
|
| 240 | + public function testUnsetOffsetOnMaybeAccessible(): void |
| 241 | + { |
| 242 | + $this->reportMaybes = true; |
| 243 | + $this->unsetOffsetOnMaybeAccessible = true; |
| 244 | + $this->analyse([__DIR__ . '/data/unset-offset-maybe-accessible.php'], [ |
| 245 | + [ |
| 246 | + 'Cannot unset offset \'port\' on array{port?: int, path?: string}|false.', |
| 247 | + 13, |
| 248 | + ], |
| 249 | + [ |
| 250 | + 'Cannot unset offset \'port\' on array<string, int>|int.', |
| 251 | + 21, |
| 252 | + ], |
| 253 | + [ |
| 254 | + 'Cannot unset offset \'port\' on array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?: string}|false.', |
| 255 | + 56, |
| 256 | + ], |
| 257 | + [ |
| 258 | + 'Cannot unset offset \'port\' on array<string, int>|bool.', |
| 259 | + 91, |
| 260 | + ], |
| 261 | + [ |
| 262 | + 'Cannot unset offset \'port\' on array<string, int>|true.', |
| 263 | + 92, |
| 264 | + ], |
| 265 | + [ |
| 266 | + 'Cannot unset offset \'port\' on array<string, int>|float.', |
| 267 | + 93, |
| 268 | + ], |
| 269 | + [ |
| 270 | + 'Cannot unset offset \'port\' on array<string, int>|false.', |
| 271 | + 101, |
| 272 | + ], |
| 273 | + ]); |
| 274 | + } |
| 275 | + |
| 276 | + public function testUnsetOffsetOnMaybeAccessibleWithoutReportMaybes(): void |
| 277 | + { |
| 278 | + $this->unsetOffsetOnMaybeAccessible = true; |
| 279 | + $this->analyse([__DIR__ . '/data/unset-offset-maybe-accessible.php'], []); |
| 280 | + } |
| 281 | + |
| 282 | + public function testUnsetOffsetOnMaybeAccessibleWithoutFeatureToggle(): void |
| 283 | + { |
| 284 | + $this->reportMaybes = true; |
| 285 | + $this->analyse([__DIR__ . '/data/unset-offset-maybe-accessible.php'], []); |
| 286 | + } |
| 287 | + |
232 | 288 | } |
0 commit comments