Skip to content

Commit

Permalink
Unification for working with current class reflection across CallStat…
Browse files Browse the repository at this point in the history
…icMethodsRule, ClassConstantRule, AccessStaticPropertiesRule
  • Loading branch information
ondrejmirtes committed Jan 24, 2021
1 parent b5c43e7 commit 863e6c6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/Rules/Classes/ClassConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array
];
}

$className = $scope->getClassReflection()->getName();
$classReflection = $scope->getClassReflection();
} elseif ($lowercasedClassName === 'parent') {
if (!$scope->isInClass()) {
return [
Expand All @@ -87,7 +87,7 @@ public function processNode(Node $node, Scope $scope): array
))->build(),
];
}
$className = $currentClassReflection->getParentClass()->getName();
$classReflection = $currentClassReflection->getParentClass();
} else {
if (!$this->reflectionProvider->hasClass($className)) {
if ($scope->isInClassExists($className)) {
Expand All @@ -109,13 +109,15 @@ public function processNode(Node $node, Scope $scope): array
$messages = $this->classCaseSensitivityCheck->checkClassNames([new ClassNameNodePair($className, $class)]);
}

$className = $this->reflectionProvider->getClass($className)->getName();
$classReflection = $this->reflectionProvider->getClass($className);
}

if (strtolower($constantName) === 'class') {
return $messages;
}

$className = $classReflection->getName();

if ($scope->isInClass() && $scope->getClassReflection()->getName() === $className) {
$classType = new ThisType($scope->getClassReflection());
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
Expand Down Expand Up @@ -139,7 +140,12 @@ public function processNode(Node $node, Scope $scope): array
}

$className = $classReflection->getName();
$classType = new ObjectType($className);
if ($scope->isInClass() && $scope->getClassReflection()->getName() === $className) {
$classType = new ThisType($scope->getClassReflection());
$classReflection = $scope->getClassReflection();
} else {
$classType = new ObjectType($className);
}

if ($classReflection->hasNativeMethod($methodName) && $lowercasedClassName !== 'static') {
$nativeMethodReflection = $classReflection->getNativeMethod($methodName);
Expand Down Expand Up @@ -169,6 +175,9 @@ static function (Type $type) use ($methodName): bool {
}

$typeForDescribe = $classType;
if ($classType instanceof ThisType) {
$typeForDescribe = $classType->getStaticObjectType();
}
$classType = TypeCombinator::remove($classType, new StringType());

if (!$classType->canCallMethods()->yes()) {
Expand Down
36 changes: 23 additions & 13 deletions src/Rules/Properties/AccessStaticPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
Expand Down Expand Up @@ -89,7 +90,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
))->build(),
];
}
$className = $scope->getClassReflection()->getName();
$classReflection = $scope->getClassReflection();
} elseif ($lowercasedClass === 'parent') {
if (!$scope->isInClass()) {
return [
Expand Down Expand Up @@ -122,7 +123,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
return [];
}

$className = $scope->getClassReflection()->getParentClass()->getName();
$classReflection = $scope->getClassReflection()->getParentClass();
} else {
if (!$this->reflectionProvider->hasClass($class)) {
if ($scope->isInClassExists($class)) {
Expand All @@ -141,19 +142,25 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
}

$classReflection = $this->reflectionProvider->getClass($class);
$className = $this->reflectionProvider->getClass($class)->getName();
if ($classReflection->isTrait()) {
return [
RuleErrorBuilder::message(sprintf(
'Access to static property $%s on trait %s.',
$name,
$className
))->build(),
];
}
}

$classType = new ObjectType($className);
$className = $classReflection->getName();
if ($scope->isInClass() && $scope->getClassReflection()->getName() === $className) {
$classType = new ThisType($scope->getClassReflection());
$classReflection = $scope->getClassReflection();
} else {
$classType = new ObjectType($className);
}

if ($classReflection->isTrait()) {
return [
RuleErrorBuilder::message(sprintf(
'Access to static property $%s on trait %s.',
$name,
$className
))->build(),
];
}
} else {
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
Expand All @@ -174,6 +181,9 @@ static function (Type $type) use ($name): bool {
}

$typeForDescribe = $classType;
if ($classType instanceof ThisType) {
$typeForDescribe = $classType->getStaticObjectType();
}
$classType = TypeCombinator::remove($classType, new StringType());

if ($scope->isInExpressionAssign($node)) {
Expand Down

0 comments on commit 863e6c6

Please sign in to comment.