Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Rules/Properties/AccessPropertiesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function array_keys;
use function array_map;
use function array_merge;
use function count;
use function in_array;
use function sprintf;

#[AutowiredService]
Expand Down Expand Up @@ -125,10 +127,17 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
}
}

$propertyTags = [];
if (count($classNames) === 1) {
$propertyClassReflection = $this->reflectionProvider->getClass($classNames[0]);
$parentClassReflection = $propertyClassReflection->getParentClass();
$propertyTags = $propertyClassReflection->isInterface() ? [] : $propertyClassReflection->getPropertyTags();

while ($parentClassReflection !== null) {
if (!$parentClassReflection->isInterface()) {
$propertyTags[] = $parentClassReflection->getPropertyTags();
}

if ($parentClassReflection->hasProperty($name)) {
if ($write) {
if ($scope->canWriteProperty($parentClassReflection->getProperty($name, $scope))) {
Expand All @@ -146,11 +155,14 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
))->identifier('property.private')->build(),
];
}

$parentClassReflection = $parentClassReflection->getParentClass();
}
}

if (in_array($name, array_keys($propertyTags), true)) {
return [];
}

if ($node->name instanceof Expr) {
$propertyExistsExpr = new FuncCall(new FullyQualified('property_exists'), [
new Arg($node->var),
Expand Down
33 changes: 32 additions & 1 deletion tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,38 @@ public function testPropertyExists(): void
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/data/property-exists.php'], []);
$this->analyse([__DIR__ . '/data/property-exists.php'], [
[
'Access to an undefined property PropertyExists\Model::$getCreatedByColumn.',
27,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
[
'Access to an undefined property PropertyExists\Model::$getUpdatedByColumn.',
27,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
[
'Access to an undefined property PropertyExists\Model::$getDeletedByColumn.',
27,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
[
'Access to an undefined property PropertyExists\Model::$getCreatedAtColumn.',
27,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
[
'Access to an undefined property PropertyExists\Model::$getUpdatedAtColumn.',
27,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
[
'Access to an undefined property PropertyExists\Model::$getDeletedAtColumn.',
27,
'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>',
],
]);
}

}
9 changes: 5 additions & 4 deletions tests/PHPStan/Rules/Properties/data/property-exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace PropertyExists;

/**
* @property-read \stdClass $getCreator
*/
class Model
{

}

class Defaults
{
public function defaults(Model $model): void
{
$columns = [
'getCreator',
'getCreatedByColumn',
'getUpdatedByColumn',
'getDeletedByColumn',
Expand All @@ -21,9 +24,7 @@ public function defaults(Model $model): void
];

foreach ($columns as $column) {
if (property_exists($model, $column)) {
echo $model->{$column};
}
echo $model->{$column};
}
}
}
Loading