diff --git a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php index d4b789f348..902127910b 100644 --- a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php @@ -303,6 +303,221 @@ public function testAccessPropertiesOnThisOnly(): void ); } + public function testAccessPropertiesOnDynamicProperties(): void + { + $this->checkThisOnly = false; + $this->checkUnionTypes = true; + $this->checkDynamicProperties = true; + $this->analyse( + [__DIR__ . '/data/access-properties.php'], + [ + [ + 'Access to an undefined property TestAccessProperties\BarAccessProperties::$loremipsum.', + 23, + ], + [ + 'Access to private property $foo of parent class TestAccessProperties\FooAccessProperties.', + 24, + ], + [ + 'Cannot access property $propertyOnString on string.', + 31, + ], + [ + 'Access to private property TestAccessProperties\FooAccessProperties::$foo.', + 42, + ], + [ + 'Access to protected property TestAccessProperties\FooAccessProperties::$bar.', + 43, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$baz.', + 45, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$baz.', + 48, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$baz.', + 49, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$nonexistent.', + 51, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$nonexistent.', + 52, + ], + [ + 'Access to private property TestAccessProperties\FooAccessProperties::$foo.', + 58, + ], + [ + 'Access to protected property TestAccessProperties\FooAccessProperties::$bar.', + 59, + ], + [ + 'Access to property $foo on an unknown class TestAccessProperties\UnknownClass.', + 63, + 'Learn more at https://phpstan.org/user-guide/discovering-symbols', + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$emptyBaz.', + 65, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$emptyBaz.', + 68, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$emptyNonexistent.', + 69, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$emptyNonexistent.', + 70, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherNonexistent.', + 75, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherNonexistent.', + 76, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherNonexistent.', + 76, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherNonexistent.', + 77, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherNonexistent.', + 77, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherNonexistent.', + 78, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherEmptyNonexistent.', + 80, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherEmptyNonexistent.', + 80, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherEmptyNonexistent.', + 81, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherEmptyNonexistent.', + 82, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherEmptyNonexistent.', + 83, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$anotherEmptyNonexistent.', + 83, + ], + [ + 'Access to property $test on an unknown class TestAccessProperties\FirstUnknownClass.', + 146, + 'Learn more at https://phpstan.org/user-guide/discovering-symbols', + ], + [ + 'Access to property $test on an unknown class TestAccessProperties\SecondUnknownClass.', + 146, + 'Learn more at https://phpstan.org/user-guide/discovering-symbols', + ], + [ + 'Access to an undefined property TestAccessProperties\WithFooAndBarProperty|TestAccessProperties\WithFooProperty::$bar.', + 176, + ], + [ + 'Access to an undefined property TestAccessProperties\SomeInterface&TestAccessProperties\WithFooProperty::$bar.', + 193, + ], + [ + 'Cannot access property $ipsum on TestAccessProperties\FooAccessProperties|null.', + 207, + ], + [ + 'Cannot access property $foo on null.', + 220, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$lorem.', + 247, + ], + [ + 'Access to an undefined property TestAccessProperties\FooAccessProperties::$dolor.', + 250, + ], + [ + 'Access to an undefined property TestAccessProperties\NullCoalesce::$bar.', + 264, + ], + [ + 'Access to an undefined property TestAccessProperties\NullCoalesce::$bar.', + 266, + ], + [ + 'Access to an undefined property TestAccessProperties\NullCoalesce::$bar.', + 270, + ], + [ + 'Cannot access property $bar on TestAccessProperties\NullCoalesce|null.', + 272, + ], + [ + 'Cannot access property $foo on TestAccessProperties\NullCoalesce|null.', + 272, + ], + [ + 'Cannot access property $foo on TestAccessProperties\NullCoalesce|null.', + 272, + ], + [ + 'Access to an undefined property TestAccessProperties\IssetPropertyInWhile::$foo.', + 282, + ], + [ + 'Access to an undefined property class@anonymous/tests/PHPStan/Rules/Properties/data/access-properties.php:294::$barProperty.', + 299, + ], + [ + 'Cannot access property $foo on TestAccessProperties\PropertyIssetOnPossibleFalse|false.', + 315, + ], + [ + 'Access to an undefined property TestAccessProperties\AccessInIsset::$foo.', + 379, + ], + [ + 'Access to an undefined property TestAccessProperties\AccessInIsset::$foo.', + 386, + ], + [ + 'Cannot access property $selfOrNull on TestAccessProperties\RevertNonNullabilityForIsset|null.', + 402, + ], + [ + 'Cannot access property $array on stdClass|null.', + 412, + ], + ], + ); + } + public function testAccessPropertiesAfterIsNullInBooleanOr(): void { $this->checkThisOnly = false; @@ -576,6 +791,39 @@ public function testDynamicProperties(): void $this->analyse([__DIR__ . '/data/dynamic-properties.php'], []); } + public function testCheckDynamicProperties(): void + { + $this->checkThisOnly = false; + $this->checkUnionTypes = true; + $this->checkDynamicProperties = true; + $this->analyse([__DIR__ . '/data/dynamic-properties.php'], [ + [ + 'Access to an undefined property DynamicProperties\Foo::$dynamicProperty.', + 9, + ], + [ + 'Access to an undefined property DynamicProperties\Foo::$dynamicProperty.', + 10, + ], + [ + 'Access to an undefined property DynamicProperties\Foo::$dynamicProperty.', + 11, + ], + [ + 'Access to an undefined property DynamicProperties\Bar::$dynamicProperty.', + 14, + ], + [ + 'Access to an undefined property DynamicProperties\Bar::$dynamicProperty.', + 15, + ], + [ + 'Access to an undefined property DynamicProperties\Bar::$dynamicProperty.', + 16, + ], + ]); + } + public function testBug4559(): void { $this->checkThisOnly = false;