PropertyGenerator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 3
loc 60
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A defValue() 0 6 1
A code() 3 18 4

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 declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:30
5
 */
6
namespace samsonframework\generator;
7
8
/**
9
 * Class property generation class.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class PropertyGenerator extends AbstractGenerator
14
{
15
    use VisibilityTrait;
16
17
    /** @var string Property name */
18
    protected $name;
19
20
    /** @var string Property value */
21
    protected $value;
22
23
    /**
24
     * PropertyGenerator constructor.
25
     *
26
     * @param string                 $name   Property name
27
     * @param mixed                  $value  Property value
28
     * @param AbstractGenerator|null $parent Parent generator
29
     */
30 4
    public function __construct(string $name, $value = null, AbstractGenerator $parent = null)
31
    {
32 4
        $this->name = $name;
33 4
        $this->value = $this->parseValue($value);
34
35 4
        parent::__construct($parent);
36 4
    }
37
38
    /**
39
     * Set property value.
40
     *
41
     * @param mixed $value Value
42
     * @return $this|PropertyGenerator
43
     */
44
    public function defValue($value) : PropertyGenerator
45
    {
46
        $this->value = $this->parseValue($value);
47
48
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function code(): string
55
    {
56 3
        $code = $this->indentation($this->indentation)
57 3
            . $this->visibility
58 3
            . ' '
59 3
            . ($this->isStatic ? 'static ' : '')
60 3
            . '$'
61 3
            . $this->name
62 3
            . ($this->value !== null ? ' = ' . $this->parseValue($this->value) : '')
63 3
            . ';';
64
65
        // Add comments
66 3 View Code Duplication
        if (array_key_exists(CommentsGenerator::class, $this->generatedCode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67
            $code = $this->generatedCode[CommentsGenerator::class] . "\n" . $code;
68
        }
69
70 3
        return $code;
71
    }
72
}
73