Issues (462)

src/helpers/ClassMapGenerator.php (39 issues)

1
<?php
2
/*
0 ignored issues
show
You must use "/**" style comments for a file comment
Loading history...
3
 * This file is part of the Symfony package.
4
 *
5
 * (c) Fabien Potencier <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace nystudio107\units\helpers;
12
13
/**
14
 * ClassMapGenerator.
15
 *
16
 * @author Gyula Sallai <[email protected]>
17
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
18
class ClassMapGenerator
19
{
20
    /**
21
     * Generate a class map file.
22
     *
23
     * @param array|string $dirs Directories or a single path to search in
24
     * @param string $file The name of the class map file
0 ignored issues
show
Expected 7 spaces after parameter type; 1 found
Loading history...
25
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
26
    public static function dump($dirs, $file)
27
    {
28
        $dirs = (array)$dirs;
29
        $maps = [];
30
        foreach ($dirs as $dir) {
31
            $maps = array_merge($maps, static::createMap($dir));
32
        }
33
        file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
34
    }
35
36
    /**
37
     * Iterate over all files in the given directory searching for classes.
38
     *
39
     * @param \Iterator|string $dir The directory to search in or an iterator
40
     *
41
     * @return array A class map array
42
     */
43
    public static function createMap($dir): array
44
    {
45
        if (\is_string($dir)) {
46
            $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
47
        }
48
        $map = [];
49
        /** @var \Iterator $dir */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
50
        foreach ($dir as $file) {
51
            if (!$file->isFile()) {
52
                continue;
53
            }
54
            $path = $file->getRealPath() ?: $file->getPathname();
55
            if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) {
56
                continue;
57
            }
58
            $classes = self::findClasses($path);
59
            if (\PHP_VERSION_ID >= 70000) {
60
                // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
61
                gc_mem_caches();
62
            }
63
            foreach ($classes as $class) {
64
                $map[$class] = $path;
65
            }
66
        }
67
68
        return $map;
69
    }
70
71
    /**
72
     * Extract the classes in the given file.
73
     *
74
     * @param string $path The file to check
75
     *
76
     * @return array The found classes
77
     */
78
    private static function findClasses($path): array
0 ignored issues
show
Private method name "ClassMapGenerator::findClasses" must be prefixed with an underscore
Loading history...
79
    {
80
        $contents = file_get_contents($path);
81
        $tokens = token_get_all($contents);
82
        $classes = [];
83
        $namespace = '';
84
        for ($i = 0; isset($tokens[$i]); ++$i) {
85
            $token = $tokens[$i];
86
            if (!isset($token[1])) {
87
                continue;
88
            }
89
            $class = '';
90
            switch ($token[0]) {
91
                case T_NAMESPACE:
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
92
                    $namespace = '';
93
                    // If there is a namespace, extract it
94
                    while (isset($tokens[++$i][1])) {
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
95
                        if (PHP_MAJOR_VERSION >= 8) {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
96
                            if (\in_array($tokens[$i][0], [T_NAME_QUALIFIED], false)) {
0 ignored issues
show
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
97
                                $namespace .= $tokens[$i][1];
98
                            }
0 ignored issues
show
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
99
                        } else {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
100
                            if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], false)) {
0 ignored issues
show
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
101
                                $namespace .= $tokens[$i][1];
102
                            }
0 ignored issues
show
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
103
                        }
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
104
                    }
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
105
                    $namespace .= '\\';
106
                    break;
107
                case T_CLASS:
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
108
                case T_INTERFACE:
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
109
                case T_TRAIT:
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
110
                    // Skip usage of ::class constant
111
                    $isClassConstant = false;
112
                    for ($j = $i - 1; $j > 0; --$j) {
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
113
                        if (!isset($tokens[$j][1])) {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
114
                            break;
115
                        }
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
116
                        if (T_DOUBLE_COLON === $tokens[$j][0]) {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
117
                            $isClassConstant = true;
118
                            break;
119
                        } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
120
                            break;
121
                        }
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
122
                    }
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
123
                    if ($isClassConstant) {
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
124
                        break;
125
                    }
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
126
                    // Find the classname
127
                    while (isset($tokens[++$i][1])) {
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
128
                        $t = $tokens[$i];
129
                        if (T_STRING === $t[0]) {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
130
                            $class .= $t[1];
131
                        } elseif ('' !== $class && T_WHITESPACE === $t[0]) {
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
132
                            break;
133
                        }
0 ignored issues
show
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
134
                    }
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
135
                    $classes[] = ltrim($namespace . $class, '\\');
136
                    break;
137
                default:
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
138
                    break;
139
            }
140
        }
141
142
        return $classes;
143
    }
144
}
145