canAccessConstant() public method

public canAccessConstant ( PHPStan\Reflection\ClassConstantReflection $constantReflection ) : boolean
$constantReflection PHPStan\Reflection\ClassConstantReflection
return boolean
Example #1
0
 /**
  * @param \PhpParser\Node\Expr\ClassConstFetch $node
  * @param \PHPStan\Analyser\Scope $scope
  * @return string[]
  */
 public function processNode(Node $node, Scope $scope) : array
 {
     $class = $node->class;
     if ($class instanceof \PhpParser\Node\Name) {
         $className = (string) $class;
     } else {
         $classType = $scope->getType($class);
         if ($classType->getClass() !== null) {
             $className = $classType->getClass();
         } else {
             return [];
         }
     }
     if ($className === 'self' || $className === 'static') {
         if ($scope->getClass() === null && !$scope->isInAnonymousClass()) {
             return [sprintf('Using %s outside of class scope.', $className)];
         }
         if ($className === 'static') {
             return [];
         }
         if ($className === 'self') {
             $className = $scope->getClass();
         }
     }
     $constantName = $node->name;
     if ($scope->getClass() !== null && $className === 'parent') {
         $currentClassReflection = $this->broker->getClass($scope->getClass());
         if ($currentClassReflection->getParentClass() === false) {
             return [sprintf('Access to parent::%s but %s does not extend any class.', $constantName, $scope->getClass())];
         }
         $className = $currentClassReflection->getParentClass()->getName();
     }
     if (!$this->broker->hasClass($className)) {
         return [sprintf('Class %s not found.', $className)];
     }
     if ($constantName === 'class') {
         return [];
     }
     $classReflection = $this->broker->getClass($className);
     if (!$classReflection->hasConstant($constantName)) {
         return [sprintf('Access to undefined constant %s::%s.', $classReflection->getName(), $constantName)];
     }
     $constantReflection = $classReflection->getConstant($constantName);
     if (!$scope->canAccessConstant($constantReflection)) {
         return [sprintf('Cannot access constant %s::%s from current scope.', $constantReflection->getDeclaringClass()->getName(), $constantName)];
     }
     return [];
 }