Extracts an array of class names from tokenized output of token_get_all().

@param array $tokens

@return string[]
187807
protected function extractClassNames(array $tokens) : array {
    $classes = [];
    $namespace = $this->extractNamespace($tokens);
    foreach ($tokens as $id => $token) {
      if ($token[0] == T_CLASS) {
        $classes[] = $namespace ? $namespace . '\\' . $tokens[$id+2][1] : $tokens[$id+2][1];
      }
    }
    return $classes;
  }
1124919
public static function getAllChildClasses($class): array
    {
        $type = gettype($class);
        $className = '';

        /* If input an object */
        if ($type == 'object') {
            $className = get_class($class);
        }

        /* If input a string */
        elseif ($type == 'string') {
            $className = $class;
        }

        $classes = [];

        /* Go through all classes */
        foreach (get_declared_classes() as $class) {
            if (is_subclass_of($class, $className)) {
                $classes[] = $class;
            }
        }

        return $classes;
    }
1191315
private static function _extractClassName($cls)
    {
        $tokens = preg_split("/\\\\/", $cls);
        $size   = sizeof($tokens);
        if ($size < 2) {
            return $cls;
        }
        return $tokens[$size-1];
    }
1153640
public function getAllSearchableFieldsFor($classNames) {
		$allfields = array();
		foreach ($classNames as $className) {
			$fields = $this->getSearchableFieldsFor($className);
			$allfields = array_merge($allfields, $fields);
		}
		
		return $allfields;
	}
530777
private static function getName($key, array $tokens): ?string
    {
        $class      = null;
        $tokenCount = \count($tokens);

        for ($i = $key; $i < $tokenCount; $i++) {
            if (\is_array($tokens[$i])) {
                if ($tokens[$i][0] === \T_STRING) {
                    $class .= $tokens[$i][1];
                } elseif ($tokens[$i][0] === \T_WHITESPACE) {
                    return $class;
                }
            }
        }

        return null;
    }
193155
private static function classNameByTokens($phpCode)
    {
        $classes = [];
        $tokens = token_get_all($phpCode);
        $count = count($tokens);
        for ($i = 2; $i < $count; $i++) {
            if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
                $classes[] = $tokens[$i][1];
            }
        }
        
        return $classes;
    }
750087
private function getSubClasses($subClasses)
    {
        $result = [];

        foreach ($subClasses as $subClass) {
            $children = $subClass->getSubClasses(false);
            $entry = [
                'uri' => $subClass->getUri(),
                'label' => $subClass->getLabel()
            ];
            if (count($children) > 0) {
                $entry['children'] = $this->getSubClasses($children);
            }
            array_push($result, $entry);
        }

        return $result;
    }
133994
private function tokensToString(array $tokens): string
    {
        $lineCount = 0;
        $headerLines = [];
        $headerLines[] = $this->name.': ';
        $currentLine = &$headerLines[$lineCount++];

        // Build all tokens back into compliant header
        foreach ($tokens as $i => $token) {
            // Line longer than specified maximum or token was just a new line
            if (("\r\n" === $token) ||
                ($i > 0 && \strlen($currentLine.$token) > $this->lineLength)
                && 0 < \strlen($currentLine)) {
                $headerLines[] = '';
                $currentLine = &$headerLines[$lineCount++];
            }

            // Append token to the line
            if ("\r\n" !== $token) {
                $currentLine .= $token;
            }
        }

        // Implode with FWS (RFC 2822, 2.2.3)
        return implode("\r\n", $headerLines);
    }
1089078
private static function cleanClasses(array $classes): array
  {
    $ret = [];

    foreach ($classes as $class)
    {
      $tmp = Cast::toManString($class, '');
      if ($tmp!=='') $ret[] = $tmp;
    }

    return array_unique($ret);
  }
