Completed
Push — master ( f04f52...152406 )
by Anton
03:17
created

src/Visitor/AddMagicGetMethod.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license MIT
6
 * @author  Valentin V (Vvval)
7
 */
8
declare(strict_types=1);
9
10
namespace Cycle\ORM\Promise\Visitor;
11
12
use Cycle\ORM\Promise\Expressions;
13
use PhpParser\Builder;
14
use PhpParser\Node;
15
use PhpParser\NodeVisitorAbstract;
16
17
final class AddMagicGetMethod extends NodeVisitorAbstract
18
{
19
    /** @var string */
20
    private $resolverProperty;
21
22
    /** @var string */
23
    private $resolveMethod;
24
25
    /**
26
     * @param string $resolverProperty
27
     * @param string $resolveMethod
28
     */
29
    public function __construct(string $resolverProperty, string $resolveMethod)
30
    {
31
        $this->resolverProperty = $resolverProperty;
32
        $this->resolveMethod = $resolveMethod;
33
    }
34
35
    /**
36
     * @param Node $node
37
     * @return int|Node|Node[]|null
38
     */
39
    public function leaveNode(Node $node)
40
    {
41
        if ($node instanceof Node\Stmt\Class_) {
42
            $method = new Builder\Method('__get');
43
            $method->makePublic();
44
            $method->addParam(new Builder\Param('name'));
45
            $method->addStmt($this->buildGetExpression());
46
47
            $node->stmts[] = $method->getNode();
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * @return Node\Stmt\If_
55
     */
56 View Code Duplication
    private function buildGetExpression(): Node\Stmt\If_
0 ignored issues
show
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...
57
    {
58
        $resolved = Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod);
59
        $stmt = new Node\Stmt\Return_(new Node\Expr\PropertyFetch($resolved, '{$name}'));
60
61
        return Expressions::throwExceptionOnNull($resolved, $stmt);
62
    }
63
}