Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
ClassFinder | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |
0.00% |
0 / 1 |
find | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 |
1 | <?php |
2 | /** |
3 | * Copyright (C) 2018 Kunal Mehta <legoktm@debian.org> |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | namespace MediaWiki\Tool\PatchCoverage; |
20 | |
21 | use MediaWiki\Tool\PatchCoverage\Parser\ClassTrackerVisitor; |
22 | use PhpParser\NodeTraverser; |
23 | use PhpParser\NodeVisitor\NameResolver; |
24 | use PhpParser\ParserFactory; |
25 | |
26 | /** |
27 | * Parse PHP files to find classes. We use a proper PHP parser |
28 | * for it's awesome namespace support and to avoid all false |
29 | * positives. |
30 | */ |
31 | class ClassFinder { |
32 | |
33 | /** |
34 | * Get all the classes that are found in these files |
35 | * |
36 | * @param array $files |
37 | * @return string[] Fully qualified class names |
38 | */ |
39 | public function find( array $files ) { |
40 | if ( !$files ) { |
41 | return []; |
42 | } |
43 | $parser = ( new ParserFactory() ) |
44 | ->create( ParserFactory::PREFER_PHP7 ); |
45 | $tracker = new ClassTrackerVisitor(); |
46 | foreach ( $files as $file ) { |
47 | $contents = file_get_contents( $file ); |
48 | $tree = $parser->parse( $contents ); |
49 | // TODO: Do we need a new traverser each time? |
50 | $traverser = new NodeTraverser(); |
51 | $traverser->addVisitor( new NameResolver() ); |
52 | $traverser->addVisitor( $tracker ); |
53 | $traverser->traverse( $tree ); |
54 | |
55 | } |
56 | |
57 | sort( $tracker->classes ); |
58 | return $tracker->classes; |
59 | } |
60 | } |