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

AddMagicSet::leaveNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 AddMagicSet 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('__set');
29
            $method->makePublic();
30
            $method->addParams([new Builder\Param('name'), new Builder\Param('value')]);
31
            $method->addStmt($this->buildSetExpression());
32
33
            $node->stmts[] = $method->getNode();
34
        }
35
36
        return null;
37
    }
38
39
    private function buildSetExpression(): Node\Stmt\Expression
40
    {
41
        return new Node\Stmt\Expression(
42
            new Node\Expr\Assign(
43
                new Node\Expr\PropertyFetch(Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod), '{$name}'),
44
                new Node\Expr\Variable('value')
45
            )
46
        );
47
    }
48
}