Completed
Pull Request — master (#20)
by Evan
02:36
created

PostType::object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
rs 10
1
<?php
2
3
namespace Silk\PostType;
4
5
use stdClass;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
introduced by
Use classes must be in alphabetical order.
Loading history...
6
use Silk\Type\Type;
7
use Illuminate\Support\Collection;
8
use Silk\Exception\WP_ErrorException;
9
use Silk\PostType\Exception\NonExistentPostTypeException;
10
11
class PostType extends Type
12
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
13
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
14
     * PostType Constructor
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
15
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
16
     * @param stdClass $object  The WordPress post type object
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
stdClass => \stdClass
Loading history...
17
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
18
     * @throws \InvalidArgumentException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
19
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
20
    public function __construct($object)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
21
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
22
        if (! is_object($object) || ! in_array(get_class($object), ['stdClass', 'WP_Post_Type'])) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
23
            throw new \InvalidArgumentException(static::class . ' can only be constructed with a Post Type object.');
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Use statement InvalidArgumentException for InvalidArgumentException should be in use block.
Loading history...
24
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
25
26
        $this->object = $object;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
27
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
28
29
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
30
     * Create a new instance using the post type slug.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
31
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
32
     * Loads an existing type, or returns a new builder for registering a new type.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
33
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
34
     * @param  string $slug    The post type slug
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
35
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
36
     * @return static|Builder  If the post type has been registered, a new static instance is returned.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
There should be no space around pipes in doc blocks.
Loading history...
introduced by
Invalid class name "Builder"
Loading history...
37
     *                         Otherwise a new Builder is created for building a new post type to register.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
38
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
39
    public static function make($slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
40
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
41
        if (static::exists($slug)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
42
            return static::load($slug);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
43
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
44
45
        return new Builder($slug);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
46
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
47
48
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
49
     * Create a new instance from an existing type.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
50
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
51
     * @param  string $slug  The post type slug
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
52
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
53
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
54
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
55
    public static function load($slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
56
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
57
        if (! $object = get_post_type_object($slug)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Conditional inline assignment not allowed
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
58
            throw new NonExistentPostTypeException("No post type exists with name '$slug'.");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
59
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
60
61
        return new static($object);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
62
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
63
64
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
65
     * Checks if a post type with this slug has been registered.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
66
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
67
     * @param string $slug  The post type slug
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
68
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
69
     * @return bool
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
70
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
71
    public static function exists($slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
72
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
73
        return post_type_exists($slug);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
74
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
75
76
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
77
     * Check for feature support.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
78
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
79
     * @param string|array $features  string - First feature of possible many,
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
80
     *                                array - Many features to check support for.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
81
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
82
     * @return mixed
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
83
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
84
    public function supports($features)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
85
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
86
        if (! is_array($features)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
87
            $features = func_get_args();
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
88
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
89
90
        return ! Collection::make($features)
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
91
            ->contains(function ($key, $feature) {
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
92
                return ! post_type_supports($this->slug, $feature);
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
93
            });
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
94
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
95
96
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
97
     * Register support of certain features for an existing post type.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
98
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
99
     * @param mixed $features string - single feature to add
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
100
     *                        array - multiple features to add
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
101
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
102
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
103
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
104
    public function addSupportFor($features)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
105
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
106
        add_post_type_support($this->slug, is_array($features) ? $features : func_get_args());
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
107
108
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
109
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
110
111
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
112
     * Deregister support of certain features for an existing post type.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
113
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
114
     * @param mixed $features string - single feature to remove
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
115
     *                        array - multiple features to remove
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
116
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
117
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
118
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
119
    public function removeSupportFor($features)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
120
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
121
        Collection::make(is_array($features) ? $features : func_get_args())
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
122
            ->each(function ($features) {
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
123
                remove_post_type_support($this->slug, $features);
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
124
            });
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
125
126
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
127
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
128
129
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
130
     * Unregister the post type
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
131
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
132
     * @throws NonExistentPostTypeException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
NonExistentPostTypeException => \Silk\PostType\Exception\NonExistentPostTypeException
Loading history...
133
     * @throws WP_ErrorException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_ErrorException => \Silk\Exception\WP_ErrorException
Loading history...
134
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
135
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
136
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
137
    public function unregister()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
138
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
139
        if (! static::exists($this->slug)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
140
            throw new NonExistentPostTypeException("No post type exists with name '{$this->slug}'.");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
141
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
142
143
        if (is_wp_error($error = unregister_post_type($this->slug))) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
144
            throw new WP_ErrorException($error);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
145
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
146
147
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
148
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
149
}
0 ignored issues
show
introduced by
Closing brace of a class must have a new line between itself and the last content.
Loading history...
150