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

AddProxiedMethods::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Visitor;
5
6
use Cycle\ORM\Promise\PHPDoc;
7
use PhpParser\Node;
8
use PhpParser\NodeVisitorAbstract;
9
10
/**
11
 * Add parent call via proxy resolver
12
 */
13
class AddProxiedMethods extends NodeVisitorAbstract
14
{
15
    /** @var string */
16
    private $property;
17
18
    /** @var Node\Stmt\ClassMethod[] */
19
    private $methods;
20
21
    public function __construct(string $property, array $methods)
22
    {
23
        $this->property = $property;
24
        $this->methods = $methods;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function leaveNode(Node $node)
31
    {
32
        if (!$node instanceof Node\Stmt\Class_) {
33
            return null;
34
        }
35
36
        foreach ($this->methods as $method) {
37
            $node->stmts[] = $this->modifyMethod($method);
38
        }
39
40
        return $node;
41
    }
42
43
    private function modifyMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassMethod
44
    {
45
        $method->setDocComment(PHPDoc::writeInheritdoc());
46
        $method->stmts = [
47
            new Node\Stmt\Return_(
48
                new Node\Expr\MethodCall(
49
                    new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $this->property),
50
                    $method->name->name
51
                )
52
            )
53
        ];
54
55
        return $method;
56
    }
57
}