Parses a class for constants and DocComments

@param \ReflectionClass $oReflectionClass
484997
protected static function parse(\ReflectionClass $oReflectionClass)
    {
        $sContent = file_get_contents($oReflectionClass->getFileName());
        $aTokens  = token_get_all($sContent);

        $sDocComment = null;
        $bIsConst    = false;
        foreach ($aTokens as $aToken) {
            if (!is_array($aToken) || count($aToken) <= 1) {
                continue;
            }

            list($iTokenType, $sTokenValue) = $aToken;

            switch ($iTokenType) {
                // Ignored tokens
                case T_WHITESPACE:
                case T_COMMENT:
                    break;

                case T_DOC_COMMENT:
                    $sDocComment = $sTokenValue;
                    break;

                case T_CONST:
                    $bIsConst = true;
                    break;

                case T_STRING:
                    if ($bIsConst) {
                        static::$aDescriptions[$sTokenValue] = self::extractDescription($sDocComment);
                        static::$aArguments[$sTokenValue]    = self::extractArgument($sDocComment);
                    }
                    $sDocComment = null;
                    $bIsConst    = false;
                    break;

                // All other tokens reset the parser
                default:
                    $sDocComment = null;
                    $bIsConst    = false;
                    break;
            }
        }
    }
946986
public function parseConst(\ReflectionClass $ref, \ReflectionClassConstant $const)
    {
        $annotations = $this->reader->getConstantAnnotations($const);
        if($this->checkAnnotations($annotations))
        {
            $this->data[$ref->getName()]['const'][$const->getName()] = $this->fileMap[$ref->getFileName()][$ref->getName()]['const'][$const->getName()] = &$annotations;
        }
    }
776405
private function getPhpFileStmt(\ReflectionClass $class) : array
    {
        $traverser = new NodeTraverser();
        $visitor = new CodeGenVisitor();
        $traverser->addVisitor($visitor);
        $fileName = $class->getFileName();
        if (is_bool($fileName)) {
            throw new InvalidSourceClassException(get_class($class));
        }
        $file = file_get_contents($fileName);
        if ($file === false) {
            throw new \RuntimeException($fileName); // @codeCoverageIgnore
        }
        $stmts = $this->parser->parse($file);
        if (is_array($stmts)) {
            $traverser->traverse($stmts);
        }

        return $visitor();
    }
448833
public function loadMetadataForClass(\ReflectionClass $class)
    {
        $classMetadata = new ClassMetadata($class->name);
        $classMetadata->fileResources[] = $class->getFileName();

        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
            if ($annotation instanceof NamespaceNode) {
                $classMetadata->addGraphNamespace($annotation);
            }

            if ($annotation instanceof GraphNode) {
                $classMetadata->addGraphMetadata($annotation, new MetadataValue($annotation->value));
            }
        }

        foreach ($class->getProperties() as $property) {
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
                if ($annotation instanceof GraphNode) {
                    $classMetadata->addGraphMetadata($annotation, new PropertyMetadata($class->name, $property->name));
                }
            }
        }

        foreach ($class->getMethods() as $method) {
            foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
                if ($annotation instanceof GraphNode) {
                    $classMetadata->addGraphMetadata($annotation, new MethodMetadata($class->name, $method->name));
                }
            }
        }

        return $classMetadata;
    }
795657
private function loadTranslatableMetadata(\ReflectionClass $class) {
        $classMetadata = new TranslatableMetadata($class->name);

        foreach ($class->getProperties() as $property) {
            if ($property->class !== $class->name) {
                continue;
            }

            $propertyMetadata = new PropertyMetadata($class->name, $property->getName());

            if ($this->reader->getPropertyAnnotation($property, 'Prezent\\Doctrine\\Translatable\\Annotation\\CurrentLocale')) {
                $classMetadata->currentLocale = $propertyMetadata;
                $classMetadata->addPropertyMetadata($propertyMetadata);
            }
            
            if ($this->reader->getPropertyAnnotation($property, 'Prezent\\Doctrine\\Translatable\\Annotation\\FallbackLocale')) {
                $classMetadata->fallbackLocale = $propertyMetadata;
                $classMetadata->addPropertyMetadata($propertyMetadata);
            }
            
            if ($annot = $this->reader->getPropertyAnnotation($property, 'Prezent\\Doctrine\\Translatable\\Annotation\\Translations')) {
                $classMetadata->targetEntity = $annot->targetEntity;
                $classMetadata->translations = $propertyMetadata;
                $classMetadata->addPropertyMetadata($propertyMetadata);
            }
        }

        return $classMetadata;
    }
