1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
12
|
|
|
* @link http://phpdoc.org |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpDocumentor\Reflection\Php; |
16
|
|
|
|
17
|
|
|
use phpDocumentor\Reflection\NodeVisitor\ElementNameResolver; |
18
|
|
|
use PhpParser\Node; |
19
|
|
|
use PhpParser\NodeTraverser; |
20
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
21
|
|
|
use PhpParser\Parser; |
22
|
|
|
use PhpParser\ParserFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Factory to create a array of nodes from a provided file. |
26
|
|
|
* This factory will use PhpParser and NodeTraverser to do the real processing. |
27
|
|
|
*/ |
28
|
|
|
class NodesFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Parser used to parse the code to nodes. |
32
|
|
|
* |
33
|
|
|
* @var Parser |
34
|
|
|
*/ |
35
|
|
|
private $parser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Containing a number of visitors to do some post processing steps on nodes. |
39
|
|
|
* |
40
|
|
|
* @var NodeTraverser |
41
|
|
|
*/ |
42
|
|
|
private $traverser; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initializes the object. |
46
|
|
|
* |
47
|
|
|
* @param Parser $parser used to parse the code |
48
|
|
|
* @param NodeTraverser $traverser used to do some post processing on the nodes |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Parser $parser, NodeTraverser $traverser) |
51
|
|
|
{ |
52
|
|
|
$this->parser = $parser; |
53
|
|
|
$this->traverser = $traverser; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates a new instance of NodeFactory with default Parser ands Traverser. |
58
|
|
|
* |
59
|
|
|
* @param int $kind One of ParserFactory::PREFER_PHP7, |
60
|
|
|
* ParserFactory::PREFER_PHP5, ParserFactory::ONLY_PHP7 or ParserFactory::ONLY_PHP5 |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
|
|
public static function createInstance($kind = ParserFactory::PREFER_PHP7): self |
64
|
|
|
{ |
65
|
|
|
$parser = (new ParserFactory())->create($kind); |
66
|
|
|
$traverser = new NodeTraverser(); |
67
|
|
|
$traverser->addVisitor(new NameResolver()); |
68
|
|
|
$traverser->addVisitor(new ElementNameResolver()); |
69
|
|
|
return new static($parser, $traverser); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Will convert the provided code to nodes. |
74
|
|
|
* |
75
|
|
|
* @param string $code code to process. |
76
|
|
|
* @return Node[] |
77
|
|
|
*/ |
78
|
|
|
public function create(string $code): array |
79
|
|
|
{ |
80
|
|
|
$stmt = $this->parser->parse($code); |
81
|
|
|
return $this->traverser->traverse($stmt); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.