DeclareClass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A leaveNode() 0 12 3
A canBeImplemented() 0 11 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\Visitor;
13
14
use PhpParser\Node;
15
use PhpParser\NodeVisitorAbstract;
16
17
/**
18
 * Declare proxy class, add extends and implements declarations
19
 */
20
final class DeclareClass extends NodeVisitorAbstract
21
{
22
    /** @var string */
23
    private $name;
24
25
    /** @var string */
26
    private $extends;
27
28
    /** @var string */
29
    private $implements;
30
31
    /**
32
     * @param string $name
33
     * @param string $extends
34
     * @param string $implements
35
     */
36
    public function __construct(string $name, string $extends, string $implements)
37
    {
38
        $this->name = $name;
39
        $this->extends = $extends;
40
        $this->implements = $implements;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function leaveNode(Node $node)
47
    {
48
        if ($node instanceof Node\Stmt\Class_) {
49
            $node->extends = new Node\Name($this->extends);
50
            $node->name->name = $this->name;
51
            if ($this->canBeImplemented($node)) {
52
                $node->implements[] = new Node\Name($this->implements);
53
            }
54
        }
55
56
        return null;
57
    }
58
59
    /**
60
     * @param Node\Stmt\Class_ $node
61
     * @return bool
62
     */
63
    private function canBeImplemented(Node\Stmt\Class_ $node): bool
64
    {
65
        foreach ($node->implements as $implement) {
66
            $name = implode('\\', $implement->parts);
67
            if ($name === $this->implements) {
68
                return false;
69
            }
70
        }
71
72
        return true;
73
    }
74
}
75