Completed
Push — master ( f04f52...152406 )
by Anton
03:17
created

src/Visitor/AddMagicCloneMethod.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license MIT
6
 * @author  Valentin V (Vvval)
7
 */
8
declare(strict_types=1);
9
10
namespace Cycle\ORM\Promise\Visitor;
11
12
use Cycle\ORM\Promise\Expressions;
13
use PhpParser\Builder;
14
use PhpParser\Node;
15
use PhpParser\NodeVisitorAbstract;
16
17
final class AddMagicCloneMethod extends NodeVisitorAbstract
18
{
19
    /** @var string */
20
    private $resolverProperty;
21
22
    /** @var bool */
23
    private $hasClone;
24
25
    /**
26
     * @param string $resolverProperty
27
     * @param bool   $hasClone
28
     */
29
    public function __construct(string $resolverProperty, bool $hasClone)
30
    {
31
        $this->resolverProperty = $resolverProperty;
32
        $this->hasClone = $hasClone;
33
    }
34
35
    /**
36
     * @param Node $node
37
     * @return int|Node|Node[]|null
38
     */
39
    public function leaveNode(Node $node)
40
    {
41
        if ($this->hasClone) {
42
            return null;
43
        }
44
45
        if ($node instanceof Node\Stmt\Class_) {
46
            $method = new Builder\Method('__clone');
47
            $method->makePublic();
48
            $method->addStmt($this->buildCloneExpression());
49
50
            $node->stmts[] = $method->getNode();
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * @return Node\Stmt\Expression
58
     */
59 View Code Duplication
    private function buildCloneExpression(): Node\Stmt\Expression
0 ignored issues
show
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...
60
    {
61
        return new Node\Stmt\Expression(
62
            new Node\Expr\Assign(
63
                Expressions::resolvePropertyFetch('this', $this->resolverProperty),
64
                new Node\Expr\Clone_(Expressions::resolvePropertyFetch('this', $this->resolverProperty))
65
            )
66
        );
67
    }
68
}