From f6c6c5e184615d2591a9321fe121005e055d57a1 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 8 Jan 2022 13:24:22 +0100 Subject: [PATCH] Regression tests Closes https://github.com/phpstan/phpstan/issues/5337 --- .../UnusedPrivatePropertyRuleTest.php | 22 +++++++++++++++ .../PHPStan/Rules/DeadCode/data/bug-5337.php | 27 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-5337.php diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index 9c265857bd..854822fd6e 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -230,4 +230,26 @@ public function testBug5935(): void $this->analyse([__DIR__ . '/data/bug-5935.php'], []); } + public function testBug5337(): void + { + if (PHP_VERSION_ID < 70400 && !self::$useStaticReflectionProvider) { + $this->markTestSkipped('Test requires PHP 7.4.'); + } + + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + $this->analyse([__DIR__ . '/data/bug-5337.php'], [ + [ + 'Property Bug5337\Clazz::$prefix is never read, only written.', + 7, + 'See: https://phpstan.org/developing-extensions/always-read-written-properties', + ], + [ + 'Property Bug5337\Foo::$field is unused.', + 20, + 'See: https://phpstan.org/developing-extensions/always-read-written-properties', + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-5337.php b/tests/PHPStan/Rules/DeadCode/data/bug-5337.php new file mode 100644 index 0000000000..4051fc7628 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-5337.php @@ -0,0 +1,27 @@ += 7.4 + +namespace Bug5337; + +class Clazz +{ + private ?string $prefix; + + public function setter(string $prefix): void + { + if (!empty($this->prefix)) { + $this->prefix = $prefix; + } + } +} + +class Foo +{ + + private string $field; + + public function __construct() + { + if (isset($this->field)) {} + } + +}