Completed
Push — master ( 5880f8...5ed87e )
by Karsten
02:08
created

ClassMirror   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 8
c 2
b 0
f 2
lcom 1
cbo 1
dl 0
loc 46
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getAccessors() 0 23 5
A ensureReflection() 0 4 2
1
<?php
2
/**
3
 * Created by gerk on 24.11.16 22:37
4
 */
5
6
namespace PeekAndPoke\Component\PropertyAccess;
7
8
/**
9
 * ClassMirror
10
 *
11
 * @author Karsten J. Gerber <[email protected]>
12
 */
13
class ClassMirror
14
{
15
    /** @var PropertyAccessFactoryImpl */
16
    private $factory;
17
18
    /**
19
     */
20 7
    public function __construct()
21
    {
22 7
        $this->factory = new PropertyAccessFactoryImpl();
23 7
    }
24
25
    /**
26
     * @param mixed $subject
27
     *
28
     * @return PropertyAccess[]
29
     */
30 7
    public function getAccessors($subject)
31
    {
32 7
        $class = self::ensureReflection($subject);
33
34 7
        $result  = [];
35 7
        $current = $class;
36
37 7
        while ($current) {
38
39 7
            foreach ($current->getProperties() as $property) {
40
41 7
                $propertyName = $property->getName();
42
43 7
                if (false === isset($result[$propertyName]) && false === $property->isStatic()) {
44 7
                    $result[$propertyName] = $this->factory->create($class, $property);
45
                }
46
            }
47
48 7
            $current = $current->getParentClass();
49
        }
50
51 7
        return $result;
52
    }
53
54 7
    private static function ensureReflection($subject) : \ReflectionClass
55
    {
56 7
        return $subject instanceof \ReflectionClass ? $subject : new \ReflectionClass($subject);
57
    }
58
}
59