|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Scanner; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Error; |
|
6
|
|
|
use PhpParser\NodeTraverser; |
|
7
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
|
8
|
|
|
use PhpParser\ParserFactory; |
|
9
|
|
|
use PHPSemVerChecker\Registry\Registry; |
|
10
|
|
|
use PHPSemVerChecker\Visitor\ClassVisitor; |
|
11
|
|
|
use PHPSemVerChecker\Visitor\FunctionVisitor; |
|
12
|
|
|
use PHPSemVerChecker\Visitor\InterfaceVisitor; |
|
13
|
|
|
use PHPSemVerChecker\Visitor\TraitVisitor; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
class Scanner |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \PHPSemVerChecker\Registry\Registry |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $registry; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \PhpParser\Parser |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $parser; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \PhpParser\NodeTraverser |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $traverser; |
|
30
|
|
|
|
|
31
|
5 |
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
5 |
|
$this->registry = new Registry(); |
|
34
|
5 |
|
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
|
35
|
5 |
|
$this->traverser = new NodeTraverser(); |
|
36
|
|
|
|
|
37
|
|
|
$visitors = [ |
|
38
|
5 |
|
new NameResolver(), |
|
39
|
5 |
|
new ClassVisitor($this->registry), |
|
40
|
5 |
|
new InterfaceVisitor($this->registry), |
|
41
|
5 |
|
new FunctionVisitor($this->registry), |
|
42
|
5 |
|
new TraitVisitor($this->registry), |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
5 |
|
foreach ($visitors as $visitor) { |
|
46
|
5 |
|
$this->traverser->addVisitor($visitor); |
|
47
|
|
|
} |
|
48
|
5 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $file |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function scan($file) |
|
54
|
|
|
{ |
|
55
|
|
|
// Set the current file used by the registry so that we can tell where the change was scanned. |
|
56
|
5 |
|
$this->registry->setCurrentFile($file); |
|
57
|
|
|
|
|
58
|
5 |
|
$code = file_get_contents($file); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
5 |
|
$statements = $this->parser->parse($code); |
|
62
|
4 |
|
$this->traverser->traverse($statements); |
|
|
|
|
|
|
63
|
1 |
|
} catch (Error $e) { |
|
64
|
1 |
|
throw new RuntimeException('Parse Error: ' . $e->getMessage() . ' in ' . $file); |
|
65
|
|
|
} |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return \PHPSemVerChecker\Registry\Registry |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function getRegistry() |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->registry; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.