1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\DependencyGuard\Php; |
8
|
|
|
|
9
|
|
|
use Mediact\DependencyGuard\Iterator\FileIteratorInterface; |
10
|
|
|
use Mediact\DependencyGuard\Php\Filter\SymbolFilterInterface; |
11
|
|
|
use PhpParser\Error; |
12
|
|
|
use PhpParser\NodeTraverser; |
13
|
|
|
use PhpParser\Parser; |
14
|
|
|
use PhpParser\ParserFactory; |
15
|
|
|
|
16
|
|
|
class SymbolExtractor implements SymbolExtractorInterface |
17
|
|
|
{ |
18
|
|
|
/** @var Parser */ |
19
|
|
|
private $parser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor. |
23
|
|
|
* |
24
|
|
|
* @param Parser|null $parser |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct(Parser $parser = null) |
27
|
|
|
{ |
28
|
1 |
|
if ($parser === null) { |
29
|
1 |
|
$factory = new ParserFactory(); |
30
|
1 |
|
$parser = $factory->create(ParserFactory::PREFER_PHP7); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
$this->parser = $parser; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Extract the PHP symbols from the given files. |
38
|
|
|
* |
39
|
|
|
* @param FileIteratorInterface $files |
40
|
|
|
* @param SymbolFilterInterface $filter |
41
|
|
|
* |
42
|
|
|
* @return SymbolIteratorInterface |
43
|
|
|
*/ |
44
|
4 |
|
public function extract( |
45
|
|
|
FileIteratorInterface $files, |
46
|
|
|
SymbolFilterInterface $filter |
47
|
|
|
): SymbolIteratorInterface { |
48
|
4 |
|
$symbols = []; |
49
|
|
|
|
50
|
4 |
|
foreach ($files as $file) { |
51
|
3 |
|
try { |
52
|
1 |
|
$contents = $this->readFile($file); |
53
|
|
|
|
54
|
|
|
if ($contents === null) { |
55
|
|
|
continue; |
56
|
2 |
|
} |
57
|
2 |
|
|
58
|
2 |
|
$statements = $this->parser->parse($contents); |
59
|
1 |
|
} catch (Error $e) { |
60
|
|
|
// Either not a PHP file or the broken file should be detected by other |
61
|
|
|
// tooling entirely. |
62
|
1 |
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$tracker = new SymbolTracker($filter); |
66
|
1 |
|
$traverser = new NodeTraverser(); |
67
|
1 |
|
$traverser->addVisitor($tracker); |
68
|
1 |
|
$traverser->traverse($statements); |
69
|
|
|
|
70
|
1 |
|
foreach ($tracker->getSymbols() as $node) { |
71
|
1 |
|
$symbols[] = new Symbol($file, $node); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
return new SymbolIterator(...$symbols); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \SplFileInfo $file |
80
|
|
|
* |
81
|
|
|
* @return null|string |
82
|
|
|
*/ |
83
|
|
|
private function readFile(\SplFileInfo $file): ?string |
84
|
|
|
{ |
85
|
|
|
$oldErrorReporting = error_reporting(); |
86
|
|
|
|
87
|
|
|
error_reporting($oldErrorReporting & ~E_WARNING); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$contents = \file_get_contents($file); |
91
|
|
|
|
92
|
|
|
if ($contents === false) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $contents; |
97
|
|
|
} finally { |
98
|
|
|
error_reporting($oldErrorReporting); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|