Completed
Pull Request — master (#9)
by Evan
05:37
created

Model   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 264
Duplicated Lines 6.06 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 16
loc 264
rs 10
wmc 28
lcom 2
cbo 7

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A fromWpTerm() 0 4 1
A fromID() 8 8 2
A fromSlug() 8 8 2
A fromArray() 0 6 1
A create() 0 9 1
A save() 0 18 4
A delete() 0 9 2
A exists() 0 4 1
A isChildOf() 0 8 2
A parent() 0 4 1
A ancestors() 0 7 1
A taxonomy() 0 4 1
A newQuery() 0 4 1
A __get() 0 12 3
A __isset() 0 4 1
A __set() 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
2
3
namespace Silk\Term;
4
5
use stdClass;
6
use WP_Term;
7
use Silk\Taxonomy\Taxonomy;
8
use Silk\Query\QueryBuilder;
9
use Illuminate\Support\Collection;
10
use Silk\Exception\WP_ErrorException;
11
use Silk\Term\Exception\TermNotFoundException;
12
use Silk\Term\Exception\TaxonomyMismatchException;
13
14
/**
15
 * @property-read int $id
16
 * @property int $term_id
17
 * @property string $name
18
 * @property string $slug
19
 * @property string $term_group
20
 * @property int $term_taxonomy_id
21
 * @property string $taxonomy
22
 * @property string $description
23
 * @property int $parent
24
 * @property int $count
25
 */
26
abstract class Model
27
{
28
    use QueryBuilder;
29
30
    /**
31
     * The term's taxonomy
32
     * @var string
33
     */
34
    const TAXONOMY = '';
35
36
    /**
37
     * The term object
38
     * @var WP_Term
39
     */
40
    protected $term;
41
42
    /**
43
     * Model Constructor.
44
     *
45
     * @param mixed $term  WP_Term to fill data from
46
     */
47
    public function __construct(WP_Term $term = null)
48
    {
49
        if (! $term) {
50
            $term = new WP_Term(new stdClass);
51
            $term->taxonomy = static::TAXONOMY;
52
        } elseif ($term->taxonomy != static::TAXONOMY) {
53
            throw new TaxonomyMismatchException();
54
        }
55
56
        $this->term = $term;
57
    }
58
59
    /**
60
     * Create a new instance from a WP_Term object.
61
     *
62
     * @param  WP_Term $term [description]
63
     *
64
     * @return static
65
     */
66
    public static function fromWpTerm(WP_Term $term)
67
    {
68
        return new static($term);
69
    }
70
71
    /**
72
     * Create a new instance from a term ID.
73
     *
74
     * @param  int|string $id  Term ID
75
     *
76
     * @return static
77
     */
78 View Code Duplication
    public static function fromID($id)
1 ignored issue
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...
79
    {
80
        if (! $term = get_term_by('id', (int) $id, static::TAXONOMY)) {
81
            throw new TermNotFoundException("No term found with ID $id.");
82
        }
83
84
        return static::fromWpTerm($term);
85
    }
86
87
    /**
88
     * Create a new instance from a slug.
89
     *
90
     * @param  string $slug  Term slug
91
     *
92
     * @return static
93
     */
94 View Code Duplication
    public static function fromSlug($slug)
1 ignored issue
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...
95
    {
96
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
97
            throw new TermNotFoundException("No term found with slug '$slug'.");
98
        }
99
100
        return static::fromWpTerm($term);
101
    }
102
103
    /**
104
     * Create a new instance from an array of attributes.
105
     *
106
     * @param  array  $attributes [description]
107
     *
108
     * @return static
109
     */
110
    public static function fromArray(array $attributes)
111
    {
112
        return new static(
113
            new WP_Term((object) $attributes)
114
        );
115
    }
116
117
    /**
118
     * Create a new term, and get the instance for it.
119
     *
120
     * @param  array $attributes  Term attributes
121
     *
122
     * @return static
123
     */
124
    public static function create(array $attributes = [])
125
    {
126
        return static::fromArray(
127
            Collection::make($attributes)
128
                ->except(['term_id', 'term_taxonomy_id'])
129
                ->put('taxonomy', static::TAXONOMY)
130
                ->toArray()
131
        )->save();
132
    }
133
134
    /**
135
     * Save or update the term instance in the database.
136
     *
137
     * @return $this
138
     */
139
    public function save()
140
    {
141
        if ($this->exists()) {
142
            $ids = wp_update_term($this->id, static::TAXONOMY, $this->term->to_array());
143
        } else {
144
            $ids = wp_insert_term($this->name, static::TAXONOMY, $this->term->to_array());
145
        }
146
147
        if (is_wp_error($ids)) {
148
            throw new WP_ErrorException($ids);
149
        }
150
151
        foreach ($ids as $field => $id) {
152
            $this->term->$field = $id;
153
        }
154
155
        return $this;
156
    }
157
158
    /**
159
     * Delete the term from the database.
160
     *
161
     * @return $this
162
     */
163
    public function delete()
164
    {
165
        if ($result = wp_delete_term($this->id, static::TAXONOMY)) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
            $this->term->term_id = null;
167
            $this->term->term_taxonomy_id = 0;
168
        }
169
170
        return $this;
171
    }
