AddMagicGetMethod::buildGetExpression()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise\Visitor;
13
14
use PhpParser\Builder;
15
use PhpParser\Node;
16
use PhpParser\NodeVisitorAbstract;
17
18
use function Cycle\ORM\Promise\resolveMethodCall;
19
use function Cycle\ORM\Promise\throwExceptionOnNull;
20
21
final class AddMagicGetMethod extends NodeVisitorAbstract
22
{
23
    /** @var string */
24
    private $class;
25
26
    /** @var string */
27
    private $resolverProperty;
28
29
    /** @var string */
30
    private $resolveMethod;
31
32
    /**
33
     * @param string $class
34
     * @param string $resolverProperty
35
     * @param string $resolveMethod
36
     */
37
    public function __construct(string $class, string $resolverProperty, string $resolveMethod)
38
    {
39
        $this->class = $class;
40
        $this->resolverProperty = $resolverProperty;
41
        $this->resolveMethod = $resolveMethod;
42
    }
43
44
    /**
45
     * @param Node $node
46
     * @return int|Node|Node[]|null
47
     */
48
    public function leaveNode(Node $node)
49
    {
50
        if ($node instanceof Node\Stmt\Class_) {
51
            $method = new Builder\Method('__get');
52
            $method->makePublic();
53
            $method->addParam(new Builder\Param('name'));
54
            $method->addStmt($this->buildGetExpression());
55
56
            $node->stmts[] = $method->getNode();
57
        }
58
59
        return null;
60
    }
61
62
    /**
63
     * @return Node\Stmt\If_
64
     */
65 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...
66
    {
67
        $resolved = resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod);
68
        $stmt = new Node\Stmt\Return_(new Node\Expr\PropertyFetch($resolved, '{$name}'));
69
70
        return throwExceptionOnNull(
71
            $resolved,
72
            $stmt,
73
            'Property `%s` not loaded in `__get()` method for `%s`',
74
            [
75
                new Node\Arg(new Node\Expr\Variable('name')),
76
                $this->class
77
            ]
78
        );
79
    }
80
}
81