Completed
Push — master ( e68f0a...480c88 )
by Valentin
02:23 queued 10s
created

AddProxiedMethods::modifyExprMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
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 Cycle\ORM\Promise\PHPDoc;
8
use PhpParser\Builder\Param;
9
use PhpParser\Node;
10
use PhpParser\NodeVisitorAbstract;
11
12
/**
13
 * Add parent call via proxy resolver
14
 */
15
class AddProxiedMethods extends NodeVisitorAbstract
16
{
17
    /** @var string */
18
    private $resolverProperty;
19
20
    /** @var Node\Stmt\ClassMethod[] */
21
    private $methods;
22
23
    /** @var string */
24
    private $resolveMethod;
25
26
    public function __construct(string $property, array $methods, string $resolveMethod)
27
    {
28
        $this->resolverProperty = $property;
29
        $this->methods = $methods;
30
        $this->resolveMethod = $resolveMethod;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function leaveNode(Node $node)
37
    {
38
        if (!$node instanceof Node\Stmt\Class_) {
39
            return null;
40
        }
41
42
        foreach ($this->methods as $method) {
43
            if ($method->name->name === '__clone') {
44
                $method->stmts = [$this->buildCloneExpression()];
45
                $node->stmts[] = $method;
46
            } elseif ($this->hasReturnStmt($method)) {
47
                $node->stmts[] = $this->modifyReturnMethod($method);
48
            } else {
49
                $node->stmts[] = $this->modifyExprMethod($method);
50
            }
51
        }
52
53
        return $node;
54
    }
55
56 View Code Duplication
    private function buildCloneExpression(): Node\Stmt\Expression
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...
57
    {
58
        return new Node\Stmt\Expression(
59
            new Node\Expr\Assign(
60
                Expressions::resolvePropertyFetch('this', $this->resolverProperty),
61
                new Node\Expr\Clone_(Expressions::resolvePropertyFetch('this', $this->resolverProperty))
62
            )
63
        );
64
    }
65
66 View Code Duplication
    private function modifyReturnMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassMethod
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...
67
    {
68
        $method->setDocComment(PHPDoc::writeInheritdoc());
69
70
        $resolved = Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod);
71
        $stmt = new Node\Stmt\Return_(new Node\Expr\MethodCall($resolved, $method->name->name, $this->packMethodArgs($method)));
72
73
        $method->stmts[] = Expressions::throwExceptionOnNull($resolved, $stmt);
74
75
        return $method;
76
    }
77
78 View Code Duplication
    private function modifyExprMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassMethod
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...
79
    {
80
        $method->setDocComment(PHPDoc::writeInheritdoc());
81
82
        $resolved = Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod);
83
        $stmt = new Node\Stmt\Expression(new Node\Expr\MethodCall($resolved, $method->name->name, $this->packMethodArgs($method)));
84
85
        $method->stmts[] = Expressions::throwExceptionOnNull($resolved, $stmt);
86
87
        return $method;
88
    }
89
90
    private function packMethodArgs(Node\Stmt\ClassMethod $method): array
91
    {
92
        $args = [];
93
        /** @var \PhpParser\Node\Param $param */
94
        foreach ($method->getParams() as $param) {
95
            $args[] = (new Param($param->var->name))->getNode();
96
        }
97
98
        return $args;
99
    }
100
101
    private function hasReturnStmt(Node\Stmt\ClassMethod $method): bool
102
    {
103
        if ($method->returnType === null || $method->returnType === 'void') {
104
            return false;
105
        }
106
107
        if ($method->returnType instanceof Node\NullableType) {
108
            return true;
109
        }
110
111
        return $method->returnType instanceof Node\Identifier && $method->returnType->name !== 'void';
112
    }
113
}