NodesFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 0
cts 13
cp 0
wmc 3
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createInstance() 0 8 1
A create() 0 5 1
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);
0 ignored issues
show
Bug introduced by
It seems like $stmt defined by $this->parser->parse($code) on line 80 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...
82
    }
83
}
84