123532
private function extractNamespaces(array $tokens)
    {
        $closureNamespace = null;
        $closureUses = [];
        $state = null;
        $namespace = null;
        $alias = null;
        foreach ($tokens as $token) {
            if (is_array($token)) {
                if ($state === null && ($token[0] === T_NAMESPACE || $token[0] === T_USE)) {
                    $state = $token[0];
                    $namespace = '';
                } elseif ($state === T_USE && $alias === null && $token[0] === T_AS) {
                    $alias = '';
                } elseif ($state !== null && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
                    if ($alias !== null) {
                        $alias .= $token[1];
                    } else {
                        $namespace .= $token[1];
                    }
                }
            } else {
                if ($state !== null && ($token === ';' || $token === '{')) {
                    if ($alias === null) {
                        if ($state === T_NAMESPACE) {
                            $closureNamespace = $namespace;
                            $closureUses = [];
                        } else {
                            $closureUses[] = $namespace;
                        }
                    } else {
                        $closureUses[$alias] = $namespace;
                        $alias = null;
                    }
                    $state = null;
                    $namespace = null;
                } elseif ($state === T_USE && $token === ',') {
                    if ($alias === null) {
                        $closureUses[] = $namespace;
                    } else {
                        $closureUses[$alias] = $namespace;
                        $alias = null;
                    }
                    $namespace = null;
                }
            }
        }
        return [
            'namespace' => $closureNamespace,
            'uses' => $closureUses,
        ];
    }
712975
private function parseNamespace($tokens)
    {
        $namespace = null;
        $tokenCount = count($tokens);
        for ($offset=0; $offset < $tokenCount; $offset++) {
            if (!is_array($tokens[$offset])) {
                continue;
            }

            if (T_NAMESPACE === $tokens[$offset][0]) {
                $offset++; // the next token is a whitespace

                return $this->parseNamespaceString($tokens, $offset);
            }
        }

        return $namespace;
    }
451626
public function filterClassNames(array $statements)
    {
        $names = [];
        foreach ($this->filterClasses($statements) as $class) {
            $names[] = $class->name;
        }

        foreach ($this->filterNamespaces($statements) as $namespace) {
            foreach ($this->filterClasses($namespace->stmts) as $class) {
                $names[] = $namespace->name.'\\'.$class->name;
            }
        }

        return $names;
    }
741738
protected function getInput(array $tokens)
    {
        $var = '';
        $firstToken = \array_pop($tokens);
        if (self::tokenIs($firstToken, self::T_STRING)) {
            $var = $firstToken[1];
        }

        return $var;
    }
431597
private function getCurrentConstantKeyFromClassTokens($tokens)
    {
        $parsingStateConstDeclarationFound = false;
        $constDeclarationKey = 0;

        foreach ($tokens as $key => $token) {
            if (is_array($token) === true && $token[0] === T_CONST) {
                $parsingStateConstDeclarationFound = true;
                $constDeclarationKey = $key;
            }

            if ($parsingStateConstDeclarationFound === true && $token[0] === T_STRING) {
                if ($token[1] === $this->getName()) {
                    break;
                } else {
                    $parsingStateConstDeclarationFound = false;
                }
            }
        }

        return $constDeclarationKey;
    }
976251
private static function extractExtensions(array &$parts): array
    {
        $extensions = [];
        while (count($parts) > 1 && strlen($parts[0]) == 1 && strtolower($parts[0]) != 'x') {
            $extension = array_shift($parts);
            $extensions[$extension] = [];
            while (count($parts) > 0 && preg_match('/([a-zA-Z]{2,8})/', $parts[0])) {
                $extensions[$extension][] = array_shift($parts);
            }
        }

        return $extensions;
    }
617988
private function getTokens(string $source): array
    {
        $tokens = [];
        $line = 0;

        foreach (token_get_all($source) as $token) {
            if (isset($token[2])) {
                $line = $token[2];
            }

            if (!is_array($token)) {
                $token = [$token, $token, $line];
            }

            $tokens[] = $token;
        }

        return $tokens;
    }
966725
protected function extractNamespace(array $tokens) : string {
    $namespace = '';
    $found = FALSE;
    foreach ($tokens as $token) {
      if (is_array($token) && $token[0] == T_NAMESPACE) {
        $found = TRUE;
      }
      if (is_array($token) && ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING)) {
        $namespace .= $token[1];
      }
      elseif ($found && $token == ';') {
        return $namespace;
      }
    }
    return $namespace;
  }