AssignmentCollection   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 264
Duplicated Lines 10.61 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 28
loc 264
rs 10
c 0
b 0
f 0
wmc 30
lcom 2
cbo 4

15 Methods

Rating   Name   Duplication   Size   Complexity  
A findByFieldSlug() 0 11 3
A findAllByFieldType() 0 11 1
A relations() 0 16 3
A dates() 0 10 1
A indexed() 0 8 1
A required() 0 8 1
A translatable() 0 8 1
A notTranslatable() 0 8 1
A fieldSlugs() 0 11 2
B locked() 14 14 5
B notLocked() 14 14 5
A unlocked() 0 4 1
A column() 0 12 1
A withFields() 0 13 2
A withoutFields() 0 13 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 namespace Anomaly\Streams\Platform\Assignment;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
5
use Anomaly\Streams\Platform\Model\EloquentCollection;
6
7
/**
8
 * Class AssignmentCollection
9
 *
10
 * @link   http://pyrocms.com/
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class AssignmentCollection extends EloquentCollection
15
{
16
17
    /**
18
     * Find an assignment by it's field slug.
19
     *
20
     * @param  $slug
21
     * @return AssignmentInterface
22
     */
23
    public function findByFieldSlug($slug)
24
    {
25
        foreach ($this->items as $item) {
26
            /* @var AssignmentInterface $item */
27
            if ($item->getFieldSlug() == $slug) {
28
                return $item;
29
            }
30
        }
31
32
        return null;
33
    }
34
35
36
    /**
37
     * Find all fields using
38
     * the provided field type.
39
     *
40
     * @param $namespace
41
     * @return static
42
     */
43
    public function findAllByFieldType($namespace)
44
    {
45
        return new static(
46
            array_filter(
47
                $this->items,
48
                function (AssignmentInterface $assignment) use ($namespace) {
49
                    return $assignment->getFieldTypeValue() == $namespace;
50
                }
51
            )
52
        );
53
    }
54
55
    /**
56
     * Return assignments only included the provided fields.
57
     *
58
     * @param  array $fields
59
     * @return AssignmentCollection
60
     */
61
    public function withFields(array $fields)
62
    {
63
        return new static(
64
            array_filter(
65
                array_map(
66
                    function (AssignmentInterface $assignment) use ($fields) {
67
                        return in_array($assignment->getFieldSlug(), $fields) ? $assignment : null;
68
                    },
69
                    $this->items
70
                )
71
            )
72
        );
73
    }
74
75
    /**
76
     * Return assignments not included the provided fields.
77
     *
78
     * @param  array $fields
79
     * @return AssignmentCollection
80
     */
81
    public function withoutFields(array $fields)
82
    {
83
        return new static(
84
            array_filter(
85
                array_map(
86
                    function (AssignmentInterface $assignment) use ($fields) {
87
                        return !in_array($assignment->getFieldSlug(), $fields) ? $assignment : null;
88
                    },
89
                    $this->items
90
                )
91
            )
92
        );
93
    }
94
95
    /**
96
     * Return only assignments that have relation fields.
97
     *
98
     * @return AssignmentCollection
99
     */
100
    public function relations()
101
    {
102
        $relations = [];
103
104
        /* @var AssignmentInterface $item */
105
        /* @var FieldType $type */
106
        foreach ($this->items as $item) {
107
            $type = $item->getFieldType();
108
109
            if (method_exists($type, 'getRelation')) {
110
                $relations[] = $item;
111
            }
112
        }
113
114
        return self::make($relations);
115
    }
116
117
    /**
118
     * Return only assignments that have date fields.
119
     *
120
     * @return AssignmentCollection
121
     */
122
    public function dates()
123
    {
124
        return $this->filter(
125
            function (AssignmentInterface $assignment) {
126
                $type = $assignment->getFieldType();
127
128
                return in_array($type->getColumnType(), ['date', 'datetime']);
129
            }
130
        );
131
    }
132
133
    /**
134
     * Return only assignments that are unique.
135
     *
136
     * @return AssignmentCollection
137
     */
138
    public function indexed()
139
    {
140
        return $this->filter(
141
            function (AssignmentInterface $assignment) {
142
                return $assignment->isUnique();
143
            }
144
        );
145
    }
146
147
    /**
148
     * Return only assignments that are required.
149
     *
150
     * @return AssignmentCollection
151
     */
152
    public function required()
153
    {
154
        return $this->filter(
155
            function (AssignmentInterface $assignment) {
156
                return $assignment->isRequired();
157
            }
158
        );
159
    }
160
161
    /**
162
     * Return only assignments that are translatable.
163
     *
164
     * @return AssignmentCollection
165
     */
166
    public function translatable()
167
    {
168
        return $this->filter(
169
            function (AssignmentInterface $assignment) {
170
                return $assignment->isTranslatable();
171
            }
172
        );
173
    }
174
175
    /**
176
     * Return only assignments that are NOT translatable.
177
     *
178
     * @return AssignmentCollection
179
     */
180
    public function notTranslatable()
181
    {
182
        return $this->filter(
183
            function (AssignmentInterface $assignment) {
184
                return !$assignment->isTranslatable();
185
            }
186
        );
187
    }
188
189
    /**
190
     * Return an array of field slugs.
191
     *
192
     * @param  null $prefix
193
     * @return array
194
     */
195
    public function fieldSlugs($prefix = null)
196
    {
197
        $slugs = [];
198
199
        /* @var AssignmentInterface $item */
200
        foreach ($this->items as $item) {
201
            $slugs[] = $prefix . $item->getFieldSlug();
202
        }
203
204
        return $slugs;
205
    }
206
207
    /**
208
     * Return only assignments with locked fields.
209
     *
210
     * @return AssignmentCollection
211
     */
212 View Code Duplication
    public function locked()
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...
213
    {
214
        $items = [];
215
216
        foreach ($this->items as $item) {
217
            if ($item instanceof AssignmentInterface && $field = $item->getField()) {
218
                if ($field->isLocked()) {
219
                    $items[] = $item;
220
                }
221
            }
222
        }
223
224
        return new static($items);
225
    }
226
227
    /**
228
     * Return only assignments with fields
229
     * that are not locked.
230
     *
231
     * @return AssignmentCollection
232
     */
233 View Code Duplication
    public function notLocked()
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...
234
    {
235
        $items = [];
236
237
        foreach ($this->items as $item) {
238
            if ($item instanceof AssignmentInterface && $field = $item->getField()) {
239
                if (!$field->isLocked()) {
240
                    $items[] = $item;
241
                }
242
            }
243
        }
244
245
        return new static($items);
246
    }
247
248
    /**
249
     * An alias for notLocked();
250
     *
251
     * @return AssignmentCollection
252
     */
253
    public function unlocked()
254
    {
255
        return $this->notLocked();
256
    }
257
258
    /**
259
     * Return the assignment
260
     * with column type.
261
     *
262
     * @param $type
263
     * @return AssignmentCollection
264
     */
265
    public function column($type)
266
    {
267
        return $this->filter(
268
            function ($item) use ($type) {
269
270
                /* @var AssignmentInterface $item */
271
                $fieldType = $item->getFieldType();
272
273
                return $fieldType->getColumnType() == $type;
274
            }
275
        );
276
    }
277
}
278