Skip to content

Commit

Permalink
Lazy creation of class properties - prevents autoloading of untouched…
Browse files Browse the repository at this point in the history
… properties types
  • Loading branch information
ondrejmirtes committed Nov 16, 2017
1 parent 76f49c6 commit 8055761
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,31 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
if (!isset($this->propertiesIncludingAnnotations[$classReflection->getName()])) {
$this->propertiesIncludingAnnotations[$classReflection->getName()] = $this->createProperties($classReflection, true);
if (!isset($this->propertiesIncludingAnnotations[$classReflection->getName()][$propertyName])) {
$this->propertiesIncludingAnnotations[$classReflection->getName()][$propertyName] = $this->createProperty($classReflection, $propertyName, true);
}

return $this->propertiesIncludingAnnotations[$classReflection->getName()][$propertyName];
}

public function getNativeProperty(ClassReflection $classReflection, string $propertyName): PhpPropertyReflection
{
if (!isset($this->nativeProperties[$classReflection->getName()])) {
/** @var \PHPStan\Reflection\Php\PhpPropertyReflection[] $properties */
$properties = $this->createProperties($classReflection, false);
$this->nativeProperties[$classReflection->getName()] = $properties;
if (!isset($this->nativeProperties[$classReflection->getName()][$propertyName])) {
/** @var \PHPStan\Reflection\Php\PhpPropertyReflection $property */
$property = $this->createProperty($classReflection, $propertyName, false);
$this->nativeProperties[$classReflection->getName()][$propertyName] = $property;
}

return $this->nativeProperties[$classReflection->getName()][$propertyName];
}

/**
* @param \PHPStan\Reflection\ClassReflection $classReflection
* @param bool $includingAnnotations
* @return \PHPStan\Reflection\PropertyReflection[]
*/
private function createProperties(
ClassReflection $classReflection,
bool $includingAnnotations
): array
{
$properties = [];
foreach ($classReflection->getNativeReflection()->getProperties() as $propertyReflection) {
$properties[$propertyReflection->getName()] = $this->createProperty($classReflection, $propertyReflection, $includingAnnotations);
}

return $properties;
}

private function createProperty(
ClassReflection $classReflection,
\ReflectionProperty $propertyReflection,
string $propertyName,
bool $includingAnnotations
): PropertyReflection
{
$propertyReflection = $classReflection->getNativeReflection()->getProperty($propertyName);
$propertyName = $propertyReflection->getName();
$declaringClassReflection = $this->broker->getClass($propertyReflection->getDeclaringClass()->getName());

Expand Down

0 comments on commit 8055761

Please sign in to comment.