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)) {} + } + +}