From ab851203d475b0b141817912fbb0955eaa7803d3 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 17 Mar 2020 15:48:22 +0100 Subject: [PATCH] Fixed issue with ArrayAccess stub --- stubs/ArrayObject.stub | 2 +- .../Arrays/OffsetAccessAssignmentRuleTest.php | 6 ++++++ .../PHPStan/Rules/Arrays/data/new-offset-stub.php | 15 +++++++++++++++ tests/PHPStan/Rules/Methods/data/bug-2950.php | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Arrays/data/new-offset-stub.php diff --git a/stubs/ArrayObject.stub b/stubs/ArrayObject.stub index fd26880c93..3fddb2b313 100644 --- a/stubs/ArrayObject.stub +++ b/stubs/ArrayObject.stub @@ -20,7 +20,7 @@ interface ArrayAccess public function offsetGet($offset); /** - * @param TKey $offset + * @param TKey|null $offset * @param TValue $value * @return void */ diff --git a/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php b/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php index dd90723a8a..fe1cfba803 100644 --- a/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php @@ -121,4 +121,10 @@ public function testInheritDocTemplateTypeResolution(): void $this->analyse([__DIR__ . '/data/inherit-doc-template-type-resolution.php'], []); } + public function testAssignNewOffsetToStubbedClass(): void + { + $this->checkUnionTypes = true; + $this->analyse([__DIR__ . '/data/new-offset-stub.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/new-offset-stub.php b/tests/PHPStan/Rules/Arrays/data/new-offset-stub.php new file mode 100644 index 0000000000..6d7584aa4b --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/new-offset-stub.php @@ -0,0 +1,15 @@ + + */ +abstract class Foo implements \ArrayAccess +{ + +} + +function (Foo $foo): void { + $foo[] = new \stdClass(); +}; diff --git a/tests/PHPStan/Rules/Methods/data/bug-2950.php b/tests/PHPStan/Rules/Methods/data/bug-2950.php index 8e5c980034..c1a3da07c7 100644 --- a/tests/PHPStan/Rules/Methods/data/bug-2950.php +++ b/tests/PHPStan/Rules/Methods/data/bug-2950.php @@ -100,7 +100,7 @@ public function offsetGet($offset): float } /** - * @param int $offset + * @param int|null $offset * @param float $value */ public function offsetSet($offset, $value): void