Completed
Push — master ( 8f0d0f...15736a )
by Дмитрий
06:53
created

FileParser::parserFile()   B

Complexity

Conditions 4
Paths 17

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.5923

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 17
nop 2
dl 0
loc 24
ccs 10
cts 15
cp 0.6667
crap 4.5923
rs 8.6845
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\Compiler;
9
use PHPSA\Context;
10
use RuntimeException;
11
12
class FileParser
13
{
14
    /**
15
     * @var \PhpParser\Parser
16
     */
17
    protected $parser;
18
19
    /**
20
     * @var \PhpParser\NodeTraverser
21
     */
22
    protected $nodeTraverser;
23
24
    /**
25
     * @var Compiler
26
     */
27
    protected $compiler;
28
29
    /**
30
     * @var \PHPSA\Compiler\DefinitionVisitor
31
     */
32
    protected $definitionVisitor;
33
34
    /**
35
     * @param \PhpParser\Parser $parser
36
     * @param Compiler $compiler
37
     */
38 55
    public function __construct(\PhpParser\Parser $parser, Compiler $compiler)
39
    {
40 55
        $this->nodeTraverser = new \PhpParser\NodeTraverser();
41 55
        $this->nodeTraverser->addVisitor(new \PHPSA\Compiler\NameResolveVisitor);
42 55
        $this->nodeTraverser->addVisitor(
43 55
            $this->definitionVisitor = new \PHPSA\Compiler\DefinitionVisitor($compiler)
44
        );
45
46 55
        $this->parser = $parser;
47 55
        $this->compiler = $compiler;
48 55
    }
49
50
    /**
51
     * @param string $filepath
52
     * @param Context $context
53
     * @throws RuntimeException when filepath is not readable
54
     */
55 55
    public function parserFile($filepath, Context $context)
56
    {
57 55
        $context->setFilepath($filepath);
58
59
        try {
60 55
            if (!is_readable($filepath)) {
61
                throw new RuntimeException('File ' . $filepath . ' is not readable');
62
            }
63
64 55
            $context->debug('<comment>Precompile: ' . $filepath . '.</comment>');
65
66 55
            $code = file_get_contents($filepath);
67 55
            $astTree = $this->parser->parse($code);
68
69 55
            $this->definitionVisitor->setFilePath($filepath);
70 55
            $this->nodeTraverser->traverse($astTree);
0 ignored issues
show
Bug introduced by
It seems like $astTree defined by $this->parser->parse($code) on line 67 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...
71
72 55
            $context->clear();
73
        } catch (\PhpParser\Error $e) {
74
            $context->syntaxError($e, $filepath);
75
        } catch (\Exception $e) {
76
            $context->output->writeln("<error>{$e->getMessage()}</error>");
77
        }
78 55
    }
79
}
80