Passed
Push — master ( 327a11...1a5769 )
by Valentin
58s
created

AddMagicGet::__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\Expressions;
7
use PhpParser\Builder;
8
use PhpParser\Node;
9
use PhpParser\NodeVisitorAbstract;
10
11
class AddMagicGet extends NodeVisitorAbstract
12
{
13
    /** @var string */
14
    private $resolverProperty;
15
16
    /** @var string */
17
    private $resolveMethod;
18
19
    public function __construct(string $resolverProperty, string $resolveMethod)
20
    {
21
        $this->resolverProperty = $resolverProperty;
22
        $this->resolveMethod = $resolveMethod;
23
    }
24
25
    public function leaveNode(Node $node)
26
    {
27
        if ($node instanceof Node\Stmt\Class_) {
28
            $method = new Builder\Method('__get');
29
            $method->makePublic();
30
            $method->addParam(new Builder\Param('name'));
31
            $method->addStmt($this->buildGetExpression());
32
33
            $node->stmts[] = $method->getNode();
34
        }
35
36
        return null;
37
    }
38
39
    private function buildGetExpression(): Node\Stmt\Return_
40
    {
41
        return new Node\Stmt\Return_(
42
            new Node\Expr\PropertyFetch(Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod), '{$name}')
43
        );
44
    }
45
}