Passed
Push — master ( e1087f...636362 )
by SignpostMarv
03:25
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\Type\IntegerType;
14
use PHPStan\Type\Type;
15
use SignpostMarv\DaftObject\DaftNestedWriteableObject;
16
use SignpostMarv\DaftObject\PHPStan\PropertyReflectionExtension as Base;
17
18
class PropertyReflectionExtension extends Base
19
{
20
    /**
21
    * @var bool
22
    */
23
    private $nestedReadable = false;
24
25
    /**
26
    * @var bool
27
    */
28
    private $nestedWriteable = false;
29
30
    /**
31
    * @var IntegerType|null
32
    */
33
    private $nestedType = null;
34
35
    public function __construct(ClassReflection $classReflection, Broker $broker, string $property)
36
    {
37
        parent::__construct($classReflection, $broker, $property);
38
39
        $intProperty = static::PropertyIsInt($property);
40
41
        if ('intNestedParentId' === $property || $intProperty) {
42
            $this->nestedReadable = true;
43
            $this->nestedWriteable = is_a(
44
                $classReflection->getNativeReflection()->getName(),
45
                DaftNestedWriteableObject::class,
46
                true
47
            );
48
        }
49
50
        if ($intProperty) {
51
            $this->nestedType = new IntegerType();
52
        }
53
    }
54
55
    public static function PropertyIsInt(string $property) : bool
56
    {
57
        return in_array(
58
            $property,
59
            [
60
                'intNestedLeft',
61
                'intNestedRight',
62
                'intNestedLevel',
63
                'intNestedSortOrder',
64
            ],
65
            true
66
        );
67
    }
68
69
    public function isReadable() : bool
70
    {
71
        return $this->nestedReadable || parent::isReadable();
72
    }
73
74
    public function isWritable() : bool
75
    {
76
        return $this->nestedWriteable || parent::isWritable();
77
    }
78
79
    public function getType() : Type
80
    {
81
        return $this->nestedType ?? parent::getType();
82
    }
83
84
    protected static function PropertyIsPublic(string $className, string $property) : bool
85
    {
86
        return
87
            static::PropertyIsInt($property) ||
88
            parent::PropertyIsPublic($className, $property);
89
    }
90
}
91