Completed
Push — master ( 71cd39...d95db8 )
by Evan
15s
created

Taxonomy::postTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
cc 1
eloc 4
nc 1
nop 0
rs 9.4285
1
<?php
2
3
namespace Silk\Taxonomy;
4
5
use Silk\Post\PostType;
6
use Silk\Term\TermQueryBuilder;
7
use Illuminate\Support\Collection;
8
use Silk\Exception\WP_ErrorException;
9
10
/**
11
 * @property-read string   $id
12
 * @property-read stdClass $taxonomy
13
 *
14
 * @property-read bool     $_builtin
15
 * @property-read stdClass $cap
16
 * @property-read string   $description
17
 * @property-read bool     $hierarchical
18
 * @property-read string   $label
19
 * @property-read stdClass $labels
20
 * @property-read callable $meta_box_cb
21
 * @property-read string   $name
22
 * @property-read array    $object_type
23
 * @property-read bool     $public
24
 * @property-read bool     $publicly_queryable
25
 * @property-read string   $query_var
26
 * @property-read array    $rewrite
27
 * @property-read bool     $show_admin_column
28
 * @property-read bool     $show_in_menu
29
 * @property-read bool     $show_in_nav_menus
30
 * @property-read bool     $show_in_quick_edit
31
 * @property-read bool     $show_tagcloud
32
 * @property-read bool     $show_ui
33
 * @property-read callable $update_count_callback
34
 */
35
class Taxonomy
36
{
37
    /**
38
     * The Taxonomy identifier
39
     * @var string
40
     */
41
    protected $id;
42
43
    /**
44
     * The taxonomy object
45
     * @var object
46
     */
47
    protected $taxonomy;
48
49
    /**
50
     * Taxonomy Constructor.
51
     *
52
     * @param object $taxonomy  The taxonomy object
53
     */
54
    public function __construct($taxonomy)
55
    {
56
        if (empty($taxonomy->name) || ! static::exists($taxonomy->name)) {
57
            throw new Exception\NonExistentTaxonomyException;
58
        }
59
60
        $this->id = $taxonomy->name;
61
        $this->taxonomy = $taxonomy;
62
    }
63
64
    /**
65
     * Create a new instance using the taxonomy identifier.
66
     *
67
     * @param  string $identifier Taxonomy name/identifier
68
     *
69
     * @return static
70
     */
71
    public static function make($identifier)
72
    {
73
        return new static(get_taxonomy($identifier));
74
    }
75
76
    /**
77
     * Check if the given taxonomy exists.
78
     *
79
     * @param  string $identifier The taxonomy identifier
80
     *
81
     * @return bool
82
     */
83
    public static function exists($identifier)
84
    {
85
        return taxonomy_exists($identifier);
86
    }
87
88
    /**
89
     * Start a new query for terms of this taxonomy.
90
     *
91
     * @return TermQueryBuilder
92
     */
93
    public function terms()
94
    {
95
        return (new TermQueryBuilder)->forTaxonomy($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Silk\Taxonomy\Taxonomy>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
    }
97
98
    /**
99
     * Get all post types associated with this taxonomy.
100
     *
101
     * @return Collection
102
     */
103
    public function postTypes()
104
    {
105
        return Collection::make($this->object_type)
106
            ->map(function ($post_type) {
107
                return PostType::load($post_type);
108
            });
109
    }
110
111
    /**
112
     * Unregister the taxonomy.
113
     *
114
     * @return $this
115
     */
116 View Code Duplication
    public function unregister()
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...
117
    {
118
        if (! static::exists($this->taxonomy->name)) {
119
            throw new Exception\NonExistentTaxonomyException;
120
        }
121
122
        if (is_wp_error($error = unregister_taxonomy($this->taxonomy->name))) {
123
            throw new WP_ErrorException($error);
124
        }
125
126
        return $this;
127
    }
128
129
    /**
130
     * Magic Getter.
131
     *
132
     * @param  string $property Accessed property
133
     *
134
     * @return mixed
135
     */
136
    public function __get($property)
137
    {
138
        if (isset($this->$property)) {
139
            return $this->$property;
140
        }
141
142
        if (isset($this->taxonomy->$property)) {
143
            return $this->taxonomy->$property;
144
        }
145
    }
146
}
147