Retrieve annotations from docblock of a given reflector

@param  \Reflector                                             $Reflection Reflector object
@return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
170117
public function getAnnotations(\Reflector $Reflection)
    {
        $doc = $Reflection->getDocComment();
        if ($this->cache) {
            $key = $this->cache->getKey($doc);
            $ast = $this->cache->get($key);
            if (! $ast) {
                $ast = $this->parser->parse($doc);
                $this->cache->set($key, $ast);
            }
        } else {
            $ast = $this->parser->parse($doc);
        }

        return new AnnotationsBag($ast);
    }
1222342
public function getDependencies(\ReflectionClass $reflected)
    {
        $dependencies = [];

        if ($constructor = $reflected->getConstructor()) {
            $parameters = $constructor->getParameters();
            foreach ($parameters as $parameter) {
                $dependencies[] = $this->getDependency($parameter, $reflected);
            }
        }

        return $dependencies;
    }
901187
public function getClassAnnotations($className, $annotationClassName = null)
    {
        $className = $this->prepareClassReflectionForUsage($className);

        $annotationClassName = $annotationClassName === null ? null : $this->cleanClassName($annotationClassName);
        if (!isset($this->classReflectionData[$className][self::DATA_CLASS_ANNOTATIONS])) {
            return [];
        }
        if ($annotationClassName === null) {
            return $this->classReflectionData[$className][self::DATA_CLASS_ANNOTATIONS];
        }

        $annotations = [];
        foreach ($this->classReflectionData[$className][self::DATA_CLASS_ANNOTATIONS] as $annotation) {
            if ($annotation instanceof $annotationClassName) {
                $annotations[] = $annotation;
            }
        }

        return $annotations;
    }
463671
public static function extractProperties(\ReflectionClass $reflector, $type = \ReflectionProperty::IS_PROTECTED, $pattern = self::INJECTABLE_PATTERN)
    {
        $properties = [];
        foreach ($reflector->getProperties($type) as $property) {
            $doc = $property->getDocComment();
            if (preg_match($pattern, $doc)) {
                $instanceType = self::extractVarType($property->getDocComment());
                if (null !== $instanceType) {
                    $properties[$property->getName()] = $instanceType;
                }
            }
        }
        return $properties;
    }
567482
public static function getMethodsAnnotations($className, $annotationClassName = null)
    {
        if(null === $annotationClassName)
        {
            return static::$annotations[$className]['Methods'] ?? [];
        }
        $result = [];
        foreach(static::$annotations[$className]['Methods'] ?? [] as $methodName => $annotations)
        {
            foreach($annotations as $annotation)
            {
                if($annotation instanceof $annotationClassName)
                {
                    $result[$methodName][] = $annotation;
                }
            }
        }
        return $result;
    }
1047140
public function extract(\Reflector $reflectionProperty)
    {
        if (!$reflectionProperty instanceof \ReflectionProperty) {
            throw new ExtractorException(\ReflectionProperty::class, $reflectionProperty);
        }

        return $this->reader->getPropertyAnnotations($reflectionProperty);
    }
1369886
protected function getCmsDocBlockTags(ReflectionMethod $method)
    {
        if ( ! ($docBlock = $method->getDocComment())) {
            return [];
        }

        $factory = DocBlockFactory::createInstance();
        $doc     = $factory->create( $docBlock );

        $tags = $doc->getTagsByName('cms');

        if ( ! $tags || ! count($tags)) {
            return [];
        }

        $cmsTags = [];

        foreach ($tags as $tag) {

            if ( ! method_exists($tag, 'getDescription')) {
                // @codeCoverageIgnoreStart
                continue;
                // @codeCoverageIgnoreEnd
            }

            $description = trim($tag->getDescription());

            if (preg_match('#\s+#', $description)) {
                list($firstWord, $parameters) = preg_split('#\s+#', $description, 2);
            } else {
                $firstWord  = $description;
                $parameters = '';
            }

            $firstWord = strtolower($firstWord);

            if ($firstWord == 'relation') {
                $cmsTags['relation'] = true;
                continue;
            }

            if ($firstWord == 'ignore') {
                $cmsTags['ignore'] = true;
                continue;
            }

            if ($firstWord == 'morph') {
                if ( ! $parameters) continue;

                $models = array_map('trim', explode(',', $parameters));

                if ( ! array_key_exists('morph', $cmsTags)) {
                    $cmsTags['morph'] = [];
                }

                $cmsTags['morph'] = array_merge($cmsTags['morph'], $models);
                $cmsTags['morph'] = array_unique($cmsTags['morph']);
                continue;
            }
        }

        return $cmsTags;
    }
