Scanner::getRegistry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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);
0 ignored issues
show
Bug introduced by
It seems like $statements defined by $this->parser->parse($code) on line 61 can also be of type null; however, PhpParser\NodeTraverser::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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