Create   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A newInstance() 0 11 2
A createPrecedents() 0 9 3
A createPrecedent() 0 5 1
A independentAttributes() 10 10 3
A createDependents() 0 6 2
A dependentRelationships() 10 10 3
A isDependentRelationship() 0 4 1

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 namespace AdamWathan\Faktory\Strategy;
2
3
use AdamWathan\Faktory\Relationship\BelongsTo;
4
use AdamWathan\Faktory\Relationship\Relationship;
5
use AdamWathan\Faktory\Relationship\DependentRelationship;
6
7
class Create extends Strategy
8
{
9
    public function newInstance()
10
    {
11
        $this->createPrecedents();
12
        $instance = $this->newModel();
13
        foreach ($this->independentAttributes() as $attribute => $value) {
14
            $instance->{$attribute} = $this->getAttributeValue($value);
15
        }
16
        $instance->save();
17
        $this->createDependents($instance);
18
        return $instance;
19
    }
20
21
    protected function createPrecedents()
22
    {
23
        foreach ($this->attributes as $attribute => $value) {
24
            if ($value instanceof BelongsTo) {
25
                $this->createPrecedent($value);
26
                $this->unsetAttribute($attribute);
27
            }
28
        }
29
    }
30
31
    protected function createPrecedent($relationship)
32
    {
33
        $precedent = $relationship->create();
34
        $this->setAttribute($relationship->getForeignKey(), $precedent->getKey());
35
    }
36
37 View Code Duplication
    protected function independentAttributes()
38
    {
39
        $result = [];
40
        foreach ($this->attributes as $attribute => $value) {
41
            if (! $value instanceof Relationship) {
42
                $result[$attribute] = $value;
43
            }
44
        }
45
        return $result;
46
    }
47
48
    protected function createDependents($instance)
49
    {
50
        foreach ($this->dependentRelationships() as $relationship) {
51
            $relationship->create($instance);
52
        }
53
    }
54
55 View Code Duplication
    protected function dependentRelationships()
56
    {
57
        $result = [];
58
        foreach ($this->attributes as $attribute => $value) {
59
            if ($this->isDependentRelationship($value)) {
60
                $result[] = $value;
61
            }
62
        }
63
        return $result;
64
    }
65
66
    protected function isDependentRelationship($value)
67
    {
68
        return $value instanceof DependentRelationship;
69
    }
70
}
71