/** * {@inheritdoc} */ public function match(\ReflectionProperty $property, $givenName) { if (array_key_exists($property->getName(), $this->map)) { return $this->map[$property->getName()] === $givenName; } return false; }
/** * @param \ReflectionProperty $accessedProperty * @param string $nameSuffix */ public function __construct(ReflectionProperty $accessedProperty, $nameSuffix) { $this->accessedProperty = $accessedProperty; $originalName = $this->accessedProperty->getName(); $name = UniqueIdentifierGenerator::getIdentifier($originalName . $nameSuffix); parent::__construct(Class_::MODIFIER_PRIVATE, array(new PropertyProperty($name))); }
public function defaultValue() { if ($this->property->isDefault()) { $defaultProperties = $this->property->getDeclaringClass()->getDefaultProperties(); return $defaultProperties[$this->property->getName()]; } return null; }
/** * Returns declaring class or trait. * @return \ReflectionClass */ public static function getDeclaringClass(\ReflectionProperty $prop) { foreach ($prop->getDeclaringClass()->getTraits() as $trait) { if ($trait->hasProperty($prop->getName())) { return self::getDeclaringClass($trait->getProperty($prop->getName())); } } return $prop->getDeclaringClass(); }
/** * Has the class in annotation this property as key? * * @return boolean */ public function isKey() { if ($this->isKey === null) { foreach ($this->getAnnotationClass()->getAll('key') as $key) { if ($key == $this->property->getName()) { return true; } } } return $this->isKey; }
/** * Generic class property resolver. * * @param \ReflectionProperty $property * @param ClassMetadata $classMetadata * * @return PropertyMetadata Resolved property metadata */ protected function resolvePropertyMetadata(\ReflectionProperty $property, ClassMetadata $classMetadata) : PropertyMetadata { // Create method metadata instance $propertyMetadata = $classMetadata->propertiesMetadata[$property->getName()] ?? new PropertyMetadata($classMetadata); $propertyMetadata->name = $property->getName(); $propertyMetadata->modifiers = $property->getModifiers(); $propertyMetadata->isPublic = $property->isPublic(); $propertyMetadata->typeHint = $this->getCommentTypeHint(is_string($property->getDocComment()) ? $property->getDocComment() : ''); // Store property metadata to class metadata return $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata; }
/** * Exports the PHP code * * @return string */ public function exportCode() { $default_properties = $this->_property->getDeclaringClass()->getDefaultProperties(); $modifiers = \Reflection::getModifierNames($this->_property->getModifiers()); $default_value = null; if (array_key_exists($this->_property->getName(), $default_properties)) { $default_value = $default_properties[$this->_property->getName()]; if (!is_numeric($default_value)) { $default_value = "'{$default_value}'"; } } return sprintf('%s $%s%s;', join(' ', $modifiers), $this->_property->getName(), !is_null($default_value) ? " = {$default_value}" : ''); }
public static function getPropertyIdentifier(\ReflectionProperty $reflectionProperty, $className) { $key = null; if ($reflectionProperty->isPrivate()) { $key = '\\0' . $className . '\\0' . $reflectionProperty->getName(); } elseif ($reflectionProperty->isProtected()) { $key = '' . "" . '*' . "" . $reflectionProperty->getName(); } elseif ($reflectionProperty->isPublic()) { $key = $reflectionProperty->getName(); } if (null === $key) { throw new \InvalidArgumentException('Unable to detect property visibility'); } return $key; }
/** * Parse the docblock of the property to get the class of the var annotation. * * @param \ReflectionProperty $property * * @throws AnnotationException Non exists class. * * @return null|ObjectDefinition */ public function getPropertyClass(\ReflectionProperty $property) { $propertyComment = $property->getDocComment(); if (!preg_match('/@var\\s+([^\\s\\(\\*\\/]+)/', $propertyComment, $matches)) { return; } $className = end($matches); if (!is_string($className) || in_array($className, static::$ignoredTypes)) { return; } $classWithNamespace = $className; if ($this->namespaceExists($classWithNamespace) === false) { $classWithNamespace = $this->namespace . '\\' . $className; } if (!$this->classExists($classWithNamespace)) { $declaringClass = $property->getDeclaringClass(); throw new AnnotationException(sprintf('The @var annotation on %s::%s contains a non existent class "%s"', $declaringClass->name, $property->getName(), $className)); } $createNewObject = function ($propertyComment, $className, $classWithNamespace) { $classParameters = $this->propertyClassParameters($propertyComment, $className); if (is_array($classParameters)) { $values = []; foreach ($classParameters as $value) { $values[] = static::parseValue($value); } $object = new ObjectDefinition($classWithNamespace, $className); $object->setConstructorInjection($values); return $object; } return new $classWithNamespace(); }; return $createNewObject($propertyComment, $className, $classWithNamespace); }
/** * {@inheritdoc} * @throws PropertyDefinitionNotFoundException */ public function resolveProperty(DefinitionAnalyzer $analyzer, ClassDefinition $classDefinition, \ReflectionProperty $reflectionProperty) { $propertyName = $reflectionProperty->getName(); if ($classDefinition->hasProperty($propertyName)) { $classDefinition->getProperty($propertyName)->defineDependency(new ServiceReference($this->value['value'])); } }
/** * @param ReflectionProperty * @param array (string => string) * @param string * @return string */ public function getNativePropertyType(ReflectionProperty $prop, &$defaults, &$type) { $name = $prop->getName(); if ($doc = trim($prop->getDocComment(), " \t\r\n*/")) { if (preg_match('/@var[ \\t]+([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s*(:?\\[[\\s0-9]*\\]|)/', $doc, $m)) { if ($m[2]) { $type = 'arr'; } else { $type = substr(strtolower($m[1]), 0, 3); if ($type == 'mix') { $type = 'str'; } elseif (!in_array($type, self::$phpTypes)) { $type = 'obj'; } } } } if (!$type) { $type = substr(gettype(self::referenceObj($prop->getDeclaringClass()->getName())->{$name}), 0, 3); } $nativeType = $this->nativeTypes[$type == 'str' ? strlen('' . $defaults[$name]) > 255 ? 'tex' : 'str' : $type]; if (!$nativeType) { throw new IllegalTypeException('Unstorable type: ' . $type . ' for property: ' . $name); } return $nativeType; }
/** * @param \ReflectionProperty $property * @return int */ public static function getPropertyLine(\ReflectionProperty $property) { $class = $property->getDeclaringClass(); $context = 'file'; $contextBrackets = 0; foreach (token_get_all(file_get_contents($class->getFileName())) as $token) { if ($token === '{') { $contextBrackets += 1; } elseif ($token === '}') { $contextBrackets -= 1; } if (!is_array($token)) { continue; } if ($token[0] === T_CLASS) { $context = 'class'; $contextBrackets = 0; } elseif ($context === 'class' && $contextBrackets === 1 && $token[0] === T_VARIABLE) { if ($token[1] === '$' . $property->getName()) { return $token[2]; } } } return NULL; }
function reflectProperty($class, $property) { $propInfo = new ReflectionProperty($class, $property); echo "**********************************\n"; echo "Reflecting on property {$class}::{$property}\n\n"; echo "__toString():\n"; var_dump($propInfo->__toString()); echo "export():\n"; var_dump(ReflectionProperty::export($class, $property, true)); echo "export():\n"; var_dump(ReflectionProperty::export($class, $property, false)); echo "getName():\n"; var_dump($propInfo->getName()); echo "isPublic():\n"; var_dump($propInfo->isPublic()); echo "isPrivate():\n"; var_dump($propInfo->isPrivate()); echo "isProtected():\n"; var_dump($propInfo->isProtected()); echo "isStatic():\n"; var_dump($propInfo->isStatic()); $instance = new $class(); if ($propInfo->isPublic()) { echo "getValue():\n"; var_dump($propInfo->getValue($instance)); $propInfo->setValue($instance, "NewValue"); echo "getValue() after a setValue():\n"; var_dump($propInfo->getValue($instance)); } echo "\n**********************************\n"; }
public function getName() { if (isset($this->_name)) { return $this->_name; } return parent::getName(); }
private function getPropertyParserBuilder(\ReflectionProperty $property) { $propertyParserBuilder = new PropertyParserBuilder($property->getName()); foreach ($this->annotationReader->getAnnotationsFromProperty($property) as $propertyAnnotation) { $this->annotationParserFactory->getAnnotationParserFor($propertyAnnotation)->execute($propertyAnnotation, $propertyParserBuilder); } return $propertyParserBuilder; }
/** * @param object $object * @param \ReflectionProperty $property * @param array $record */ private function setPropertyValueIfPossible($object, \ReflectionProperty $property, array $record) { $member = $property->getName(); if (isset($record[$member])) { $property->setAccessible(true); $property->setValue($object, $record[$member]); } }
/** * {@inheritdoc} */ public function match(\ReflectionProperty $property, $givenName) { $propertyName = $property->getName(); if ($propertyName === $givenName || $propertyName === $this->camelize($givenName) || $propertyName === lcfirst($this->camelize($givenName)) || $propertyName === strtolower($this->camelize($givenName)) || strtolower($propertyName) === strtolower($givenName)) { return true; } return false; }
/** * {@inheritdoc} * @throws PropertyDefinitionNotFoundException */ public function analyze(DefinitionAnalyzer $analyzer, ClassDefinition $classDefinition, \ReflectionProperty $reflectionProperty) { $propertyName = $reflectionProperty->getName(); // Set property metadata if ($classDefinition->hasProperty($propertyName)) { $classDefinition->getProperty($propertyName)->setIsPublic($reflectionProperty->isPublic())->setModifiers($reflectionProperty->getModifiers()); } }
/** * @param \ReflectionProperty $property * * @return \ReflectionMethod|null */ public function getSetter(\ReflectionProperty $property) { $setter = sprintf(static::SETTER_FORMAT, ucfirst($property->getName())); $class = $property->getDeclaringClass(); if ($class->hasMethod($setter)) { return $class->getMethod($setter); } }
/** * {@inheritdoc} */ public function getPropertyValue(\ReflectionProperty $property) { $name = $property->getName(); if (isset($this->values[$name])) { return $this->values[$name]; } return $this->fallback->getPropertyValue($property); }
/** * @param \ReflectionProperty $property * @param Builder $builder */ private function makeField(\ReflectionProperty $property, Builder $builder) { $field = $property->getName(); $resolved = TypeResolver::resolve($field); if (false !== $resolved) { list($type, $multiple, $options) = $resolved; $this->addFieldOfType($field, $type, $multiple, $options, $builder); } }
/** * Returns the name of the property. * * @return string property name */ public function getName() { if ($this->reflectionSource instanceof ReflectionProperty) { $name = $this->reflectionSource->getName(); } else { $name = parent::getName(); } return $name; }
private static function getValue(\ReflectionProperty $property, $object) { if (method_exists($object, '__get')) { $name = $property->getName(); return $object->{$name}; } $property->setAccessible(true); return $property->getValue($object); }
/** * @param \ReflectionProperty $property * @return Property */ public function property(\ReflectionProperty $property) { $phpyProperty = new Property($property->getName()); $phpyProperty->setStatic($property->isStatic()); $refClass = $property->getDeclaringClass(); $defClassValues = $refClass->getDefaultProperties(); if (isset($defClassValues[$property->getName()])) { $phpyProperty->setDefaultValue($defClassValues[$property->getName()]); } if ($property->isPublic()) { $phpyProperty->setVisibility('public'); } elseif ($property->isProtected()) { $phpyProperty->setVisibility('protected'); } else { $phpyProperty->setVisibility('private'); } return $phpyProperty; }
public function __construct(\ReflectionClass $reflectionClass, \ReflectionProperty $reflectionProperty) { $reflectionProperty->setAccessible(true); if (strpos($reflectionProperty->getDocComment(), "@skipSerialization") !== false) { $this->isSkipped = true; } if (strpos($reflectionProperty->getDocComment(), "@var") === false) { throw new PropertyTypeWasNotDefined($reflectionClass->getName(), $reflectionProperty->getName()); } if (preg_match('/@var\\s+([^\\s]+)/', $reflectionProperty->getDocComment(), $matches)) { list(, $type) = $matches; $types = explode("|", $type); if (!empty($types)) { foreach ($types as $type) { if ($pos = strpos($type, '[]')) { $this->isArray = true; $type = substr($type, 0, $pos); } if (class_exists($type)) { // Object $this->types[] = $type; $typeReflectionClass = new \ReflectionClass($type); if (!$typeReflectionClass->isInterface() && !$typeReflectionClass->isAbstract()) { $this->isObject = $type; } } else { $typeCheck = $reflectionClass->getNamespaceName() . '\\' . $type; if (class_exists($typeCheck)) { // Object $this->types[] = $typeCheck; $typeReflectionClass = new \ReflectionClass($typeCheck); if (!$typeReflectionClass->isInterface() && !$typeReflectionClass->isAbstract()) { $this->isObject = $typeCheck; } } else { $aliases = $this->getAliases($reflectionClass->getFileName()); if (array_key_exists($type, $aliases) && class_exists($aliases[$type])) { $type = $aliases[$type]; // Object $this->types[] = $type; $typeReflectionClass = new \ReflectionClass($type); if (!$typeReflectionClass->isInterface() && !$typeReflectionClass->isAbstract()) { $this->isObject = $type; } } else { $this->types[] = $type = null; } } } } array_unique($this->types); } } $this->reflectionClass = $reflectionClass; $this->reflectionProperty = $reflectionProperty; }
/** * @param object|NULL * @return AccessBase $this */ public function asInstance($object) { if (is_object($object)) { if ($this->reflection instanceof ReflectionClass) { $class = $this->reflection->getName(); } else { $class = $this->reflection->getDeclaringClass()->getName(); } if (!$object instanceof $class) { throw new Exception('Must be instance of accessible class.'); } } else { if ($object !== NULL) { throw new Exception('Instance must be object or NULL.'); } } $this->instance = $object; return $this; }
/** * {@inheritDoc} */ public function setValue(\ReflectionProperty $property, $object, $value) { $propertyName = $property->getName(); $declaringClass = $property->getDeclaringClass(); $setterName = sprintf('set%s', ucfirst($propertyName)); if (!$declaringClass->hasMethod($setterName)) { throw new \RuntimeException('No setter found for "' . $propertyName . '"'); } $declaringClass->getMethod($setterName)->invoke($object, $value); }
/** * @return self */ public static function from(\ReflectionProperty $from) { $prop = new static($from->getName()); $defaults = $from->getDeclaringClass()->getDefaultProperties(); $prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL; $prop->static = $from->isStatic(); $prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public'); $prop->comment = $from->getDocComment() ? preg_replace('#^\\s*\\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; return $prop; }
public static function match(\ReflectionProperty $property, $matches = array()) { $comment = static::get($property->getDeclaringClass()->getName(), $property->getName()); foreach ($matches as $match) { if (preg_match("|{$match}|", $comment)) { return true; } } return false; }
private function parseDoc(\ReflectionProperty $p) { $isAKey = false; $matches = array(); preg_match_all('/@rk (.*)/', $p->getDocComment(), $matches); if (count($matches) == 2 && !empty($matches[1])) { if ($matches[1][0][0] == '\\') { $matches[1][0] = substr($matches[1][0], 1); } $this->rkeys[$p->getName()] = $matches[1][0]; $this->instance->isARKey($p->getName()); $isAKey = true; } $matches = array(); preg_match_all('/@fk (.*)/', $p->getDocComment(), $matches); if (count($matches) == 2 && !empty($matches[1])) { if ($matches[1][0][0] == '\\') { $matches[1][0] = substr($matches[1][0], 1); } $this->fkeys[$p->getName()] = $matches[1][0]; $isAKey = true; } $matches = array(); preg_match_all('/@notshow/', $p->getDocComment(), $matches); if (in_array('@notshow', $matches[0])) { $this->notshow[] = $p->getName(); } $matches = array(); preg_match_all('/@setter (.*)/', $p->getDocComment(), $matches); if (count($matches) == 2 && !empty($matches[1])) { $this->setters[$p->getName()] = $matches[1][0]; $isAKey = true; } else { if (count($matches) == 1 && !empty($matches[0])) { $this->setters[$p->getName()] = $matches[0][0]; $isAKey = false; } } $matches = array(); preg_match_all('/@getter (.*)/', $p->getDocComment(), $matches); if (count($matches) == 2 && !empty($matches[1])) { $this->getters[$p->getName()] = $matches[1][0]; $isAKey = true; } $matches = array(); preg_match_all('/@adder (.*)/', $p->getDocComment(), $matches); if (count($matches) == 2 && !empty($matches[1])) { $this->adders[$p->getName()] = $matches[1][0]; $isAKey = true; } return $isAKey; }