Completed
Pull Request — master (#293)
by Дмитрий
04:54
created

FileParser::parseTopDefinitions()   C

Complexity

Conditions 21
Paths 36

Size

Total Lines 66
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 28.2437

Importance

Changes 0
Metric Value
cc 21
eloc 44
nc 36
nop 3
dl 0
loc 66
ccs 44
cts 59
cp 0.7458
crap 28.2437
rs 5.781
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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