Issues (16)

src/Parser/PhpParser.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Parser;
13
14
use PhpParser\Error;
15
use PhpParser\Parser;
16
use PhpUnitGen\Exception\ParseException;
17
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
18
use PhpUnitGen\Model\PhpFileModel;
19
use PhpUnitGen\Parser\NodeParser\PhpFileNodeParser;
20
use PhpUnitGen\Parser\ParserInterface\PhpParserInterface;
21
22
/**
23
 * Class PhpParser.
24
 *
25
 * @author     Paul Thébaud <[email protected]>.
26
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
27
 * @license    https://opensource.org/licenses/MIT The MIT license.
28
 * @link       https://github.com/paul-thebaud/phpunit-generator
29
 * @since      Class available since Release 2.0.0.
30
 */
31
class PhpParser implements PhpParserInterface
32
{
33
    /**
34
     * @var Parser $phpParser A parser to parse php code as a string.
35
     */
36
    private $phpParser;
37
38
    /**
39
     * @var PhpFileNodeParser $phpFileNodeParser A php file node parser to parse php nodes.
40
     */
41
    private $phpFileNodeParser;
42
43
    /**
44
     * PhpFileParser constructor.
45
     *
46
     * @param Parser            $phpParser         The php code parser to use.
47
     * @param PhpFileNodeParser $phpFileNodeParser The php file node parser to use.
48
     */
49
    public function __construct(
50
        Parser $phpParser,
51
        PhpFileNodeParser $phpFileNodeParser
52
    ) {
53
        $this->phpParser         = $phpParser;
54
        $this->phpFileNodeParser = $phpFileNodeParser;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function invoke(string $code): PhpFileModelInterface
61
    {
62
        try {
63
            $nodes = $this->phpParser->parse($code);
64
        } catch (Error $error) {
65
            throw new ParseException('Unable to parse given php code (maybe your code contains errors)');
66
        }
67
68
        $phpFileModel = new PhpFileModel();
69
        $this->phpFileNodeParser->preParseUses($nodes, $phpFileModel);
0 ignored issues
show
It seems like $nodes can also be of type null; however, parameter $nodes of PhpUnitGen\Parser\NodePa...eParser::preParseUses() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $this->phpFileNodeParser->preParseUses(/** @scrutinizer ignore-type */ $nodes, $phpFileModel);
Loading history...
70
71
        $this->phpFileNodeParser->parseSubNodes($nodes, $phpFileModel);
0 ignored issues
show
It seems like $nodes can also be of type null; however, parameter $nodes of PhpUnitGen\Parser\NodePa...Parser::parseSubNodes() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $this->phpFileNodeParser->parseSubNodes(/** @scrutinizer ignore-type */ $nodes, $phpFileModel);
Loading history...
72
73
        return $phpFileModel;
74
    }
75
}
76