Completed
Push — master ( dbd6b1...1c86dd )
by Jared
04:09
created

BelongsToMany   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 17.28 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A initQuery() 14 14 3
A getResults() 0 8 2
A getTablename() 0 4 1
B create() 0 25 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
/**
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 BelongsToMany extends Relation
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $tablename;
21
22
    /**
23
     * @param string $model      foreign model class
24
     * @param string $tablename  pivot table name
25
     * @param string $foreignKey identifying key on foreign model
26
     * @param string $localKey   identifying key on local model
27
     * @param Model  $relation
28
     */
29
    public function __construct($model, $tablename, $foreignKey, $localKey, Model $relation)
30
    {
31
        parent::__construct($model, $foreignKey, $localKey, $relation);
32
33
        $this->tablename = $tablename;
34
    }
35
36 View Code Duplication
    protected function initQuery()
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...
37
    {
38
        // set join
39
        // TODO
40
41
        $ids = $this->relation->ids();
42
        foreach ($ids as $property => $id) {
43
            if ($id === null) {
44
                $this->empty = true;
45
            }
46
47
            $this->query->where($this->foreignKey, $id);
48
        }
49
    }
50
51
    public function getResults()
52
    {
53
        if ($this->empty) {
54
            return;
55
        }
56
57
        return $this->query->execute();
58
    }
59
60
    /**
61
     * Gets the pivot tablename.
62
     *
63
     * @return string
64
     */
65
    public function getTablename()
66
    {
67
        return $this->tablename;
68
    }
69
70
    public function create(array $values = [])
71
    {
72
        $class = $this->model;
73
        $model = new $class($values);
74
        $model->save();
75
76
        // create pivot relation
77
        $pivot = new Pivot();
78
        $pivot->setTablename($this->tablename);
79
80
        $ids = $model->ids();
81
        foreach ($ids as $property => $id) {
82
            $pivot->{$this->localKey} = $id;
83
        }
84
85
        $ids = $this->relation->ids();
86
        foreach ($ids as $property => $id) {
87
            $pivot->{$this->foreignKey} = $id;
88
        }
89
90
        $pivot->save();
91
        $model->pivot = $pivot;
92
93
        return $model;
94
    }
95
}
96