Extractor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A extract() 0 13 2
A hasCloneMethod() 0 14 3
1
<?php
2
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise\Declaration;
13
14
use Cycle\ORM\Promise\Declaration\Extractor\Constants;
15
use Cycle\ORM\Promise\Declaration\Extractor\Methods;
16
use Cycle\ORM\Promise\Declaration\Extractor\Properties;
17
use ReflectionClass;
18
use ReflectionException;
19
20
final class Extractor
21
{
22
    /** @var Extractor\Methods */
23
    private $methods;
24
25
    /** @var Extractor\Properties */
26
    private $properties;
27
28
    /** @var Extractor\Constants */
29
    private $constants;
30
31
    /** @var array  */
32
    private $reflectionStructureCache = [];
33
34
    /**
35
     * @param Constants  $constants
36
     * @param Properties $properties
37
     * @param Methods    $methods
38
     */
39
    public function __construct(
40
        Extractor\Constants $constants,
41
        Extractor\Properties $properties,
42
        Extractor\Methods $methods
43
    ) {
44
        $this->constants = $constants;
45
        $this->properties = $properties;
46
        $this->methods = $methods;
47
    }
48
49
    /**
50
     * @param ReflectionClass $reflection
51
     * @return Structure
52
     * @throws ReflectionException
53
     */
54
    public function extract(ReflectionClass $reflection): Structure
55
    {
56
        if (!isset($this->reflectionStructureCache[$reflection->name])) {
57
            $this->reflectionStructureCache[$reflection->name] = Structure::create(
58
                $this->constants->getConstants($reflection),
59
                $this->properties->getProperties($reflection),
60
                $this->hasCloneMethod($reflection),
61
                ...$this->methods->getMethods($reflection)
62
            );
63
        }
64
65
        return $this->reflectionStructureCache[$reflection->name];
66
    }
67
68
    /**
69
     * @param ReflectionClass $reflection
70
     * @return bool
71
     */
72
    private function hasCloneMethod(ReflectionClass $reflection): bool
73
    {
74
        if (!$reflection->hasMethod('__clone')) {
75
            return false;
76
        }
77
78
        try {
79
            $cloneMethod = $reflection->getMethod('__clone');
80
        } catch (ReflectionException $exception) {
81
            return false;
82
        }
83
84
        return !$cloneMethod->isPrivate();
85
    }
86
}
87