HasMany   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 53
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A areDirty() 12 12 4
A rollback() 10 10 3
A calculateChangeSet() 11 11 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\Relationships;
4
5
use As3\Modlr\Models\Collections\Collection;
6
use As3\Modlr\Models\Properties;
7
8
/**
9
 * Represents the has-many relationships of a Model.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13 View Code Duplication
class HasMany extends Properties
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * Extended to also check for dirty states of has-many Collections.
17
     *
18
     * {@inheritDoc}
19
     */
20
    public function areDirty()
21
    {
22
        if (true === parent::areDirty()) {
23
            return true;
24
        }
25
        foreach ($this->getOriginalAll() as $collection) {
26
            if (true === $collection->isDirty()) {
27
                return true;
28
            }
29
        }
30
        return false;
31
    }
32
33
    /**
34
     * Extended to also rollback changes of has-many collections.
35
     *
36
     * {@inheritDoc}
37
     */
38
    public function rollback()
39
    {
40
        parent::rollback();
41
        foreach ($this->getOriginalAll() as $collection) {
42
            if ($collection instanceof Collection) {
43
                $collection->rollback();
44
            }
45
        }
46
        return $this;
47
    }
48
49
    /**
50
     * Overwritten to account for collection change sets.
51
     *
52
     * {@inheritDoc}
53
     */
54
    public function calculateChangeSet()
55
    {
56
        $set = [];
57
        foreach ($this->getOriginalAll() as $key => $collection) {
58
            if (false === $collection->isDirty()) {
59
                continue;
60
            }
61
            $set[$key] = $collection->calculateChangeSet();
62
        }
63
        return $set;
64
    }
65
}
66