Passed
Push — master ( 414a22...f0b2e3 )
by Jesse
03:26
created

ReflectionProperty   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 38
rs 10
c 0
b 0
f 0
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 ReflectionProperty as BaseReflectionProperty;
6
use function sprintf;
7
8
/**
9
 * ReflectionProperty that is automatically made accessible and adds a level
10
 * suffix to the property name.
11
 *
12
 * @internal
13
 * @author Stratadox
14
 */
15
final class ReflectionProperty extends BaseReflectionProperty
16
{
17
    private $level;
18
19
    private function __construct($class, string $name, int $level)
20
    {
21
        parent::__construct($class, $name);
22
        $this->setAccessible(true);
23
        $this->level = $level;
24
    }
25
26
    /**
27
     * Transforms a base ReflectionProperty into a custom ReflectionProperty.
28
     *
29
     * @param BaseReflectionProperty $property The property to convert.
30
     * @param int                    $level    The inheritance deepness.
31
     * @return ReflectionProperty              The converted property.
32
     */
33
    public static function from(
34
        BaseReflectionProperty $property,
35
        int $level
36
    ): ReflectionProperty {
37
        return new ReflectionProperty(
38
            $property->class,
39
            $property->name,
40
            $level
41
        );
42
    }
43
44
    public function getName(): string
45
    {
46
        if ($this->level === 0) {
47
            return parent::getName();
48
        }
49
        return sprintf(
50
            '%s{%d}',
51
            parent::getName(),
52
            $this->level
53
        );
54
    }
55
}
56