1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Reflection; |
11
|
|
|
|
12
|
|
|
use Railt\Io\File; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
use Railt\Reflection\Common\Serializable; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
16
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
17
|
|
|
use Railt\Reflection\Contracts\Reflection as ReflectionInterface; |
18
|
|
|
use Railt\Reflection\Contracts\Type as TypeInterface; |
19
|
|
|
use Railt\Reflection\Exception\TypeNotFoundException; |
20
|
|
|
use Railt\Reflection\Invocation\Behaviour\HasDirectives; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Document |
24
|
|
|
*/ |
25
|
|
|
class Document extends AbstractDefinition implements DocumentInterface |
26
|
|
|
{ |
27
|
|
|
use Serializable; |
28
|
|
|
use HasDirectives; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Readable |
32
|
|
|
*/ |
33
|
|
|
protected $file; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Reflection |
37
|
|
|
*/ |
38
|
|
|
protected $store; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array|string[] |
42
|
|
|
*/ |
43
|
|
|
protected $types = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* DocumentDefinition constructor. |
47
|
|
|
* @param Reflection|ReflectionInterface $parent |
48
|
|
|
* @param Readable|null $file |
49
|
|
|
*/ |
50
|
4 |
|
public function __construct(Reflection $parent, Readable $file = null) |
51
|
|
|
{ |
52
|
4 |
|
$this->file = $file ?? File::fromSources(''); |
53
|
4 |
|
$this->store = $parent; |
54
|
|
|
|
55
|
4 |
|
parent::__construct($this, 0); |
|
|
|
|
56
|
|
|
|
57
|
4 |
|
$parent->addDocument($this); |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return TypeInterface |
62
|
|
|
*/ |
63
|
|
|
public static function getType(): TypeInterface |
64
|
|
|
{ |
65
|
|
|
return Type::of(Type::DOCUMENT); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param TypeDefinition $type |
70
|
|
|
*/ |
71
|
2 |
|
public function addTypeDefinition(TypeDefinition $type): void |
72
|
|
|
{ |
73
|
2 |
|
$this->store->add($type); |
74
|
2 |
|
$this->types[] = $type->getName(); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Readable |
79
|
|
|
*/ |
80
|
1 |
|
public function getFile(): Readable |
81
|
|
|
{ |
82
|
1 |
|
return $this->file; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ReflectionInterface|Reflection |
87
|
|
|
*/ |
88
|
|
|
public function getReflection(): ReflectionInterface |
89
|
|
|
{ |
90
|
|
|
return $this->store; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function isDeprecated(): bool |
97
|
|
|
{ |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getDeprecationReason(): string |
105
|
|
|
{ |
106
|
|
|
return ''; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return iterable|TypeDefinition[] |
111
|
|
|
* @throws TypeNotFoundException |
112
|
|
|
*/ |
113
|
|
|
public function getTypeDefinitions(): iterable |
114
|
|
|
{ |
115
|
|
|
foreach ($this->types as $type) { |
116
|
|
|
yield $this->store->get($type); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function hasTypeDefinition(string $name): bool |
125
|
|
|
{ |
126
|
|
|
return \in_array($name, $this->types, true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* @return null|TypeDefinition |
132
|
|
|
*/ |
133
|
|
|
public function getTypeDefinition(string $name): ?TypeDefinition |
134
|
|
|
{ |
135
|
|
|
if (! \in_array($name, $this->types, true)) { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->store->find($name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
4 |
|
public function getName(): string |
146
|
|
|
{ |
147
|
4 |
|
return $this->file->isFile() ? $this->file->getPathname() : $this->file->getHash(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.