Completed
Pull Request — master (#107)
by Alexander
03:29
created

ReflectionFile::getFileNamespaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Parser Reflection API
6
 *
7
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Go\ParserReflection;
14
15
16
use Go\ParserReflection\Instrument\PathResolver;
17
use PhpParser\Node;
18
use PhpParser\Node\Stmt\Namespace_;
19
20
/**
21
 * AST-based reflector for the source file
22
 */
23
class ReflectionFile
24
{
25
26
    /**
27
     * Name of the file for reflection
28
     *
29
     * @var string
30
     */
31
    protected $fileName;
32
33
    /**
34
     * List of namespaces in the file
35
     *
36
     * @var ReflectionFileNamespace[]|array
37
     */
38
    protected $fileNamespaces;
39
40
    /**
41
     * Top-level nodes for the file
42
     *
43
     * @var Node[]
44
     */
45
    private $topLevelNodes;
46
47
    /**
48
     * ReflectionFile constructor.
49
     *
50
     * @param string            $fileName      Name of the file to reflect
51
     * @param null|array|Node[] $topLevelNodes Optional corresponding list of AST nodes for that file
52
     */
53 3049
    public function __construct(string $fileName, ?array $topLevelNodes = null)
54
    {
55 3049
        $fileName            = PathResolver::realpath($fileName);
56 3049
        $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...
57 3049
        $this->topLevelNodes = $topLevelNodes ?: ReflectionEngine::parseFile($fileName);
58 3049
    }
59
60
    /**
61
     * Returns a namespace from the file or false if no such a namespace
62
     *
63
     * @param string $namespaceName
64
     *
65
     * @return bool|ReflectionFileNamespace
66
     */
67 3030
    public function getFileNamespace(string $namespaceName)
68
    {
69 3030
        if ($this->hasFileNamespace($namespaceName)) {
70 3030
            return $this->fileNamespaces[$namespaceName];
71
        }
72
73 1
        return false;
74
    }
75
76
    /**
77
     * Gets the list of namespaces in the file
78
     *
79
     * @return array|ReflectionFileNamespace[]
80
     */
81 3040
    public function getFileNamespaces(): array
82
    {
83 3040
        if (!isset($this->fileNamespaces)) {
84 3040
            $this->fileNamespaces = $this->findFileNamespaces();
85
        }
86
87 3040
        return $this->fileNamespaces;
88
    }
89
90
    /**
91
     * Returns the name of current reflected file
92
     */
93 1
    public function getName(): string
94
    {
95 1
        return $this->fileName;
96
    }
97
98
    /**
99
     * Returns an AST-nodes for file
100
     *
101
     * @return Node[]
102
     */
103
    public function getNodes(): ?array
104
    {
105
        return $this->topLevelNodes;
106
    }
107
108
    /**
109
     * Returns the presence of namespace in the file
110
     */
111 3031
    public function hasFileNamespace(string $namespaceName): bool
112
    {
113 3031
        $namespaces = $this->getFileNamespaces();
114
115 3031
        return isset($namespaces[$namespaceName]);
116
    }
117
118
    /**
119
     * Checks if the current file is in strict mode
120
     */
121 4
    public function isStrictMode(): bool
122
    {
123
        // declare statement for the strict_types can be only top-level node
124 4
        $topLevelNode = reset($this->topLevelNodes);
125 4
        if (!$topLevelNode instanceof Node\Stmt\Declare_) {
126 1
            return false;
127
        }
128
129 3
        $declareStatement = reset($topLevelNode->declares);
130 3
        $isStrictTypeKey  = $declareStatement->key->toString() === 'strict_types';
131 3
        $isScalarValue    = $declareStatement->value instanceof Node\Scalar\LNumber;
132 3
        $isStrictMode     = $isStrictTypeKey && $isScalarValue && $declareStatement->value->value === 1;
133
134 3
        return $isStrictMode;
135
    }
136
137
    /**
138
     * Searches for file namespaces in the given AST
139
     *
140
     * @return ReflectionFileNamespace[]
141
     */
142 3040
    private function findFileNamespaces(): array
143
    {
144 3040
        $namespaces = [];
145
146
        // namespaces can be only top-level nodes, so we can scan them directly
147 3040
        foreach ($this->topLevelNodes as $topLevelNode) {
148 3040
            if ($topLevelNode instanceof Namespace_) {
149 3040
                $namespaceName = $topLevelNode->name ? $topLevelNode->name->toString() : '';
150
151 3040
                $namespaces[$namespaceName] = new ReflectionFileNamespace(
152 3040
                    $this->fileName,
153
                    $namespaceName,
154
                    $topLevelNode
155
                );
156
            }
157
        }
158
159 3040
        return $namespaces;
160
    }
161
}
162