|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Error; |
|
6
|
|
|
use PhpParser\Parser; |
|
7
|
|
|
use PhpUnitGen\Exception\ParsingException; |
|
8
|
|
|
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface; |
|
9
|
|
|
use PhpUnitGen\Model\PhpFileModel; |
|
10
|
|
|
use PhpUnitGen\Parser\NodeParser\PhpFileNodeParser; |
|
11
|
|
|
use PhpUnitGen\Parser\ParserInterface\PhpParserInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PhpParser. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Paul Thébaud <[email protected]>. |
|
17
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
|
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
|
19
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
|
20
|
|
|
* @since Class available since Release 2.0.0. |
|
21
|
|
|
*/ |
|
22
|
|
|
class PhpParser implements PhpParserInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Parser $phpParser A parser to parse php code as a string. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $phpParser; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var PhpFileNodeParser $phpFileNodeParser A php file node parser to parse php nodes. |
|
31
|
|
|
*/ |
|
32
|
|
|
private $phpFileNodeParser; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* PhpFileParser constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Parser $phpParser The php code parser. |
|
38
|
|
|
* @param PhpFileNodeParser $phpFileNodeParser The php file node parser. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
Parser $phpParser, |
|
42
|
|
|
PhpFileNodeParser $phpFileNodeParser |
|
43
|
|
|
) { |
|
44
|
|
|
$this->phpParser = $phpParser; |
|
45
|
|
|
$this->phpFileNodeParser = $phpFileNodeParser; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function invoke(string $code): PhpFileModelInterface |
|
52
|
|
|
{ |
|
53
|
|
|
try { |
|
54
|
|
|
$nodes = $this->phpParser->parse($code); |
|
55
|
|
|
} catch (Error $error) { |
|
56
|
|
|
throw new ParsingException("Unable to parse given php code (maybe your code contains errors)."); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$phpFileModel = new PhpFileModel(); |
|
60
|
|
|
|
|
61
|
|
|
/** @var PhpFileModelInterface $phpFileModel */ |
|
62
|
|
|
$phpFileModel = $this->phpFileNodeParser->parseSubNodes($nodes, $phpFileModel); |
|
63
|
|
|
|
|
64
|
|
|
return $phpFileModel; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|