Completed
Branch master (f2a445)
by Valentin
01:50
created

AddMagicCloneMethod::buildCloneExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
declare(strict_types=1);
10
11
namespace Cycle\ORM\Promise\Visitor;
12
13
use Cycle\ORM\Promise\Expressions;
14
use PhpParser\Builder;
15
use PhpParser\Node;
16
use PhpParser\NodeVisitorAbstract;
17
18
final class AddMagicCloneMethod extends NodeVisitorAbstract
19
{
20
    /** @var string */
21
    private $resolverProperty;
22
23
    /** @var bool */
24
    private $hasClone;
25
26
    /**
27
     * @param string $resolverProperty
28
     * @param bool   $hasClone
29
     */
30
    public function __construct(string $resolverProperty, bool $hasClone)
31
    {
32
        $this->resolverProperty = $resolverProperty;
33
        $this->hasClone = $hasClone;
34
    }
35
36
    /**
37
     * @param Node $node
38
     * @return int|Node|Node[]|null
39
     */
40
    public function leaveNode(Node $node)
41
    {
42
        if ($this->hasClone) {
43
            return null;
44
        }
45
46
        if ($node instanceof Node\Stmt\Class_) {
47
            $method = new Builder\Method('__clone');
48
            $method->makePublic();
49
            $method->addStmt(Expressions::buildCloneExpression($this->resolverProperty));
50
51
            $node->stmts[] = $method->getNode();
52
        }
53
54
        return null;
55
    }
56
}
57