Completed
Push — master ( e24a08...649c71 )
by Joshua
9s
created

HasOne   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 41.89 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
c 2
b 0
f 1
lcom 1
cbo 1
dl 31
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 10 3
A areDirty() 0 13 4
B calculateChangeSet() 19 19 5
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
        return false;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function calculateChangeSet()
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...
52
    {
53
        $set = [];
54
        foreach ($this->current as $key => $current) {
55
            if (false === $current->isDirty()) {
56
                continue;
57
            }
58
            $original = isset($this->original[$key]) ? $this->original[$key] : null;
59
            $set[$key]['old'] = $original;
60
            $set[$key]['new'] = $current;
61
        }
62
63
        foreach ($this->remove as $key) {
64
            $set[$key]['old'] = $this->original[$key];
65
            $set[$key]['new'] = null;
66
        }
67
        ksort($set);
68
        return $set;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function rollback()
75
    {
76
        $this->current = [];
77 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...
78
            if (!$embed instanceof Embed) {
79
                continue;
80
            }
81
            $this->current[$key] = clone $embed;
82
        }
83
        $this->remove = [];
84
        return $this;
85
    }
86
}
87