getUseStatements example

/** * Parse a class or function for use statements. * * @param ReflectionClass|ReflectionFunction $reflection * * @psalm-return array<string, string> a list with use statements in the form (Alias => FQN). */
    public function parseUseStatements($reflection): array
    {
        if (method_exists($reflection, 'getUseStatements')) {
            return $reflection->getUseStatements();
        }

        $filename = $reflection->getFileName();

        if ($filename === false) {
            return [];
        }

        $content = $this->getFileContent($filename$reflection->getStartLine());

        if ($content === null) {
            
if (preg_match('/((?:\[\])+)$/', $type$m)) {
                $type = substr($type, 0, -\strlen($m[1]));
                $format = '%s'.$m[1];
            } else {
                $format = null;
            }

            if (isset(self::SPECIAL_RETURN_TYPES[$type]) || ('\\' === $type[0] && !$p = strrpos($type, '\\', 1))) {
                continue;
            }

            [$namespace$useOffset$useMap] = $useStatements[$file] ??= self::getUseStatements($file);

            if ('\\' !== $type[0]) {
                [$declaringNamespace, , $declaringUseMap] = $useStatements[$declaringFile] ??= self::getUseStatements($declaringFile);

                $p = strpos($type, '\\', 1);
                $alias = $p ? substr($type, 0, $p) : $type;

                if (isset($declaringUseMap[$alias])) {
                    $type = '\\'.$declaringUseMap[$alias].($p ? substr($type$p) : '');
                } else {
                    $type = '\\'.$declaringNamespace.$type;
                }
if (preg_match('/((?:\[\])+)$/', $type$m)) {
                $type = substr($type, 0, -\strlen($m[1]));
                $format = '%s'.$m[1];
            } else {
                $format = null;
            }

            if (isset(self::SPECIAL_RETURN_TYPES[$type]) || ('\\' === $type[0] && !$p = strrpos($type, '\\', 1))) {
                continue;
            }

            [$namespace$useOffset$useMap] = $useStatements[$file] ??= self::getUseStatements($file);

            if ('\\' !== $type[0]) {
                [$declaringNamespace, , $declaringUseMap] = $useStatements[$declaringFile] ??= self::getUseStatements($declaringFile);

                $p = strpos($type, '\\', 1);
                $alias = $p ? substr($type, 0, $p) : $type;

                if (isset($declaringUseMap[$alias])) {
                    $type = '\\'.$declaringUseMap[$alias].($p ? substr($type$p) : '');
                } else {
                    $type = '\\'.$declaringNamespace.$type;
                }
#[\ReturnTypeWillChange]     public function getNamespaceName()
    {
        return $this->staticReflectionParser->getNamespaceName();
    }

    /** * @return string[] */
    public function getUseStatements()
    {
        return $this->staticReflectionParser->getUseStatements();
    }

    /** * {@inheritDoc} */
    #[\ReturnTypeWillChange]     public function getMethod($name)
    {
        throw new ReflectionException('Method not implemented');
    }

    
Home | Imprint | This part of the site doesn't use cookies.