Passed
Push — master ( d3a94c...76e109 )
by Valentin
05:50
created

AddResolverProperty   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A leaveNode() 0 8 2
A definePlacementID() 0 10 3
A buildProperty() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Visitor;
5
6
use Cycle\ORM\Promise\PHPDoc;
7
use Cycle\ORM\Promise\Utils;
8
use PhpParser\Builder\Property;
9
use PhpParser\Node;
10
use PhpParser\NodeVisitorAbstract;
11
12
/**
13
 * Add resolver property
14
 */
15
class AddResolverProperty extends NodeVisitorAbstract
16
{
17
    /** @var string */
18
    private $property;
19
20
    /** @var string */
21
    private $type;
22
23
    /** @var string */
24
    private $class;
25
26
    public function __construct(string $property, string $type, string $class)
27
    {
28
        $this->property = $property;
29
        $this->type = $type;
30
        $this->class = $class;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function leaveNode(Node $node)
37
    {
38
        if ($node instanceof Node\Stmt\Class_) {
39
            $node->stmts = Utils::injectValues($node->stmts, $this->definePlacementID($node), [$this->buildProperty()]);
40
        }
41
42
        return null;
43
    }
44
45
    private function definePlacementID(Node\Stmt\Class_ $node): int
46
    {
47
        foreach ($node->stmts as $index => $child) {
48
            if ($child instanceof Node\Stmt\ClassMethod) {
49
                return $index;
50
            }
51
        }
52
53
        return 0;
54
    }
55
56
    private function buildProperty(): Node\Stmt\Property
57
    {
58
        $property = new Property($this->property);
59
        $property->makePrivate();
60
        $property->setDocComment(PHPDoc::writeProperty("{$this->type}|{$this->class}"));
61
62
        return $property->getNode();
63
    }
64
}