357538
public function getGeneratorFromReflection(ClassReflection $classReflection)
  {
    // class generator
    $cg = new ClassGenerator($classReflection->getName());

    $cg->setSourceContent($cg->getSourceContent());
    $cg->setSourceDirty(false);

    if ($classReflection->getDocComment() != '')
    {
      $docblock
        = DocBlockGenerator::fromReflection($classReflection->getDocBlock());
      $docblock->setIndentation(Generator::$indentation);
      $cg->setDocBlock($docblock);
    }

    $cg->setAbstract($classReflection->isAbstract());

    // set the namespace
    if ($classReflection->inNamespace())
    {
      $cg->setNamespaceName($classReflection->getNamespaceName());
    }

    /* @var \Zend\Code\Reflection\ClassReflection $parentClass */
    $parentClass = $classReflection->getParentClass();
    if ($parentClass)
    {
      $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\'));
      $interfaces = array_diff($classReflection->getInterfaces(),
                               $parentClass->getInterfaces());
    }
    else
    {
      $interfaces = $classReflection->getInterfaces();
    }

    $interfaceNames = array();
    foreach ($interfaces as $interface)
    {
      /* @var \Zend\Code\Reflection\ClassReflection $interface */
      $interfaceNames[] = $interface->getName();
    }

    $cg->setImplementedInterfaces($interfaceNames);

    $properties = array();
    foreach ($classReflection->getProperties() as $reflectionProperty)
    {
      if (
        $reflectionProperty->getDeclaringClass()->getName()
        == $classReflection->getName()
      )
      {
        $property = PropertyGenerator::fromReflection($reflectionProperty);
        $property->setIndentation(Generator::$indentation);
        $properties[] = $property;
      }
    }
    $cg->addProperties($properties);

    $methods = array();
    foreach ($classReflection->getMethods() as $reflectionMethod)
    {
      $className
        = ($cg->getNamespaceName()) ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName();
      if ($reflectionMethod->getDeclaringClass()->getName() == $className)
      {
        $method = MethodGenerator::fromReflection($reflectionMethod);
        $method->setBody(
          preg_replace(
            "/^" . Generator::$indentation . Generator::$indentation . "/m",
            '',
            $method->getBody()
          )
        );
        $method->setIndentation(Generator::$indentation);
        $methods[] = $method;
      }
    }
    $cg->addMethods($methods);

    return $cg;
  }
819196
protected function _createOptionsFromPublic(\ReflectionClass $class)
  {
    $properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
    if(empty($properties))
    {
      return null;
    }
    foreach($properties as $property)
    {
      $propBlock = new DocBlockParser($property->getDocComment());
      $short = null;
      $description = $propBlock->getSummary();
      $mode = InputOption::VALUE_OPTIONAL;
      if($propBlock->hasTag('short'))
      {
        $short = $propBlock->getTag('short');
      }
      if($propBlock->hasTag('description'))
      {
        $description = $propBlock->getTag('description');
      }
      if($propBlock->hasTag('valuerequired'))
      {
        $mode = InputOption::VALUE_REQUIRED;
      }
      if($propBlock->hasTag('flag'))
      {
        $mode = InputOption::VALUE_NONE;
      }
      $this->addOption($property->name, $short, $mode, $description, $property->getValue($this));
    }
    return null;
  }
422687
protected function processPropertiesParsing(\ReflectionClass $reflectedClass)
    {
        $metadata = array();

        $reflectedProperties = $reflectedClass->getProperties();

        foreach ($reflectedProperties as $reflectedProperty) {
            if ($this->isBoomgoProperty($reflectedProperty)) {
                $propertyMetadata = $this->parseMetadataProperty($reflectedProperty);
                $metadata['definitions'][$reflectedProperty->getName()] = $propertyMetadata;
            }
        }

        return $metadata;
    }
