Completed
Push — master ( 3c68a7...9aacf2 )
by Pieter
11:31
created

isAllowedProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace W2w\Lib\Apie\Plugins\Core\PropertyInfo;
4
5
use ReflectionMethod;
6
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor as SymfonyReflectionExtractor;
7
8
class ReflectionExtractorSerializer4 extends SymfonyReflectionExtractor
9
{
10
    private $accessFlags;
11
12
    public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null, bool $enableConstructorExtraction = true, int $accessFlags = self::ALLOW_PUBLIC)
13
    {
14
        $this->accessFlags = $accessFlags;
15
        parent::__construct($mutatorPrefixes, $accessorPrefixes, $arrayMutatorPrefixes, $enableConstructorExtraction, $accessFlags);
16
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function isReadable($class, $property, array $context = [])
22
    {
23
        if ($this->isAllowedProperty($class, $property)) {
24
            return true;
25
        }
26
27
        list($reflectionMethod) = $this->getAccessorMethod($class, $property);
28
29
        return null !== $reflectionMethod;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function isWritable($class, $property, array $context = [])
36
    {
37
        if ($this->isAllowedProperty($class, $property)) {
38
            return true;
39
        }
40
41
        list($reflectionMethod) = $this->getMutatorMethod($class, $property);
42
43
        return null !== $reflectionMethod;
44
    }
45
46
    private function isAllowedProperty(string $class, string $property): bool
47
    {
48
        $refl = new ReflectionMethod(SymfonyReflectionExtractor::class, 'isAllowedProperty');
49
        $refl->setAccessible(true);
50
        return $refl->invoke($this, $class, $property);
51
    }
52
53
54
    private function getAccessorMethod(string $class, string $property): ?array
55
    {
56
        $refl = new ReflectionMethod(SymfonyReflectionExtractor::class, 'getAccessorMethod');
57
        $refl->setAccessible(true);
58
        $result = $refl->invoke($this, $class, $property);
59
        return $this->filterMethodOnAccessFlag($result);
60
    }
61
62
    private function getMutatorMethod(string $class, string $property): ?array
63
    {
64
        $refl = new ReflectionMethod(SymfonyReflectionExtractor::class, 'getMutatorMethod');
65
        $refl->setAccessible(true);
66
        $result = $refl->invoke($this, $class, $property);
67
        return $this->filterMethodOnAccessFlag($result);
68
    }
69
70
    private function filterMethodOnAccessFlag(?array $result): ?array
71
    {
72
        if (null === $result) {
0 ignored issues
show
introduced by
The condition null === $result is always false.
Loading history...
73
            return null;
74
        }
75
        /** @var ReflectionMethod $method */
76
        $method = $result[0];
77
        if ($this->accessFlags & self::ALLOW_PUBLIC && $method->isPublic()) {
78
            return $result;
79
        }
80
81
        if ($this->accessFlags & self::ALLOW_PROTECTED && $method->isProtected()) {
82
            return $result;
83
        }
84
85
        if ($this->accessFlags & self::ALLOW_PRIVATE && $method->isPrivate()) {
86
            return $result;
87
        }
88
        return null;
89
    }
90
}
91