Skip to content

Commit

Permalink
ClassReflection - stop on first accessible property from current scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 17, 2016
1 parent c9ed95c commit 2e64fbb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public function getType(Node $node): Type
return new MixedType(true);
}

return $propertyClassReflection->getProperty($node->name)->getType();
return $propertyClassReflection->getProperty($node->name, $this)->getType();
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Reflection;

use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;

class ClassReflection
Expand Down Expand Up @@ -95,27 +96,20 @@ public function getMethod(string $methodName): MethodReflection
return $this->methods[$methodName];
}

public function getProperty(string $propertyName): PropertyReflection
public function getProperty(string $propertyName, Scope $scope = null): PropertyReflection
{
if (!isset($this->properties[$propertyName])) {
$privateProperty = null;
$publicProperty = null;
foreach ($this->propertiesClassReflectionExtensions as $extension) {
if ($extension->hasProperty($this, $propertyName)) {
$property = $extension->getProperty($this, $propertyName);
if ($privateProperty === null && !$property->isPublic()) {
$privateProperty = $property;
} elseif ($publicProperty === null && $property->isPublic()) {
$publicProperty = $property;
if ($scope !== null && $scope->canAccessProperty($property)) {
return $this->properties[$propertyName] = $property;
}
$this->properties[$propertyName] = $property;
}
}

if ($publicProperty !== null) {
return $this->properties[$propertyName] = $publicProperty;
} elseif ($privateProperty !== null) {
return $this->properties[$propertyName] = $privateProperty;
}
}

return $this->properties[$propertyName];
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/AccessPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
];
}

$propertyReflection = $propertyClassReflection->getProperty($name);
$propertyReflection = $propertyClassReflection->getProperty($name, $scope);
if (!$scope->canAccessProperty($propertyReflection)) {
return [
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/AccessStaticPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function processNode(Node $node, Scope $scope): array
];
}

$property = $classReflection->getProperty($name);
$property = $classReflection->getProperty($name, $scope);
if (!$property->isStatic()) {
return [
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/ExistingClassesInPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function processNode(Node $node, Scope $scope): array
}

$classReflection = $this->broker->getClass($className);
$propertyType = $classReflection->getProperty($node->name)->getType();
$propertyType = $classReflection->getProperty($node->name, $scope)->getType();

if ($propertyType instanceof ArrayType) {
$nestedItemType = $propertyType->getNestedItemType();
Expand Down

0 comments on commit 2e64fbb

Please sign in to comment.