From b47ca9f56f7ea461f6a5e7a631c62f1e39525200 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Mon, 17 Oct 2022 11:49:59 +0200 Subject: [PATCH] Regression tests --- ...nexistentOffsetInArrayDimFetchRuleTest.php | 9 +++++++ tests/PHPStan/Rules/Arrays/data/bug-6243.php | 22 ++++++++++++++++ .../Rules/Classes/InstantiationRuleTest.php | 14 +++++++++++ .../PHPStan/Rules/Classes/data/bug-3311a.php | 25 +++++++++++++++++++ .../TypesAssignedToPropertiesRuleTest.php | 15 +++++++++++ .../Rules/Properties/data/bug-3311b.php | 17 +++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 tests/PHPStan/Rules/Arrays/data/bug-6243.php create mode 100644 tests/PHPStan/Rules/Classes/data/bug-3311a.php create mode 100644 tests/PHPStan/Rules/Properties/data/bug-3311b.php diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index cc2dca6fbf..0e5629f83e 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -544,4 +544,13 @@ public function testBug8068(): void ]); } + public function testBug6243(): void + { + if (PHP_VERSION_ID < 704000) { + $this->markTestSkipped('Test requires PHP 7.4.'); + } + + $this->analyse([__DIR__ . '/data/bug-6243.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-6243.php b/tests/PHPStan/Rules/Arrays/data/bug-6243.php new file mode 100644 index 0000000000..97c62f8512 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-6243.php @@ -0,0 +1,22 @@ += 7.4 + +namespace Bug6243; + +class Foo +{ + /** @var list|(\ArrayAccess&iterable) */ + private iterable $values; + + /** + * @param list $values + */ + public function update(array $values): void { + foreach ($this->values as $key => $_) { + unset($this->values[$key]); + } + + foreach ($values as $value) { + $this->values[] = $value; + } + } +} diff --git a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php index bb0b72a1d9..812be972e9 100644 --- a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php +++ b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php @@ -431,4 +431,18 @@ public function testBug7594(): void $this->analyse([__DIR__ . '/data/bug-7594.php'], []); } + public function testBug3311a(): void + { + if (PHP_VERSION_ID < 70400) { + $this->markTestSkipped('Test requires PHP 7.4.'); + } + + $this->analyse([__DIR__ . '/data/bug-3311a.php'], [ + [ + 'Parameter #1 $bar of class Bug3311a\Foo constructor expects list, array{1: \'baz\'} given.', + 24, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Classes/data/bug-3311a.php b/tests/PHPStan/Rules/Classes/data/bug-3311a.php new file mode 100644 index 0000000000..d4f646deb5 --- /dev/null +++ b/tests/PHPStan/Rules/Classes/data/bug-3311a.php @@ -0,0 +1,25 @@ += 7.4 + +namespace Bug3311a; + +final class Foo +{ + /** + * @var array + * @psalm-var list + */ + public array $bar = []; + + /** + * @param array $bar + * @psalm-param list $bar + */ + public function __construct(array $bar) + { + $this->bar = $bar; + } +} + +function () { + $instance = new Foo([1 => 'baz']); +}; diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index cd0fa17e76..47be97aa85 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -503,4 +503,19 @@ public function testIntegerRangesAndConstants(): void ]); } + public function testBug3311b(): void + { + if (PHP_VERSION_ID < 70400) { + $this->markTestSkipped('Test requires PHP 7.4.'); + } + + $this->checkExplicitMixed = true; + $this->analyse([__DIR__ . '/data/bug-3311b.php'], [ + [ + 'Property Bug3311b\Foo::$bar (list) does not accept non-empty-array, string>.', + 16, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Properties/data/bug-3311b.php b/tests/PHPStan/Rules/Properties/data/bug-3311b.php new file mode 100644 index 0000000000..93464208a8 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-3311b.php @@ -0,0 +1,17 @@ += 7.4 + +namespace Bug3311b; + +final class Foo +{ + /** + * @var array + * @psalm-var list + */ + public array $bar = []; +} + +function () { + $instance = new Foo; + $instance->bar[1] = 'baz'; +};