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

AddMagicClone::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
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Visitor;
5
6
use Cycle\ORM\Promise\Expressions;
7
use PhpParser\Builder;
8
use PhpParser\Node;
9
use PhpParser\NodeVisitorAbstract;
10
11
class AddMagicClone extends NodeVisitorAbstract
12
{
13
    /** @var string */
14
    private $resolverProperty;
15
    private $hasClone;
16
17
    public function __construct(string $resolverProperty, bool $hasClone)
18
    {
19
        $this->resolverProperty = $resolverProperty;
20
        $this->hasClone = $hasClone;
21
    }
22
23
    public function leaveNode(Node $node)
24
    {
25
        if ($this->hasClone) {
26
            return null;
27
        }
28
29
        if ($node instanceof Node\Stmt\Class_) {
30
            $method = new Builder\Method('__clone');
31
            $method->makePublic();
32
            $method->addStmt($this->buildCloneExpression());
33
34
            $node->stmts[] = $method->getNode();
35
        }
36
37
        return null;
38
    }
39
40 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...
41
    {
42
        return new Node\Stmt\Expression(
43
            new Node\Expr\Assign(
44
                Expressions::resolvePropertyFetch('this', $this->resolverProperty),
45
                new Node\Expr\Clone_(Expressions::resolvePropertyFetch('this', $this->resolverProperty))
46
            )
47
        );
48
    }
49
}