1298640
public function getRules(Reflector $reflection): array
    {
        if ($reflection instanceof ReflectionMethod) {
            $key = $reflection->getDeclaringClass()->getName().'::'.$reflection->getName();
        } elseif ($reflection instanceof ReflectionClass) {
            $key = $reflection->getName();
        } elseif ($reflection instanceof ReflectionProperty) {
            $key = $reflection->getDeclaringClass()->getName().'::$'.$reflection->getName();
        } else {
            throw new InvalidArgumentException('Reflection must be an instance of either ReflectionMethod, ReflectionClass or ReflectionProperty.');
        }

        if (!isset($this->cache[$key])) {
            $this->cache[$key] = $this->ruleProvider->getRules($reflection);
        }

        return $this->cache[$key];
    }
107048
public function parseFactoryAnnotations($docBlock)
    {
        return $this->parseAnnotations(
            $docBlock,
            '/@factory\("(?P<className>[\/\\\\A-Za-z0-9_-]+?)"(, params={(?P<params>[, \"\n\t*A-Za-z0-9_-]*)})?\)?[ \t]*\r?$/m',
            function (array $matches) {
                if (1 != count($matches[0])) {
                    throw new \InvalidArgumentException();
                }
                current($matches);

                // clean params
                $params = null;
                if (!empty($matches['params'][0])) {
                    $params = $matches['params'][0];
                    foreach (['*', ' ', "\n", "\t", "\r", '"'] as $char) {
                        $params = str_replace($char, '', $params);
                    }
                }

                return [
                    'className' => $matches['className'][0],
                    'params'    => !empty($params)? explode(',', $params):[],
                ];
            }
        );
    }
1156844
protected function readAllAnnotationsFor($reflection)
    {
        // Construct Method to be used for reading.
        $methodName = $this->constructReadMethod();

        // Read, Collect and return annotations.
        return collect($this->reader->{$methodName}($reflection));
    }
317525
private function parseAnnotations($docBlock, $pattern, callable $callback)
    {
        $annotations = [];
        // Strip away the docblock header and footer to ease parsing of one line annotations
        $docBlock = substr($docBlock, 3, -2);

        if (preg_match_all($pattern, $docBlock, $matches) && is_callable($callback)) {
            return call_user_func($callback, $matches);
        }

        return $annotations;
    }
960120
public function extract(\Reflector $reflectionMethod)
    {
        if (!$reflectionMethod instanceof \ReflectionMethod) {
            throw new ExtractorException(\ReflectionMethod::class, $reflectionMethod);
        }

        return $this->reader->getMethodAnnotations($reflectionMethod);
    }
880163
private function getDeclaringClass(\Reflector $reflector) : ?\ReflectionClass
    {
        if ($reflector instanceof \ReflectionClass) {
            return $reflector;
        }

        if ($reflector instanceof \ReflectionProperty) {
            return $reflector->getDeclaringClass();
        }

        if ($reflector instanceof \ReflectionMethod) {
            return $reflector->getDeclaringClass();
        }

        if ($reflector instanceof \ReflectionParameter) {
            return $reflector->getDeclaringClass();
        }

        return null;
    }
1141869
protected static function parseAnnotations($docblock)
    {
        $annotations = array();
        // Strip away the docblock header and footer
        // to ease parsing of one line annotations
        $docblock = substr($docblock, 3, -2);

        $re = '/@(?P<name>[A-Za-z_-]+)(?:[ \t]+(?P<value>.*?))?[ \t]*\r?$/m';
        if (preg_match_all($re, $docblock, $matches)) {
            $numMatches = count($matches[0]);

            for ($i = 0; $i < $numMatches; ++$i) {
                $annotations[$matches['name'][$i]][] = $matches['value'][$i];
            }
        }

        return $annotations;
    }
681988
public function extractRelations(\ReflectionClass $reflection)
    {
        $propertyList = $reflection->getDefaultProperties();

        if ($this->cache && $res = $this->cache->get(md5(json_encode($propertyList)))) {
            return $res;
        }

        $relation = [];

        foreach ($propertyList as $name => $value) {
            $comment = $reflection->getProperty($name)->getDocComment();
            $d = $this->parser->parse($comment);
            if (!$d) {
                continue;
            }
            $relation[] = [
                'mapping_id' => $d[0],
                'model'      => $d[1],
                'target'     => $name
            ];
        }

        if ($this->cache) {
            $this->cache->set(md5(json_encode($propertyList)), $relation, 300);
        }

        return $relation;
    }
955335
public static function fetchAllAnnotationsFromDocblock($docblock)
    {
        $annotations = [];
        if (!preg_match_all(sprintf(self::$regex, '(\w+)'), $docblock, $matched)) {
            return $annotations;
        }
        foreach ($matched[1] as $k => $annotation) {
            if (!isset($annotations[$annotation])) {
                $annotations[$annotation] = [];
            }
            $annotations[$annotation][] = $matched[2][$k];
        };
        return $annotations;
    }
1343812
private function parseMethods($docType = '') {
        foreach ($this->reflector->getMethods() as $refMeth) {
            $this->annotations['methods'][$refMeth->name] = $this->parseDocComment($refMeth->getDocComment(), $docType);
        }
    }