|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Marcosh\PhpTypeChecker\Check; |
|
6
|
|
|
|
|
7
|
|
|
use phpDocumentor\Reflection\Type; |
|
8
|
|
|
use phpDocumentor\Reflection\Types\Array_; |
|
9
|
|
|
use phpDocumentor\Reflection\Types\Mixed; |
|
10
|
|
|
use phpDocumentor\Reflection\Types\Object_; |
|
11
|
|
|
use phpDocumentor\Reflection\Types\Self_; |
|
12
|
|
|
use Roave\BetterReflection\Reflection\ReflectionParameter; |
|
13
|
|
|
|
|
14
|
|
|
final class Parameter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ReflectionParameter |
|
18
|
|
|
*/ |
|
19
|
|
|
private $parameter; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(ReflectionParameter $parameter) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->parameter = $parameter; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return Type[] |
|
28
|
|
|
*/ |
|
29
|
|
|
private function docBlockTypes(): array |
|
30
|
|
|
{ |
|
31
|
|
|
$docBlockTypes = []; |
|
32
|
|
|
|
|
33
|
|
|
try { |
|
34
|
|
|
$docBlockTypes = $this->parameter->getDocBlockTypes(); |
|
35
|
|
|
} catch (\InvalidArgumentException $e) { |
|
36
|
|
|
// we need this here to prevent reflection-bocblock errors on @see invalid Fqsen |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $docBlockTypes; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function typeIsMissing(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return null === $this->parameter->getTypeHint() && |
|
45
|
|
|
empty($this->docBlockTypes()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function typeIsMissingWithDocBlock(): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return null === $this->parameter->getTypeHint() && |
|
51
|
|
|
!empty($this->docBlockTypes()) && |
|
52
|
|
|
!$this->docBlockTypes()[0] instanceof Mixed && |
|
53
|
|
|
!$this->docBlockTypes()[0] instanceof Object_; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function typeDoesNotCoincideWithDocBlock(): bool |
|
57
|
|
|
{ |
|
58
|
|
|
$docBlockTypes = $this->docBlockTypes(); |
|
59
|
|
|
|
|
60
|
|
|
return !empty($docBlockTypes) && |
|
61
|
|
|
$this->parameter->getTypeHint() instanceof Type && |
|
62
|
|
|
!in_array($this->parameter->getTypeHint(), $docBlockTypes, false) && |
|
63
|
|
|
!($this->parameter->getTypeHint() instanceof Array_ && $docBlockTypes[0] instanceof Array_) && |
|
64
|
|
|
!($this->parameter->getTypeHint() instanceof Self_ && |
|
65
|
|
|
$docBlockTypes[0] instanceof Object_ && |
|
66
|
|
|
$this->parameter->getDeclaringClass()->getName() === ltrim((string) $docBlockTypes[0]->getFqsen(), '\\') |
|
67
|
|
|
) && |
|
68
|
|
|
!($this->parameter->getTypeHint() instanceof Object_ && |
|
69
|
|
|
$docBlockTypes[0] instanceof Self_ && |
|
70
|
|
|
$this->parameter->getDeclaringClass()->getName() === ltrim((string) $this->parameter->getTypeHint()->getFqsen(), '\\') |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|