Completed
Push — master ( 9b95b7...985d72 )
by Jared
02:36
created

HasMany   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 4.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 0 Features 4
Metric Value
wmc 10
c 7
b 0
f 4
lcom 1
cbo 3
dl 4
loc 95
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initQuery() 0 11 2
A getResults() 0 8 2
A save() 0 7 1
A create() 0 9 1
A attach() 0 7 1
A detach() 0 7 1
A sync() 4 15 2

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
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Pulsar\Relation;
12
13
use Pulsar\Model;
14
15
class HasMany extends Relation
16
{
17
    protected function initQuery()
18
    {
19
        $localKey = $this->localKey;
20
        $value = $this->localModel->$localKey;
21
22
        if ($value === null) {
23
            $this->empty = true;
24
        }
25
26
        $this->query->where($this->foreignKey, $value);
27
    }
28
29
    public function getResults()
30
    {
31
        if ($this->empty) {
32
            return;
33
        }
34
35
        return $this->query->execute();
36
    }
37
38
    public function save(Model $model)
39
    {
40
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
41
        $model->save();
42
43
        return $model;
44
    }
45
46
    public function create(array $values = [])
47
    {
48
        $class = $this->foreignModel;
49
        $model = new $class();
50
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
51
        $model->create($values);
52
53
        return $model;
54
    }
55
56
    /**
57
     * Attaches a child model from this model.
58
     *
59
     * @param Model $model child model
60
     *
61
     * @return self
62
     */
63
    public function attach(Model $model)
64
    {
65
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
66
        $model->save();
67
68
        return $this;
69
    }
70
71
    /**
72
     * Detaches a child model from this model.
73
     *
74
     * @param Model $model child model
75
     *
76
     * @return self
77
     */
78
    public function detach(Model $model)
79
    {
80
        $model->{$this->foreignKey} = null;
81
        $model->save();
82
83
        return $this;
84
    }
85
86
    /**
87
     * Removes any relationships that are not included
88
     * in the list of IDs.
89
     * 
90
     * @param array $ids
91
     *
92
     * @return self
93
     */
94
    public function sync(array $ids)
95
    {
96
        $class = $this->foreignModel;
97
        $model = new $class();
98
        $query = $model::query();
99
100 View Code Duplication
        if (count($ids) > 0) {
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...
101
            $in = implode(',', $ids);
102
            $query->where("{$this->foreignKey} NOT IN ($in)");
103
        }
104
105
        $query->delete();
106
107
        return $this;
108
    }
109
}
110