Skip to content

Commit

Permalink
Add InAnyClassScope scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman committed Feb 11, 2022
1 parent 6db6c4b commit 487c7ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
44 changes: 44 additions & 0 deletions src/Analyser/InAnyClassScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\PropertyReflection;

class InAnyClassScope implements ClassMemberAccessAnswerer
{

/** @api */
public function __construct()
{
}

public function isInClass(): bool
{
return true;
}

public function getClassReflection(): ?ClassReflection
{
return null;
}

public function canAccessProperty(PropertyReflection $propertyReflection): bool
{
return $propertyReflection->isPublic();
}

public function canCallMethod(MethodReflection $methodReflection): bool
{
return $methodReflection->isPublic();
}

public function canAccessConstant(ConstantReflection $constantReflection): bool
{
return $constantReflection->isPublic();
}

}
12 changes: 3 additions & 9 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PHPStan\Type\Constant;

use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Analyser\InAnyClassScope;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\InaccessibleMethod;
use PHPStan\Reflection\ParametersAcceptor;
Expand Down Expand Up @@ -316,13 +316,7 @@ public function equals(Type $type): bool

public function isCallable(): TrinaryLogic
{
// @todo is there a way to fetch the full scope here.
// An OutOfClassScope causes frivolous errors when checking if a
// non-static method was invoked statically in its own class, which
// is allowed per: https://3v4l.org/mTa9S.
// Should add an optional scope parameter to isCallable and if null,
// create a new OutOfClassScope instance.
$typeAndMethod = $this->findTypeAndMethodName(new OutOfClassScope());
$typeAndMethod = $this->findTypeAndMethodName(new InAnyClassScope());
if ($typeAndMethod === null) {
return TrinaryLogic::createNo();
}
Expand Down Expand Up @@ -397,7 +391,7 @@ public function findTypeAndMethodName(?ClassMemberAccessAnswerer $scope = null):
$has = $has->and(TrinaryLogic::createMaybe());
}
if ($scope === null) {
$scope = new OutOfClassScope();
$scope = new InAnyClassScope();
}
if (
$isClassString
Expand Down

0 comments on commit 487c7ca

Please sign in to comment.