Passed
Push — master ( 1e65a5...6cebba )
by SignpostMarv
03:39
created

PropertyReflectionExtension::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject\DaftNestedObject\PHPStan;
10
11
use PHPStan\Broker\Broker;
12
use PHPStan\Reflection\ClassReflection;
13
use PHPStan\Reflection\PropertyReflection;
14
use PHPStan\Type\IntegerType;
15
use PHPStan\Type\Type;
16
use SignpostMarv\DaftObject\DaftNestedWriteableObject;
17
use SignpostMarv\DaftObject\PHPStan\PropertyReflectionExtension as Base;
18
19
class PropertyReflectionExtension extends Base
20
{
21
    /**
22
    * @var bool
23
    */
24
    private $nestedReadable = false;
25
26
    /**
27
    * @var bool
28
    */
29
    private $nestedWriteable = false;
30
31
    /**
32
    * @var IntegerType
33
    */
34
    private $nestedType = null;
35
36
    public function __construct(ClassReflection $classReflection, Broker $broker, string $property)
37
    {
38
        parent::__construct($classReflection, $broker, $property);
39
40
        if (
41
            in_array($property, [
42
                'intNestedLeft',
43
                'intNestedRight',
44
                'intNestedLevel',
45
                'intNestedParentId',
46
            ])
47
        ) {
48
            $this->nestedReadable = true;
49
            $this->nestedWriteable = is_a(
50
                $classReflection->getNativeReflection()->getName(),
51
                DaftNestedWriteableObject::class,
52
                true
53
            );
54
        }
55
56
        if (
57
            in_array($property, [
58
                'intNestedLeft',
59
                'intNestedRight',
60
                'intNestedLevel',
61
            ])
62
        ) {
63
            $this->nestedType = new IntegerType();
64
        }
65
    }
66
67
    public function isReadable() : bool
68
    {
69
        return $this->nestedReadable || parent::isReadable();
70
    }
71
72
    public function isWritable() : bool
73
    {
74
        return $this->nestedWriteable || parent::isWritable();
75
    }
76
77
    public function getType() : Type
78
    {
79
        return $this->nestedType ?? parent::getType();
80
    }
81
82
    protected static function PropertyIsPublic(string $className, string $property) : bool
83
    {
84
        return
85
            in_array($property, [
86
                'intNestedLeft',
87
                'intNestedRight',
88
                'intNestedLevel',
89
                'intNestedParentId',
90
            ]) ||
91
            parent::PropertyIsPublic($className, $property);
92
    }
93
}
94