Completed
Pull Request — master (#3)
by Valentin
01:55
created

AddPromiseMethod   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A leaveNode() 0 16 3
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;
9
use PhpParser\Node;
10
use PhpParser\NodeVisitorAbstract;
11
12
class AddPromiseMethod extends NodeVisitorAbstract
13
{
14
    /** @var string */
15
    private $resolverProperty;
16
17
    /** @var string */
18
    private $name;
19
20
    /** @var string|null */
21
    private $returnType;
22
23
    public function __construct(string $resolverProperty, string $name, string $returnType = null)
24
    {
25
        $this->resolverProperty = $resolverProperty;
26
        $this->name = $name;
27
        $this->returnType = $returnType;
28
    }
29
30
    public function leaveNode(Node $node)
31
    {
32
        if ($node instanceof Node\Stmt\Class_) {
33
            $method = new Builder\Method($this->name);
34
            $method->makePublic();
35
            $method->addStmt(new Node\Stmt\Return_(Expressions::resolveMethodCall('this', $this->resolverProperty, $this->name)));
36
            if ($this->returnType !== null) {
37
                $method->setReturnType($this->returnType);
38
            }
39
            $method->setDocComment(PHPDoc::writeInheritdoc());
40
41
            $node->stmts[] = $method->getNode();
42
        }
43
44
        return null;
45
    }
46
}