ReflectionProperty   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 9 2
A __construct() 0 5 1
A from() 0 8 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\EntityState\Internal;
4
5
use ReflectionException;
6
use ReflectionProperty as BaseReflectionProperty;
7
use function sprintf;
8
9
/**
10
 * ReflectionProperty that is automatically made accessible and adds a level
11
 * suffix to the property name.
12
 *
13
 * @internal
14
 * @author Stratadox
15
 */
16
final class ReflectionProperty extends BaseReflectionProperty
17
{
18
    private $level;
19
20
    private function __construct($class, string $name, int $level)
21
    {
22
        parent::__construct($class, $name);
23
        $this->setAccessible(true);
24
        $this->level = $level;
25
    }
26
27
    /**
28
     * Transforms a base ReflectionProperty into a custom ReflectionProperty.
29
     *
30
     * @param BaseReflectionProperty $property The property to convert.
31
     * @param int                    $level    The inheritance deepness.
32
     * @return ReflectionProperty              The converted property.
33
     * @throws ReflectionException             When the property cannot be
34
     *                                         accessed.
35
     */
36
    public static function from(
37
        BaseReflectionProperty $property,
38
        int $level
39
    ): ReflectionProperty {
40
        return new ReflectionProperty(
41
            $property->class,
42
            $property->name,
43
            $level
44
        );
45
    }
46
47
    public function getName(): string
48
    {
49
        if ($this->level === 0) {
50
            return parent::getName();
51
        }
52
        return sprintf(
53
            '%s{%d}',
54
            parent::getName(),
55
            $this->level
56
        );
57
    }
58
}
59