Skip to content

Commit

Permalink
Simplify access to ClassReflection in ClassPropertyNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller authored and ondrejmirtes committed Aug 8, 2023
1 parent 9adae6c commit 11fdfe8
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ private function processStmtNode(
$scope->isInTrait(),
$scope->getClassReflection()->isReadOnly(),
false,
$scope->getClassReflection(),
), $methodScope);
$methodScope = $methodScope->assignExpression(new PropertyInitializationExpr($param->var->name), new MixedType(), new MixedType());
}
Expand Down Expand Up @@ -743,6 +744,7 @@ private function processStmtNode(
$scope->isInTrait(),
$scope->getClassReflection()->isReadOnly(),
$isAllowedPrivateMutation,
$scope->getClassReflection(),
),
$scope,
);
Expand Down
4 changes: 2 additions & 2 deletions src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
}
} elseif ($node instanceof ClassPropertyNode) {
$nativeTypeNode = $node->getNativeType();
if ($nativeTypeNode !== null && $scope->isInClass()) {
$nativeType = ParserNodeTypeToPHPStanType::resolve($nativeTypeNode, $scope->getClassReflection());
if ($nativeTypeNode !== null) {
$nativeType = ParserNodeTypeToPHPStanType::resolve($nativeTypeNode, $node->getClassReflection());
foreach ($nativeType->getReferencedClasses() as $referencedClass) {
$this->addClassToDependencies($referencedClass, $dependenciesReflections);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Node/ClassPropertyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeAbstract;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Type;

/** @api */
Expand All @@ -28,6 +29,7 @@ public function __construct(
private bool $isDeclaredInTrait,
private bool $isReadonlyClass,
private bool $isAllowedPrivateMutation,
private ClassReflection $classReflection,
)
{
parent::__construct($originalNode->getAttributes());
Expand Down Expand Up @@ -117,6 +119,11 @@ public function getNativeType()
return $this->type;
}

public function getClassReflection(): ClassReflection
{
return $this->classReflection;
}

public function getType(): string
{
return 'PHPStan_Node_ClassPropertyNode';
Expand Down
6 changes: 1 addition & 5 deletions src/Rules/Generics/PropertyVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Type\Generic\TemplateTypeVariance;
use function sprintf;
Expand All @@ -31,10 +30,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof ClassReflection) {
return [];
}
$classReflection = $node->getClassReflection();

if (!$classReflection->hasNativeProperty($node->getName())) {
return [];
Expand Down
20 changes: 9 additions & 11 deletions src/Rules/PhpDoc/IncompatiblePropertyPhpDocTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Rules\Generics\GenericObjectTypeCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -36,40 +35,39 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$propertyName = $node->getName();
$phpDocType = $node->getPhpDocType();
if ($phpDocType === null) {
return [];
}

$propertyName = $node->getName();

$description = 'PHPDoc tag @var';
if ($node->isPromoted()) {
$description = 'PHPDoc type';
}

$classReflection = $node->getClassReflection();

$messages = [];
if (
$this->unresolvableTypeHelper->containsUnresolvableType($phpDocType)
) {
$messages[] = RuleErrorBuilder::message(sprintf(
'%s for property %s::$%s contains unresolvable type.',
$description,
$scope->getClassReflection()->getDisplayName(),
$classReflection->getDisplayName(),
$propertyName,
))->build();
}

$nativeType = ParserNodeTypeToPHPStanType::resolve($node->getNativeType(), $scope->getClassReflection());
$nativeType = ParserNodeTypeToPHPStanType::resolve($node->getNativeType(), $classReflection);
$isSuperType = $nativeType->isSuperTypeOf($phpDocType);
if ($isSuperType->no()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'%s for property %s::$%s with type %s is incompatible with native type %s.',
$description,
$scope->getClassReflection()->getDisplayName(),
$classReflection->getDisplayName(),
$propertyName,
$phpDocType->describe(VerbosityLevel::typeOnly()),
$nativeType->describe(VerbosityLevel::typeOnly()),
Expand All @@ -79,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array
$errorBuilder = RuleErrorBuilder::message(sprintf(
'%s for property %s::$%s with type %s is not subtype of native type %s.',
$description,
$scope->getClassReflection()->getDisplayName(),
$classReflection->getDisplayName(),
$propertyName,
$phpDocType->describe(VerbosityLevel::typeOnly()),
$nativeType->describe(VerbosityLevel::typeOnly()),
Expand All @@ -92,7 +90,7 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = $errorBuilder->build();
}

$className = SprintfHelper::escapeFormatString($scope->getClassReflection()->getDisplayName());
$className = SprintfHelper::escapeFormatString($classReflection->getDisplayName());
$escapedPropertyName = SprintfHelper::escapeFormatString($propertyName);

$messages = array_merge($messages, $this->genericObjectTypeCheck->check(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\MixedType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
Expand All @@ -30,16 +29,13 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$classReflection = $scope->getClassReflection();
$default = $node->getDefault();
if ($default === null) {
return [];
}

$classReflection = $node->getClassReflection();

$propertyReflection = $classReflection->getNativeProperty($node->getName());
$propertyType = $propertyReflection->getWritableType();
if ($propertyReflection->getNativeType() instanceof MixedType) {
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Properties/ExistingClassesInPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function array_map;
use function array_merge;
use function sprintf;
Expand Down Expand Up @@ -41,11 +40,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$propertyReflection = $scope->getClassReflection()->getNativeProperty($node->getName());
$propertyReflection = $node->getClassReflection()->getNativeProperty($node->getName());
if ($this->checkThisOnly) {
$referencedClasses = $propertyReflection->getNativeType()->getReferencedClasses();
} else {
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Properties/MissingPropertyTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\MixedType;
use PHPStan\Type\VerbosityLevel;
use function implode;
Expand All @@ -31,11 +30,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$propertyReflection = $scope->getClassReflection()->getNativeProperty($node->getName());
$propertyReflection = $node->getClassReflection()->getNativeProperty($node->getName());

if ($propertyReflection->isPromoted()) {
return [];
Expand Down
11 changes: 3 additions & 8 deletions src/Rules/Properties/OverridingPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
Expand All @@ -36,11 +35,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$classReflection = $scope->getClassReflection();
$classReflection = $node->getClassReflection();
$prototype = $this->findPrototype($classReflection, $node->getName());
if ($prototype === null) {
return [];
Expand Down Expand Up @@ -121,7 +116,7 @@ public function processNode(Node $node, Scope $scope): array
$prototype->getNativeType()->describe(VerbosityLevel::typeOnly()),
))->nonIgnorable()->build();
} else {
$nativeType = ParserNodeTypeToPHPStanType::resolve($node->getNativeType(), $scope->getClassReflection());
$nativeType = ParserNodeTypeToPHPStanType::resolve($node->getNativeType(), $classReflection);
if (!$prototype->getNativeType()->equals($nativeType)) {
$typeErrors[] = RuleErrorBuilder::message(sprintf(
'Type %s of property %s::$%s is not the same as type %s of overridden property %s::$%s.',
Expand All @@ -139,7 +134,7 @@ public function processNode(Node $node, Scope $scope): array
'Property %s::$%s (%s) overriding property %s::$%s should not have a native type.',
$classReflection->getDisplayName(),
$node->getName(),
ParserNodeTypeToPHPStanType::resolve($node->getNativeType(), $scope->getClassReflection())->describe(VerbosityLevel::typeOnly()),
ParserNodeTypeToPHPStanType::resolve($node->getNativeType(), $classReflection)->describe(VerbosityLevel::typeOnly()),
$prototype->getDeclaringClass()->getDisplayName(),
$node->getName(),
))->nonIgnorable()->build();
Expand Down

0 comments on commit 11fdfe8

Please sign in to comment.