function it_can_be_static(ReflectionProperty $reflectionProperty) { $reflectionProperty->isStatic()->willReturn(false); $this->isStatic()->shouldBe(false); $reflectionProperty->isStatic()->willReturn(true); $this->isStatic()->shouldBe(true); }
function it_has_properties(ReflectionClass $reflectionClass, ReflectionProperty $property, AnnotationScanner $annotations, TypeFactory $typeFactory) { $reflectionClass->getName()->willReturn('MyClass'); $property->getName()->willReturn('id'); $property->getDocBlockTypeStrings()->willReturn(['string']); $property->getDocComment()->willReturn('/** @Target("CLASS") */'); $typeFactory->create(Argument::any(), Argument::type(Mixed::class), ['string'], false)->willReturn(new StringType()); $reflectionClass->getProperties()->willReturn([$property]); $annotations->scanForAnnotations('/** @Target("CLASS") */', '/var/www/MyClass.php', $this->imports)->willReturn(new Annotations([new Target(['value' => "CLASS"])])); $this->properties()->shouldHaveSize(1); }
/** * Given a property, attempt to find the type of the property. * * @param ReflectionProperty $reflectionProperty * @return Type[] */ public function __invoke(ReflectionProperty $reflectionProperty) { $contextFactory = new ContextFactory(); $context = $contextFactory->createForNamespace($reflectionProperty->getDeclaringClass()->getNamespaceName(), $reflectionProperty->getDeclaringClass()->getLocatedSource()->getSource()); $docBlock = DocBlockFactory::createInstance()->create($reflectionProperty->getDocComment(), new Context($context->getNamespace(), $context->getNamespaceAliases())); /* @var \phpDocumentor\Reflection\DocBlock\Tags\Var_ $varTag */ $resolvedTypes = []; $varTags = $docBlock->getTagsByName('var'); foreach ($varTags as $varTag) { $resolvedTypes = array_merge($resolvedTypes, (new ResolveTypes())->__invoke(explode('|', $varTag->getType()), $context)); } return $resolvedTypes; }
/** * 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; }
/** * Given a property, attempt to find the type of the property * * @param PropertyNode $node * @param ReflectionProperty $reflectionProperty * @return Type[] */ public function __invoke(PropertyNode $node, ReflectionProperty $reflectionProperty) { $contextFactory = new ContextFactory(); $context = $contextFactory->createForNamespace($reflectionProperty->getDeclaringClass()->getNamespaceName(), $reflectionProperty->getDeclaringClass()->getLocatedSource()->getSource()); /* @var \PhpParser\Comment\Doc $comment */ if (!$node->hasAttribute('comments')) { return []; } $comment = $node->getAttribute('comments')[0]; $docBlock = new DocBlock($comment->getReformattedText(), new DocBlock\Context($context->getNamespace(), $context->getNamespaceAliases())); /* @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $varTag */ $resolvedTypes = []; $varTags = $docBlock->getTagsByName('var'); foreach ($varTags as $varTag) { $resolvedTypes = array_merge($resolvedTypes, (new ResolveTypes())->__invoke($varTag->getTypes(), $context)); } return $resolvedTypes; }
/** * Factory depuis une reflection * * @param ReflectionProperty $reflection * @return static */ public static function fromReflection(ReflectionProperty $reflection) { // gestion du type $type = implode('|', $reflection->getDocBlockTypeStrings()); // cas de la valeur null $value = $reflection->getDefaultValue(); if (is_null($value)) { $value = Maker::NO_VALUE; $class = $reflection->getDeclaringClass(); foreach ($class->getAst()->stmts as $stmt) { // si pas un attribut, on zap if (!$stmt instanceof \PhpParser\Node\Stmt\Property) { continue; } foreach ($stmt->props as $prop) { if ($prop instanceof \PhpParser\Node\Stmt\PropertyProperty) { // lecture du fichier $file = file($class->getFileName()); if (!empty($line = $file[$prop->getLine() - 1])) { if (strpos($line, '=')) { $value = null; } } } } } } // construction $property = new static($reflection->getName(), $value, $type); // docblock $docblock = new \phpDocumentor\Reflection\DocBlock($reflection->getDocComment()); $property->setSummary($docblock->getShortDescription()); $property->setDescription($docblock->getLongDescription()); // gestion des modifiers $reflection->isPrivate() ? $property->enablePrivate() : $property->disablePrivate(); $reflection->isProtected() ? $property->enableProtected() : $property->disabledProtected(); $reflection->isPublic() ? $property->enablePublic() : $property->disablePublic(); $reflection->isStatic() ? $property->enableStatic() : $property->disableStatic(); return $property; }
/** * 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; }
/** * 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 testExportThrowsException() { $this->setExpectedException(\Exception::class); ReflectionProperty::export(); }
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()); }
/** * {@inheritDoc} */ public function getDocComment() { return $this->betterReflectionProperty->getDocComment(); }
/** * Create an AST PropertyNode given a reflection * * Note that we don't copy across DocBlock, protected, private or static * because runtime properties can't have these attributes. * * @param \ReflectionProperty $property * @param object $instance * @return PropertyNode */ private function createPropertyNodeFromReflection(\ReflectionProperty $property, $instance) { $builder = new PropertyNodeBuilder($property->getName()); $builder->setDefault($property->getValue($instance)); if ($property->isPublic()) { $builder->makePublic(); } return $builder->getNode(); }
public function isStatic() : bool { return $this->reflection->isStatic(); }