createFromNode() public static method

public static createFromNode ( BetterReflection\Reflector\Reflector $reflector, Property $node, ReflectionClass $declaringClass, boolean $declaredAtCompileTime = true ) : ReflectionProperty
$reflector BetterReflection\Reflector\Reflector
$node PhpParser\Node\Stmt\Property
$declaringClass ReflectionClass
$declaredAtCompileTime boolean
return ReflectionProperty
Esempio n. 1
0
 /**
  * Create from a Class Node
  *
  * @param ClassNode $node
  * @param LocatedSource $locatedSource
  * @param NamespaceNode|null $namespace optional - if omitted, we assume it is global namespaced class
  * @return ReflectionClass
  */
 public static function createFromNode(ClassNode $node, LocatedSource $locatedSource, NamespaceNode $namespace = null)
 {
     $class = new self();
     $class->locatedSource = $locatedSource;
     $class->name = $node->name;
     if (null !== $namespace) {
         $class->declaringNamespace = $namespace;
     }
     $methodNodes = $node->getMethods();
     foreach ($methodNodes as $methodNode) {
         $class->methods[] = ReflectionMethod::createFromNode($methodNode, $class);
     }
     foreach ($node->stmts as $stmt) {
         if ($stmt instanceof ConstNode) {
             $constName = $stmt->consts[0]->name;
             $constValue = (new CompileNodeToValue())->__invoke($stmt->consts[0]->value);
             $class->constants[$constName] = $constValue;
         }
         if ($stmt instanceof PropertyNode) {
             $prop = ReflectionProperty::createFromNode($stmt, $class);
             $class->properties[$prop->getName()] = $prop;
         }
     }
     return $class;
 }
 /**
  * Get the properties for this class.
  *
  * @return ReflectionProperty[]
  */
 public function getProperties()
 {
     if (null !== $this->cachedProperties) {
         return $this->cachedProperties;
     }
     $properties = [];
     foreach ($this->node->stmts as $stmt) {
         if ($stmt instanceof PropertyNode) {
             $prop = ReflectionProperty::createFromNode($this->reflector, $stmt, $this);
             $properties[$prop->getName()] = $prop;
         }
     }
     $this->cachedProperties = $properties;
     return $properties;
 }
Esempio n. 3
0
 /**
  * Create from a Class Node.
  *
  * @param Reflector          $reflector
  * @param ClassLikeNode      $node
  * @param LocatedSource      $locatedSource
  * @param NamespaceNode|null $namespace optional - if omitted, we assume it is global namespaced class
  *
  * @return ReflectionClass
  */
 public static function createFromNode(Reflector $reflector, ClassLikeNode $node, LocatedSource $locatedSource, NamespaceNode $namespace = null)
 {
     $class = new self();
     $class->reflector = $reflector;
     $class->node = $node;
     $class->locatedSource = $locatedSource;
     $class->name = $node->name;
     if (null !== $namespace) {
         $class->declaringNamespace = $namespace;
     }
     if ($node instanceof ClassNode && null !== $node->extends) {
         $objectType = (new FindTypeFromAst())->__invoke($node->extends, $locatedSource, $class->getNamespaceName());
         if (null !== $objectType && $objectType instanceof Object_) {
             $class->extendsClassType = $objectType->getFqsen();
         }
     }
     $methodNodes = $node->getMethods();
     foreach ($methodNodes as $methodNode) {
         $class->methods[] = ReflectionMethod::createFromNode($methodNode, $class);
     }
     foreach ($node->stmts as $stmt) {
         if ($stmt instanceof ConstNode) {
             $constName = $stmt->consts[0]->name;
             $constValue = (new CompileNodeToValue())->__invoke($stmt->consts[0]->value);
             $class->constants[$constName] = $constValue;
         }
         if ($stmt instanceof PropertyNode) {
             $prop = ReflectionProperty::createFromNode($stmt, $class);
             $class->properties[$prop->getName()] = $prop;
         }
     }
     return $class;
 }
 public function testIsDefaultWithRuntimeDeclaredProperty()
 {
     $this->assertFalse(ReflectionProperty::createFromNode($this->reflector, new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo')]), $this->reflector->reflect('\\BetterReflectionTest\\Fixture\\ExampleClass'), false)->isDefault());
 }
 /**
  * Reflect on runtime properties for the current instance
  *
  * @return ReflectionProperty[]
  */
 private function getRuntimeProperties()
 {
     if (!$this->reflectionClass->isInstance($this->object)) {
         throw new \InvalidArgumentException('Cannot reflect runtime properties of a separate class');
     }
     // Ensure we have already cached existing properties so we can add to them
     $this->reflectionClass->getProperties();
     // Only known current way is to use internal ReflectionObject to get
     // the runtime-declared properties  :/
     $reflectionProperties = (new \ReflectionObject($this->object))->getProperties();
     $runtimeProperties = [];
     foreach ($reflectionProperties as $property) {
         if ($this->reflectionClass->hasProperty($property->getName())) {
             continue;
         }
         $runtimeProperty = ReflectionProperty::createFromNode($this->reflector, $this->createPropertyNodeFromReflection($property, $this->object), $this, false);
         $runtimeProperties[$runtimeProperty->getName()] = $runtimeProperty;
     }
     return $runtimeProperties;
 }