Skip to content

Fix AccessPropertiesCheck for checkThisOnly #4117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
55 changes: 41 additions & 14 deletions src/Rules/Properties/AccessPropertiesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function __construct(
private bool $checkDynamicProperties,
#[AutowiredParameter(ref: '%featureToggles.checkNonStringableDynamicAccess%')]
private bool $checkNonStringableDynamicAccess,
#[AutowiredParameter]
private bool $checkThisOnly,
)
{
}
Expand All @@ -58,20 +60,7 @@ public function check(PropertyFetch $node, Scope $scope, bool $write): array
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $scope->getType($node->name)->getConstantStrings());

if (!$write && $this->checkNonStringableDynamicAccess) {
$nameTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->name,
'',
static fn (Type $type) => $type->toString()->isString()->yes(),
);
$nameType = $nameTypeResult->getType();
if ($nameType instanceof ErrorType || $nameType->toString() instanceof ErrorType || !$nameType->toString()->isString()->yes()) {
$originalNameType = $scope->getType($node->name);
$className = $scope->getType($node->var)->describe(VerbosityLevel::typeOnly());
$errors[] = RuleErrorBuilder::message(sprintf('Property name for %s must be a string, but %s was given.', $className, $originalNameType->describe(VerbosityLevel::precise())))
->identifier('property.nameNotString')
->build();
}
$errors = array_merge($errors, $this->checkNonStringableDynamicAccess($scope, $node->var, $node->name));
}
}

Expand All @@ -82,6 +71,44 @@ public function check(PropertyFetch $node, Scope $scope, bool $write): array
return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function checkNonStringableDynamicAccess(Scope $scope, Expr $nodeVar, Expr $nodeName): array
{
if (
$this->checkThisOnly
&& !$this->ruleLevelHelper->isThis($nodeVar)
) {
return [];
}

$nameTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$nodeName,
'',
static fn (Type $type) => $type->toString()->isString()->yes(),
);
$nameType = $nameTypeResult->getType();
if (
!$nameType instanceof ErrorType
&& !$nameType->toString() instanceof ErrorType
&& $nameType->toString()->isString()->yes()
) {
return [];
}

$originalNameType = $scope->getType($nodeName);
$className = $scope->getType($nodeVar)->describe(VerbosityLevel::typeOnly());
return [
RuleErrorBuilder::message(sprintf(
'Property name for %s must be a string, but %s was given.',
$className,
$originalNameType->describe(VerbosityLevel::precise()),
))->identifier('property.nameNotString')->build(),
];
}

/**
* @return list<IdentifierRuleError>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ protected function getRule(): Rule
{
$reflectionProvider = self::createReflectionProvider();
return new AccessPropertiesInAssignRule(
new AccessPropertiesCheck($reflectionProvider, new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new PhpVersion(PHP_VERSION_ID), true, true, true),
new AccessPropertiesCheck(
$reflectionProvider,
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true),
new PhpVersion(PHP_VERSION_ID),
true,
true,
true,
false,
),
);
}

Expand Down
18 changes: 17 additions & 1 deletion tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ class AccessPropertiesRuleTest extends RuleTestCase
protected function getRule(): Rule
{
$reflectionProvider = self::createReflectionProvider();
return new AccessPropertiesRule(new AccessPropertiesCheck($reflectionProvider, new RuleLevelHelper($reflectionProvider, true, $this->checkThisOnly, $this->checkUnionTypes, false, false, false, true), new PhpVersion(PHP_VERSION_ID), true, $this->checkDynamicProperties, true));
return new AccessPropertiesRule(new AccessPropertiesCheck(
$reflectionProvider,
new RuleLevelHelper($reflectionProvider, true, $this->checkThisOnly, $this->checkUnionTypes, false, false, false, true),
new PhpVersion(PHP_VERSION_ID),
true,
$this->checkDynamicProperties,
true,
$this->checkThisOnly,
));
}

public function testAccessProperties(): void
Expand Down Expand Up @@ -1092,6 +1100,14 @@ public function testNewIsAlwaysFinalClass(): void
]);
}

public function testBug13271(): void
{
$this->checkThisOnly = true;
$this->checkUnionTypes = false;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/data/bug-13271.php'], []);
}

public function testPropertyExists(): void
{
$this->checkThisOnly = false;
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-13271.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types = 1);

namespace Bug13271;

$object = new class{
public string $example_one = "";
public string $example_two = "";
};

$field = rand() > 0.5 ? 'example_one' : 'example_two';
$result = $object->$field;
Loading