365566
private function getDoctrineMetadata($entityClass)
    {
        if (!isset($this->doctrineMetadata[$entityClass])) {
            try {
                $this->doctrineMetadata[$entityClass] = $this->em->getClassMetadata($entityClass);
            } catch (MappingException $ex) {
                throw new FiltererException(sprintf('Unable to get Doctrine metadata for class "%s".', $entityClass));
            }
        }

        return $this->doctrineMetadata[$entityClass];
    }
1215001
public function loadMetadataForClass($className, ClassMetadataInterface $class)
    {
        /** @var $class ClassMetadata */
        $reflection     = $class->getReflectionClass();
        $documentAnnots = [];
        $indexes        = [];

        //class annotations
        $classAnnotations = $this->reader->getClassAnnotations($reflection);
        foreach ($classAnnotations as $annot) {
            //document annotations
            $classAnnotations[get_class($annot)] = $annot;

            foreach ($this->entityAnnotationClasses as $annotClass => $i) {
                if ($annot instanceof $annotClass) {
                    $documentAnnots[$i] = $annot;
                    continue 2;
                }
            }

            //non-document class annotations
            if ($annot instanceof ODM\Discriminator) {
                if ($annot->attribute !== null) {
                    $class->setDiscriminatorAttribute($annot->attribute);
                }
                if ($annot->map !== null) {
                    $class->setDiscriminatorMap($annot->map);
                }
                if ($annot->defaultValue !== null) {
                    $class->setDiscriminatorDefaultValue($annot->defaultValue);
                }
            } elseif ($annot instanceof ODM\InheritanceType) {
                $class->setInheritanceType(constant(ClassMetadata::class . '::INHERITANCE_TYPE_' . $annot->value));
            } elseif ($annot instanceof ODM\Index) {
                $indexes[$annot->name]['projectionType'] = $annot->projectionType;
                $indexes[$annot->name]['readCapacity']   = $annot->readCapacity;
                $indexes[$annot->name]['writeCapacity']  = $annot->writeCapacity;
            } elseif ($annot instanceof ODM\ChangeTrackingPolicy) {
                $class->setChangeTrackingPolicy(constant(ClassMetadata::class . '::CHANGETRACKING_' . $annot->value));
            }
        }

        if (!$documentAnnots) {
            throw new MappingExceptionClassNotMapped($className);
        }

        //find the winning document annotation
        ksort($documentAnnots);
        $documentAnnot = reset($documentAnnots);

        if ($documentAnnot instanceof ODM\MappedSuperclass) {
            $class->isMappedSuperclass = true;
        } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) {
            $class->isEmbeddedDocument = true;
        }
        if (isset($documentAnnot->table)) {
            $class->setTable($documentAnnot->table);
        }
        if (isset($documentAnnot->repositoryClass) && !$class->isEmbeddedDocument) {
            $class->setCustomRepositoryClass($documentAnnot->repositoryClass);
        }
        if (isset($documentAnnot->requireIndexes)) {
            $class->setRequireIndexes($documentAnnot->requireIndexes);
        }
        if (!empty($documentAnnot->readOnly)) {
            $class->markReadOnly();
        }

        //property annotations
        $propertyFactory = new PropertyMetadataFactory($class);
        foreach ($reflection->getProperties() as $property) {
            if (!$this->propertyNeedsMapping($class, $property)) {
                continue;
            }

            $mapping        = $propertyFactory->create($property->getName());
            $attributeAnnot = null;

            foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
                if ($annot instanceof ODM\AbstractAttribute) {
                    $attributeAnnot         = true;
                    $mapping->attributeName = $annot->name;

                    if ($annot instanceof ODM\NotSaved) {
                        $mapping->notSaved = true;
                        continue;
                    }

                    $mapping->nullable = $annot->nullable;
                    $mapping->strategy = $annot->strategy;
                    $mapping->type     = $annot->type;

                    if ($class->isEmbeddedDocument && $mapping->strategy === PropertyMetadata::STORAGE_STRATEGY_ADD_TO_SET) {
                        throw MappingException::addToSetOnEmbeddedDocument($class->name, $property->getName());
                    }

                    if ($annot instanceof ODM\AbstractEmbed) {
                        $mapping->embedded       = $annot->embedded;
                        $mapping->targetDocument = $annot->targetDocument;

                        if ($annot instanceof ODM\EmbedMany) {
                            $mapping->collectionClass = $annot->collectionClass;
                        }
                    } elseif ($annot instanceof ODM\AbstractReference) {
                        $mapping->criteria         = $annot->criteria;
                        $mapping->inversedBy       = $annot->inversedBy;
                        $mapping->mappedBy         = $annot->mappedBy;
                        $mapping->mappedIndex      = $annot->mappedIndex;
                        $mapping->orphanRemoval    = $annot->orphanRemoval;
                        $mapping->reference        = $annot->reference;
                        $mapping->requireIndexes   = $annot->requireIndexes;
                        $mapping->repositoryMethod = $annot->repositoryMethod;
                        $mapping->storeAs          = $annot->storeAs;
                        $mapping->targetDocument   = $annot->targetDocument;

                        if ($annot instanceof ODM\ReferenceMany) {
                            $mapping->limit           = $annot->limit;
                            $mapping->prime           = $annot->prime;
                            $mapping->collectionClass = $annot->collectionClass;

                            if ($annot->order === PropertyMetadata::ORDER_REVERSE) {
                                $mapping->reverse = true;
                            }
                        }

                        $mapping->setCascades((array)$annot->cascade);
                    }
                } elseif ($annot instanceof ODM\HashKey) {
                    $indexes[$annot->index]['hashKey'] = new HashKey($mapping);
                } elseif ($annot instanceof ODM\RangeKey) {
                    $indexes[$annot->index]['rangeKey'] = new RangeKey($mapping);
                } elseif ($annot instanceof ODM\Project) {
                    $value = (array)$annot->value;

                    if (count($value) === 0) {
                        //if no name is specified the primary index is assumed
                        $indexes[null]['projectionAttributes'][] = $property->getName();
                    } else {
                        foreach ($value as $indexName) {
                            $indexes[$indexName]['projectionAttributes'][] = $property->getName();
                        }
                    }
                } elseif ($annot instanceof ODM\GeneratedValue) {
                    $mapping->generatorStrategy = $annot->strategy;
                    $mapping->generatorOptions  = $annot->options;
                } elseif ($annot instanceof ODM\Discriminator) {
                    $mapping->discriminatorAttribute    = $annot->attribute;
                    $mapping->discriminatorMap          = $annot->map;
                    $mapping->discriminatorDefaultValue = $annot->defaultValue;
                } elseif ($annot instanceof ODM\AlsoLoad) {
                    $mapping->alsoLoadAttributes = (array)$annot->value;
                } elseif ($annot instanceof ODM\Version) {
                    $mapping->version = true;
                } elseif ($annot instanceof ODM\Lock) {
                    $mapping->lock = true;
                } elseif ($annot instanceof ODM\ChangeTrackingPolicy) {
                    $class->setChangeTrackingPolicy(constant(ClassMetadata::class . '::CHANGETRACKING_' . $annot->value));
                }
            }

            if ($attributeAnnot) {
                $mapping->mapAttribute($class);
            }
        }

        //method annotations
        foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
            /* Filter for the declaring class only. Callbacks from parent
             * classes will already be registered.
             */
            if ($method->getDeclaringClass()->name !== $reflection->name) {
                continue;
            }

            foreach ($this->reader->getMethodAnnotations($method) as $annot) {
                if ($annot instanceof ODM\AlsoLoad) {
                    $class->registerAlsoLoadMethod($method->getName(), $annot->value);
                }

                if (!isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) {
                    continue;
                }

                if ($annot instanceof ODM\PrePersist) {
                    $class->addLifecycleCallback($method->getName(), Event::prePersist);
                } elseif ($annot instanceof ODM\PostPersist) {
                    $class->addLifecycleCallback($method->getName(), Event::postPersist);
                } elseif ($annot instanceof ODM\PreUpdate) {
                    $class->addLifecycleCallback($method->getName(), Event::preUpdate);
                } elseif ($annot instanceof ODM\PostUpdate) {
                    $class->addLifecycleCallback($method->getName(), Event::postUpdate);
                } elseif ($annot instanceof ODM\PreRemove) {
                    $class->addLifecycleCallback($method->getName(), Event::preRemove);
                } elseif ($annot instanceof ODM\PostRemove) {
                    $class->addLifecycleCallback($method->getName(), Event::postRemove);
                } elseif ($annot instanceof ODM\PreLoad) {
                    $class->addLifecycleCallback($method->getName(), Event::preLoad);
                } elseif ($annot instanceof ODM\PostLoad) {
                    $class->addLifecycleCallback($method->getName(), Event::postLoad);
                } elseif ($annot instanceof ODM\PreFlush) {
                    $class->addLifecycleCallback($method->getName(), Event::preFlush);
                }
            }
        }

        //indexes
        $indexFactory = new IndexFactory($class);
        foreach ($indexes as $name => $annot) {
            $primary = $name === '';

            //create index, merging values collected from annotations with defaults
            $opts                  = array_fill_keys(['hashKey', 'rangeKey', 'projectionType', 'projectionAttributes'], null);
            $opts['readCapacity']  = IndexFactory::DEFAULT_READ_CAPACITY;
            $opts['writeCapacity'] = IndexFactory::DEFAULT_WRITE_CAPACITY;
            $opts                  = array_merge($opts, $annot);

            $index = $indexFactory->create(
                $primary ? null : $name,
                $opts['hashKey'],
                $opts['rangeKey'],
                $opts['projectionType'],
                $opts['projectionAttributes'],
                $opts['readCapacity'],
                $opts['writeCapacity']
            );

            if ($primary === true) {
                $class->setIndex($index);
            } else {
                $class->addSecondaryIndex($index);
            }
        }
    }