172
173
    /**
174
     * Check if this term exists in the database.
175
     *
176
     * @return boolean
177
     */
178
    public function exists()
179
    {
180
        return (bool) term_exists((int) $this->id, static::TAXONOMY);
181
    }
182
183
    /**
184
     * Check if this term exists in the database as the child of the given parent.
185
     *
186
     * @param  int|string|object  $parent  integer Parent term ID
187
     *                                     string  Parent term slug or name
188
     *                                     object  The parent term object/model.
189
     *
190
     * @return boolean                     True if the this term and the parent
191
     *                                     exist in the database, and the instance
192
     *                                     is a child of the given parent;
193
     *                                     otherwise false
194
     */
195
    public function isChildOf($parent)
196
    {
197
        if (isset($parent->term_id)) {
198
            $parent = $parent->term_id;
199
        }
200
201
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
202
    }
203
204
    /**
205
     * Get the parent term instance.
206
     *
207
     * @return static
208
     */
209
    public function parent()
210
    {
211
        return static::fromID($this->term->parent);
212
    }
213
214
    /**
215
     * Get all ancestors of this term as a collection.
216
     *
217
     * @return Collection
218
     */
219
    public function ancestors()
220
    {
221
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
222
            ->map(function ($term_ID) {
223
                return static::fromID($term_ID);
224
            });
225
    }
226
227
    /**
228
     * Get the Taxonomy model.
229
     *
230
     * @return Taxonomy
231
     */
232
    public function taxonomy()
233
    {
234
        return Taxonomy::make($this->taxonomy);
235
    }
236
237
    /**
238
     * Start a new query for terms of this type.
239
     *
240
     * @return TermQueryBuilder
241
     */
242
    public function newQuery()
243
    {
244
        return (new TermQueryBuilder)->setModel($this);
245
    }
246
247
    /**
248
     * Magic Getter.
249
     *
250
     * @param  string $property Property name accessed
251
     *
252
     * @return mixed
253
     */
254
    public function __get($property)
255
    {
256
        if ('id' == strtolower($property)) {
257
            return $this->term->term_id;
258
        }
259
260
        if (isset($this->term->$property)) {
261
            return $this->term->$property;
262
        }
263
264
        return null;
265
    }
266
267
    /**
268
     * Magic set checker.
269
     *
270
     * @param  string  $property  Property name queried
271
     *
272
     * @return boolean
273
     */
274
    public function __isset($property)
275
    {
276
        return property_exists($this->term, $property);
277
    }
278
279
    /**
280
     * Magic Setter.
281
     *
282
     * @param string $property  Property name assigned
283
     * @param mixed  $value     Assigned property value
284
     */
285
    public function __set($property, $value)
286
    {
287
        $this->term->$property = $value;
288
    }
289
}
290