Completed
Push — master ( 1eb6e4...cfd01c )
by Jacob
05:16 queued 02:01
created

HasOne   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 88
Duplicated Lines 31.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 28
loc 88
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 10 3
B areDirty() 0 18 6
C calculateChangeSet() 16 28 8
A rollback() 6 12 3

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
namespace As3\Modlr\Models\Embeds;
4
5
use As3\Modlr\Models\Attributes;
6
use As3\Modlr\Models\Embed;
7
8
/**
9
 * Represents the has-one embeds of a Model.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
class HasOne extends Attributes
14
{
15
    /**
16
     * Constructor.
17
     *
18
     * @param   Embed[]   $original   Any original properties to apply.
19
     */
20
    public function __construct(array $original = [])
21
    {
22 View Code Duplication
        foreach ($original as $key => $embed) {
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...
23
            if (!$embed instanceof Embed) {
24
                continue;
25
            }
26
            $this->current[$key] = clone $embed;
27
        }
28
        parent::__construct($original);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function areDirty()
35
    {
36
        if (!empty($this->remove)) {
37
            return true;
38
        }
39
40
        foreach ($this->getCurrentAll() as $current) {
41
            if (true === $current->isDirty()) {
42
                return true;
43
            }
44
        }
45
        foreach ($this->getOriginalAll() as $original) {
46
            if (true === $original->isDirty()) {
47
                return true;
48
            }
49
        }
50
        return false;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function calculateChangeSet()
57
    {
58
        $set = [];
59 View Code Duplication
        foreach ($this->original as $key => $current) {
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...
60
            if (false === $current->isDirty()) {
61
                continue;
62
            }
63
            $original = isset($this->original[$key]) ? $this->original[$key] : null;
64
            $set[$key]['old'] = $original;
65
            $set[$key]['new'] = $current;
66
        }
67
68 View Code Duplication
        foreach ($this->current as $key => $current) {
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...
69
            if (false === $current->isDirty()) {
70
                continue;
71
            }
72
            $original = isset($this->original[$key]) ? $this->original[$key] : null;
73
            $set[$key]['old'] = $original;
74
            $set[$key]['new'] = $current;
75
        }
76
77
        foreach ($this->remove as $key) {
78
            $set[$key]['old'] = $this->original[$key];
79
            $set[$key]['new'] = null;
80
        }
81
        ksort($set);
82
        return $set;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function rollback()
89
    {
90
        $this->current = [];
91 View Code Duplication
        foreach ($this->original as $key => $embed) {
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...
92
            if (!$embed instanceof Embed) {
93
                continue;
94
            }
95
            $this->current[$key] = clone $embed;
96
        }
97
        $this->remove = [];
98
        return $this;
99
    }
100
}
101