1244645
private function getClassTranslation(\ReflectionClass $classReflection, $parseDocComments)
    {
        if ($parseDocComments) {
            $translation = $this->parseTranslationFromDocComment($classReflection->getDocComment());

            if (!empty($translation)) {
                return $translation;
            }
        }

        return StringsUtil::humanize($this->entityNamer->name($classReflection->getName()));
    }
1335896
function constants($class,$prefix='') {
		$ref=new ReflectionClass($class);
		$out=array();
		foreach (preg_grep('/^'.$prefix.'/',array_keys($ref->getconstants()))
			as $val) {
			$out[$key=substr($val,strlen($prefix))]=
				constant((is_object($class)?get_class($class):$class).'::'.$prefix.$key);
		}
		unset($ref);
		return $out;
	}
833651
private function parse($docComment) {
		$lines = self::getDocLinesFromComment($docComment);
		
		// First, let's go to the first Annotation.
		// Anything before is the pure comment.
		$annotationLines = array();
		
		$oneAnnotationFound = false;
		// Is the line an annotation? Let's test this with a regexp.
		foreach ($lines as $line) {
			if (preg_match("/^[@][a-zA-Z]/", $line) === 0) {
				if ($oneAnnotationFound == false) {
					$this->comment .= $line."\n";
				}
			} else {
				$this->parseAnnotation($line);
				$oneAnnotationFound = true;
			}
		}
	}
