Completed
Push — master ( fdee56...483c4c )
by Alexander
02:07
created

ReflectionFile   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 116
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getFileNamespace() 0 8 2
A getFileNamespaces() 0 8 2
A getName() 0 4 1
A hasFileNamespace() 0 6 1
A findFileNamespaces() 0 19 4
1
<?php
2
/**
3
 * Parser Reflection API
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\ParserReflection;
12
13
14
use Go\ParserReflection\Instrument\PathResolver;
15
use PhpParser\Node;
16
use PhpParser\Node\Stmt\Namespace_;
17
18
/**
19
 * AST-based reflector for the source file
20
 */
21
class ReflectionFile
22
{
23
24
    /**
25
     * Name of the file for reflectino
26
     *
27
     * @var string
28
     */
29
    protected $fileName;
30
31
    /**
32
     * List of namespaces in the file
33
     *
34
     * @var ReflectionFileNamespace[]|array
35
     */
36
    protected $fileNamespaces;
37
38
    /**
39
     * Top-level nodes for the file
40
     *
41
     * @var Node[]
42
     */
43
    private $topLevelNodes;
44
45
    /**
46
     * ReflectionFile constructor.
47
     *
48
     * @param string $fileName Name of the file to reflect
49
     * @param null|array|Node[] $topLevelNodes Optional corresponding list of AST nodes for that file
50
     */
51 143
    public function __construct($fileName, $topLevelNodes = null)
52
    {
53 143
        $fileName            = PathResolver::realpath($fileName);
54 143
        $this->fileName      = $fileName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fileName can also be of type array or boolean. However, the property $fileName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55 143
        $this->topLevelNodes = $topLevelNodes ?: ReflectionEngine::parseFile($fileName);
56 143
    }
57
58
    /**
59
     * Returns a namespace from the file or false if no such a namespace
60
     *
61
     * @param string $namespaceName
62
     *
63
     * @return bool|ReflectionFileNamespace
64
     */
65 134
    public function getFileNamespace($namespaceName)
66
    {
67 134
        if ($this->hasFileNamespace($namespaceName)) {
68 134
            return $this->fileNamespaces[$namespaceName];
69
        }
70
71 1
        return false;
72
    }
73
74
    /**
75
     * Gets the list of namespaces in the file
76
     *
77
     * @return array|ReflectionFileNamespace[]
78
     */
79 140
    public function getFileNamespaces()
80
    {
81 140
        if (!isset($this->fileNamespaces)) {
82 140
            $this->fileNamespaces = $this->findFileNamespaces();
83
        }
84
85 140
        return $this->fileNamespaces;
86
    }
87
88
    /**
89
     * Returns the name of current reflected file
90
     *
91
     * @return string
92
     */
93 1
    public function getName()
94
    {
95 1
        return $this->fileName;
96
    }
97
98
    /**
99
     * Returns the presence of namespace in the file
100
     *
101
     * @param string $namespaceName Namespace to check
102
     *
103
     * @return bool
104
     */
105 135
    public function hasFileNamespace($namespaceName)
106
    {
107 135
        $namespaces = $this->getFileNamespaces();
108
109 135
        return isset($namespaces[$namespaceName]);
110
    }
111
112
    /**
113
     * Searches for file namespaces in the given AST
114
     *
115
     * @return array|ReflectionFileNamespace[]
116
     */
117 140
    private function findFileNamespaces()
118
    {
119 140
        $namespaces = array();
120
121
        // namespaces can be only top-level nodes, so we can scan them directly
122 140
        foreach ($this->topLevelNodes as $topLevelNode) {
123 140
            if ($topLevelNode instanceof Namespace_) {
124 140
                $namespaceName = $topLevelNode->name ? $topLevelNode->name->toString() : '';
125
126 140
                $namespaces[$namespaceName] = new ReflectionFileNamespace(
127 140
                    $this->fileName,
128
                    $namespaceName,
129
                    $topLevelNode
130
                );
131
            }
132
        }
133
134 140
        return $namespaces;
135
    }
136
}
137