Parameter   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 60
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A docBlockTypes() 0 12 2
A typeIsMissing() 0 5 2
A typeIsMissingWithDocBlock() 0 7 4
B typeDoesNotCoincideWithDocBlock() 0 17 11
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