Vanilla\Addon::scanFile PHP Method

scanFile() private static method

Looks what classes and namespaces are defined in a file and returns them.
See also: http://stackoverflow.com/a/11114724/1984219
private static scanFile ( string $path ) : array
$path string Path to file.
return array Returns an empty array if no classes are found or an array with namespaces and classes found in the file.
    private static function scanFile($path)
    {
        $classes = $nsPos = $final = [];
        $foundNamespace = false;
        $ii = 0;
        if (!file_exists($path)) {
            return [];
        }
        $er = error_reporting();
        error_reporting(E_ALL ^ E_NOTICE);
        $php_code = file_get_contents($path);
        $tokens = token_get_all($php_code);
        //        $count = count($tokens);
        foreach ($tokens as $i => $token) {
            //} ($i = 0; $i < $count; $i++) {
            if (!$foundNamespace && $token[0] == T_NAMESPACE) {
                $nsPos[$ii]['start'] = $i;
                $foundNamespace = true;
            } elseif ($foundNamespace && ($token == ';' || $token == '{')) {
                $nsPos[$ii]['end'] = $i;
                $ii++;
                $foundNamespace = false;
            } elseif ($i - 2 >= 0 && $tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $token[0] == T_STRING) {
                if ($i - 4 >= 0 && $tokens[$i - 4][0] == T_ABSTRACT) {
                    $classes[$ii][] = array('name' => $token[1], 'type' => 'ABSTRACT CLASS');
                } else {
                    $classes[$ii][] = array('name' => $token[1], 'type' => 'CLASS');
                }
            } elseif ($i - 2 >= 0 && $tokens[$i - 2][0] == T_INTERFACE && $tokens[$i - 1][0] == T_WHITESPACE && $token[0] == T_STRING) {
                $classes[$ii][] = array('name' => $token[1], 'type' => 'INTERFACE');
            } elseif ($i - 2 >= 0 && $tokens[$i - 2][0] == T_TRAIT && $tokens[$i - 1][0] == T_WHITESPACE && $token[0] == T_STRING) {
                $classes[$ii][] = array('name' => $token[1], 'type' => 'TRAIT');
            }
        }
        error_reporting($er);
        if (empty($classes)) {
            return [];
        }
        if (!empty($nsPos)) {
            foreach ($nsPos as $k => $p) {
                $ns = '';
                for ($i = $p['start'] + 1; $i < $p['end']; $i++) {
                    $ns .= $tokens[$i][1];
                }
                $ns = trim($ns);
                $final[$k] = array('namespace' => $ns, 'classes' => $classes[$k + 1]);
            }
            $classes = $final;
        }
        return $classes;
    }