Passed
Push — master ( 503081...57dad7 )
by Valentin
51s
created

AddMagicGetMethod::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 AddMagicGetMethod 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 View Code Duplication
    private function buildGetExpression(): Node\Stmt\If_
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $resolved = Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod);
42
        $stmt = new Node\Stmt\Return_(new Node\Expr\PropertyFetch($resolved, '{$name}'));
43
44
        return Expressions::throwExceptionOnNull($resolved, $stmt);
45
    }
46
}