Completed
Push — 1.1 ( e7f438...feb647 )
by Patrick
06:08 queued 01:36
created

JoinColumn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 17
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace LaravelDoctrine\Fluent\Relations;
4
5
use Doctrine\ORM\Mapping\NamingStrategy;
6
7
class JoinColumn
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $relation;
13
14
    /**
15
     * @var string
16
     */
17
    protected $joinColumn;
18
19
    /**
20
     * @var string
21
     */
22
    protected $referenceColumn;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $nullable = true;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $unique = false;
33
34
    /**
35
     * @var string|null
36
     */
37
    protected $onDelete = null;
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $columnDefinition = null;
43
44
    /**
45
     * @var NamingStrategy
46
     */
47
    protected $namingStrategy;
48
49
    /**
50
     * @param NamingStrategy $namingStrategy
51
     * @param string         $relation
52
     * @param string|null    $joinColumn
53
     * @param string|null    $referenceColumn
54
     * @param bool           $nullable
55
     * @param bool           $unique
56
     * @param string|null    $onDelete
57
     * @param string|null    $columnDefinition
58
     */
59 139
    public function __construct(
60
        NamingStrategy $namingStrategy,
61
        $relation,
62
        $joinColumn = null,
63
        $referenceColumn = null,
64
        $nullable = true,
65
        $unique = false,
66
        $onDelete = null,
67
        $columnDefinition = null
68
    ) {
69 139
        $this->namingStrategy   = $namingStrategy;
70 139
        $this->relation         = $relation;
71 139
        $this->joinColumn       = $joinColumn;
72 139
        $this->referenceColumn  = $referenceColumn;
73 139
        $this->nullable         = $nullable;
74 139
        $this->unique           = $unique;
75 139
        $this->onDelete         = $onDelete;
76 139
        $this->columnDefinition = $columnDefinition;
77 139
    }
78
79
    /**
80
     * @param string $foreignKey
81
     *
82
     * @return $this
83
     */
84 4
    public function foreignKey($foreignKey)
85
    {
86 4
        $this->joinColumn = $foreignKey;
87
88 4
        return $this;
89
    }
90
91
    /**
92
     * @param string $foreignKey
93
     *
94
     * @return $this
95
     */
96 3
    public function target($foreignKey)
97
    {
98 3
        return $this->foreignKey($foreignKey);
99
    }
100
101
    /**
102
     * @param string $localKey
103
     *
104
     * @return $this
105
     */
106 4
    public function localKey($localKey)
107
    {
108 4
        $this->referenceColumn = $localKey;
109
110 4
        return $this;
111
    }
112
113
    /**
114
     * @param string $localKey
115
     *
116
     * @return $this
117
     */
118 3
    public function source($localKey)
119
    {
120 3
        return $this->localKey($localKey);
121
    }
122
123
    /**
124
     * @return string
125
     */
126 61
    public function getJoinColumn()
127
    {
128 61
        return $this->joinColumn ?: $this->namingStrategy->joinColumnName($this->relation);
129
    }
130
131
    /**
132
     * @param string $joinColumn
133
     *
134
     * @return $this
135
     */
136 1
    public function setJoinColumn($joinColumn)
137
    {
138 1
        $this->joinColumn = $joinColumn;
139
140 1
        return $this;
141
    }
142
143
    /**
144
     * @return string
145
     */
146 61
    public function getReferenceColumn()
147
    {
148 61
        return $this->referenceColumn ?: $this->namingStrategy->referenceColumnName();
149
    }
150
151
    /**
152
     * @param string $referenceColumn
153
     *
154
     * @return $this
155
     */
156 1
    public function setReferenceColumn($referenceColumn)
157
    {
158 1
        $this->referenceColumn = $referenceColumn;
159
160 1
        return $this;
161
    }
162
163
    /**
164
     * @return $this
165
     */
166 56
    public function nullable()
167
    {
168 56
        $this->nullable = true;
169
170 56
        return $this;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176 59
    public function isNullable()
177
    {
178 59
        return $this->nullable;
179
    }
180
181
    /**
182
     * @return bool
183
     */
184 59
    public function isUnique()
185
    {
186 59
        return $this->unique;
187
    }
188
189
    /**
190
     * @return $this
191
     */
192 2
    public function unique()
193
    {
194 2
        $this->unique = true;
195
196 2
        return $this;
197
    }
198
199
    /**
200
     * @return string|null
201
     */
202 59
    public function getOnDelete()
203
    {
204 59
        return $this->onDelete;
205
    }
206
207
    /**
208
     * @param string|null $onDelete
209
     *
210
     * @return $this
211
     */
212 2
    public function onDelete($onDelete = null)
213
    {
214 2
        $this->onDelete = $onDelete;
215
216 2
        return $this;
217
    }
218
219
    /**
220
     * @return string|null
221
     */
222 56
    public function getColumnDefinition()
223
    {
224 56
        return $this->columnDefinition;
225
    }
226
}
227