Skip to content

Commit

Permalink
PHP 8.2 - behave as if checkDynamicProperties was set to true
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 19, 2022
1 parent 92bc503 commit 22561e7
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 289 deletions.
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ public function producesWarningForFinalPrivateMethods(): bool
return $this->versionId >= 80000;
}

public function deprecatesDynamicProperties(): bool
{
return $this->versionId >= 80200;
}

}
6 changes: 5 additions & 1 deletion src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ public function check(Expr $expr, Scope $scope, string $operatorDescription, cal
return null;
}

return RuleErrorBuilder::message(sprintf('Using nullsafe property access "?->%s" %s is unnecessary. Use -> instead.', $expr->name->name ?? '(Expression)', $operatorDescription))->build();
if ($expr->name instanceof Node\Identifier) {
return RuleErrorBuilder::message(sprintf('Using nullsafe property access "?->%s" %s is unnecessary. Use -> instead.', $expr->name->name, $operatorDescription))->build();
}

return RuleErrorBuilder::message(sprintf('Using nullsafe property access "?->(Expression)" %s is unnecessary. Use -> instead.', $operatorDescription))->build();
}

return null;
Expand Down
12 changes: 11 additions & 1 deletion src/Rules/Properties/AccessPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
Expand All @@ -32,6 +33,7 @@ class AccessPropertiesRule implements Rule
public function __construct(
private ReflectionProvider $reflectionProvider,
private RuleLevelHelper $ruleLevelHelper,
private PhpVersion $phpVersion,
private bool $reportMagicProperties,
private bool $checkDynamicProperties,
)
Expand Down Expand Up @@ -165,7 +167,15 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string

private function canAccessUndefinedProperties(Scope $scope, Node\Expr $node): bool
{
return $scope->isUndefinedExpressionAllowed($node) && !$this->checkDynamicProperties;
if (!$scope->isUndefinedExpressionAllowed($node)) {
return false;
}

if ($this->checkDynamicProperties) {
return false;
}

return !$this->phpVersion->deprecatesDynamicProperties();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Properties;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -17,7 +18,7 @@ protected function getRule(): Rule
{
$reflectionProvider = $this->createReflectionProvider();
return new AccessPropertiesInAssignRule(
new AccessPropertiesRule($reflectionProvider, new RuleLevelHelper($reflectionProvider, true, false, true, false), true, true),
new AccessPropertiesRule($reflectionProvider, new RuleLevelHelper($reflectionProvider, true, false, true, false), new PhpVersion(PHP_VERSION_ID), true, true),
);
}

Expand Down
Loading

0 comments on commit 22561e7

Please sign in to comment.