AddPromiseMethod::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Cycle\ORM\Promise\PHPDoc;
15
use PhpParser\Builder;
16
use PhpParser\Node;
17
use PhpParser\NodeVisitorAbstract;
18
19
use function Cycle\ORM\Promise\resolveMethodCall;
20
21
final class AddPromiseMethod extends NodeVisitorAbstract
22
{
23
    /** @var string */
24
    private $resolverProperty;
25
26
    /** @var string */
27
    private $name;
28
29
    /** @var string|null */
30
    private $returnType;
31
32
    /**
33
     * @param string      $resolverProperty
34
     * @param string      $name
35
     * @param string|null $returnType
36
     */
37
    public function __construct(string $resolverProperty, string $name, string $returnType = null)
38
    {
39
        $this->resolverProperty = $resolverProperty;
40
        $this->name = $name;
41
        $this->returnType = $returnType;
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($this->name);
52
            $method->makePublic();
53
            $method->addStmt(new Node\Stmt\Return_(resolveMethodCall(
54
                'this',
55
                $this->resolverProperty,
56
                $this->name
57
            )));
58
            if ($this->returnType !== null) {
59
                $method->setReturnType($this->returnType);
60
            }
61
            $method->setDocComment(PHPDoc::writeInheritdoc());
62
63
            $node->stmts[] = $method->getNode();
64
        }
65
66
        return null;
67
    }
68
}
69