Completed
Push — master ( 4dd860...67b321 )
by Nikola
03:47
created

MetadataLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 71
ccs 18
cts 19
cp 0.9474
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B load() 0 26 5
A create() 0 4 1
1
<?php
2
3
namespace RunOpenCode\AbstractBuilder\Ast;
4
5
use PhpParser\NodeTraverser;
6
use PhpParser\NodeVisitor\NameResolver;
7
use RunOpenCode\AbstractBuilder\Ast\Visitor\FileMetadataIntrospectionVisitor;
8
use RunOpenCode\AbstractBuilder\Exception\RuntimeException;
9
10
class MetadataLoader
11
{
12
    /**
13
     * @var \PhpParser\Parser
14
     */
15
    private $parser;
16
17
    /**
18
     * @var \PhpParser\NodeTraverser
19
     */
20
    private $traverser;
21
22
    /**
23
     * @var FileMetadataIntrospectionVisitor
24
     */
25
    private $introspector;
26
27 3
    private function __construct()
28
    {
29 3
        $this->parser = Parser::getInstance();
30 3
        $this->traverser = new NodeTraverser();
31
32 3
        $this->traverser->addVisitor(new NameResolver());
33 3
    }
34
35
    /**
36
     * Load file metadata
37
     *
38
     * @param string $arg
39
     *
40
     * @return Metadata\FileMetadata
41
     *
42
     * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException
43
     */
44 3
    public function load($arg)
45
    {
46 3
        $filename = $arg;
47
48
        if (
49 3
            class_exists($arg, true)
50
            ||
51 3
            trait_exists($arg, true))
52
        {
53 1
            $filename = (new \ReflectionClass($arg))->getFileName();
54
        }
55
56 3
        if (null !== $this->introspector) {
57
            $this->traverser->removeVisitor($this->introspector);
58
        }
59
60 3
        $this->traverser->addVisitor($this->introspector = new FileMetadataIntrospectionVisitor($filename));
61
62 3
        if (!file_exists($filename)) {
63 1
            throw new RuntimeException(sprintf('Unable to load file metadata from "%s".', $arg));
64
        }
65
66 2
        $this->traverser->traverse($this->parser->parse(file_get_contents($filename)));
0 ignored issues
show
Bug introduced by
It seems like $this->parser->parse(fil...et_contents($filename)) targeting PhpParser\Parser::parse() 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?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
68 2
        return $this->introspector->getMetadata();
69
    }
70
71
    /**
72
     * MetadataLoader Factory
73
     *
74
     * @return MetadataLoader
75
     */
76 3
    public static function create()
77
    {
78 3
        return new static();
79
    }
80
}
81