301824
public function getConditions(ReflectionClass $class): array
    {
        $annotations   = [];
        $parents = [];

        $this->getParentClasses($class, $parents);
        $this->getInterfaces($class, $parents);

        foreach ($parents as $parent) {
            $annotations[] = $this->annotationReader->getClassAnnotations($parent);
        }

        if (\count($annotations)) {
            $annotations = \array_merge(...$annotations);
        }

        return $this->filterContractAnnotation($annotations);
    }
990979
private static function _encodeConstants(\ReflectionClass $cls)
    {
        $result    = "constants : {";
        $constants = $cls->getConstants();

        $tmpArray = array();
        if (!empty($constants)) {
            foreach ($constants as $key => $value) {
                $tmpArray[] = "$key: " . self::encode($value);
            }

            $result .= implode(', ', $tmpArray);
        }

        return $result . "}";
    }
824993
private static function getSetters(\ReflectionClass $reflectionClass)
    {
        foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
            if (!$reflectionMethod->isStatic() && 1 === $reflectionMethod->getNumberOfParameters() && 0 === strpos($reflectionMethod->name, 'set')) {
                yield $reflectionMethod;
            }
        }
    }
841691
protected function createInstance(\ReflectionClass $reflectedClass)
    {
        $constructor = $reflectedClass->getConstructor();

        if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) {
            throw new \RuntimeException('Unable to hydrate an object requiring constructor param');
        }

        return $reflectedClass->newInstance();
    }