AddPropertiesConst   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 12
loc 52
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A leaveNode() 12 12 2
A buildProperty() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 PhpParser\Node;
15
use PhpParser\NodeVisitorAbstract;
16
17
use function Cycle\ORM\Promise\inject;
18
19
/**
20
 * Add const with properties list
21
 */
22
final class AddPropertiesConst extends NodeVisitorAbstract
23
{
24
    /** @var string */
25
    private $name;
26
27
    /** @var array */
28
    private $values;
29
30
    /**
31
     * @param string $name
32
     * @param array  $values
33
     */
34
    public function __construct(string $name, array $values)
35
    {
36
        $this->name = $name;
37
        $this->values = $values;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 View Code Duplication
    public function leaveNode(Node $node)
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...
44
    {
45
        if ($node instanceof Node\Stmt\Class_) {
46
            $node->stmts = inject(
47
                $node->stmts,
48
                Node\Stmt\ClassMethod::class,
49
                [$this->buildProperty()]
50
            );
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * @return Node\Stmt\ClassConst
58
     */
59
    private function buildProperty(): Node\Stmt\ClassConst
60
    {
61
        $array = [];
62
        foreach ($this->values as $value) {
63
            $array[] = new Node\Expr\ArrayItem(new Node\Scalar\String_($value));
64
        }
65
66
        return new Node\Stmt\ClassConst([
67
            new Node\Const_(
68
                $this->name,
69
                new Node\Expr\Array_($array, ['kind' => Node\Expr\Array_::KIND_SHORT])
70
            )
71
        ], Node\Stmt\Class_::MODIFIER_PRIVATE);
72
    }
73
}
74