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

UpdatePromiseMethods::resolverCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Visitor;
5
6
use Cycle\ORM\Promise\Expressions;
7
use PhpParser\Node;
8
use PhpParser\NodeVisitorAbstract;
9
10
/**
11
 * Modify all accessible methods
12
 */
13
class UpdatePromiseMethods extends NodeVisitorAbstract
14
{
15
    /** @var string */
16
    private $property;
17
18
    public function __construct(string $property)
19
    {
20
        $this->property = $property;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function leaveNode(Node $node)
27
    {
28
        if ($node instanceof Node\Stmt\ClassMethod && !$this->ignoreMethod($node)) {
29
            $node->stmts = [new Node\Stmt\Return_($this->resolvedParentMethodCall($node))];
30
        }
31
32
        return null;
33
    }
34
35
    private function ignoreMethod(Node\Stmt\ClassMethod $node): bool
36
    {
37
        return $node->isPrivate() || $node->isStatic() || $node->isFinal() || $node->isAbstract() || $node->isMagic();
38
    }
39
40
    private function resolvedParentMethodCall(Node\Stmt\ClassMethod $node): Node\Expr\MethodCall
41
    {
42
        return new Node\Expr\MethodCall(Expressions::resolvePropertyFetch('this', $this->property), $node->name->name);
43
    }
44
}