Test Failed
Push — master ( fdb79d...2e4512 )
by Chris
19:35
created

TermCollection::without()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Leonidas\Library\System\Term;
4
5
use WP_Term;
0 ignored issues
show
Bug introduced by
The type WP_Term was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WP_Term_Query;
0 ignored issues
show
Bug introduced by
The type WP_Term_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class TermCollection
9
{
10
    /**
11
     * @var WP_Term[]
12
     */
13
    protected $terms;
14
15
    /**
16
     *
17
     */
18
    public function __construct(WP_Term ...$terms)
19
    {
20
        $this->terms = $terms;
21
    }
22
23
    /**
24
     * @return WP_Term[]
25
     */
26
    public function getTerms(): array
27
    {
28
        return $this->terms;
29
    }
30
31
    /**
32
     *
33
     */
34
    public function get(string $property)
35
    {
36
        return array_map(function (WP_Term $term) use ($property) {
37
            return $term->{$property};
38
        }, $this->terms);
39
    }
40
41
    /**
42
     *
43
     */
44
    public function getIds()
45
    {
46
        return $this->get('term_id');
47
    }
48
49
    /**
50
     *
51
     */
52
    public function getNames()
53
    {
54
        return $this->get('name');
55
    }
56
57
    /**
58
     *
59
     */
60
    public function getSlugs()
61
    {
62
        return $this->get('slug');
63
    }
64
65
    /**
66
     *
67
     */
68
    public function append(WP_Term $term)
69
    {
70
        $this->terms[] = $term;
71
    }
72
73
    /**
74
     *
75
     */
76
    public function isEmpty(): bool
77
    {
78
        return empty($this->terms);
79
    }
80
81
    /**
82
     *
83
     */
84
    protected function diffCallback()
85
    {
86
        return function (WP_Term $term1, WP_Term $term2) {
87
            return $term1->term_id - $term2->term_id;
88
        };
89
    }
90
91
    /**
92
     *
93
     */
94
    public function without(TermCollection $collection)
95
    {
96
        return array_udiff(
97
            $this->getTerms(),
98
            $collection->getTerms(),
99
            $this->diffCallback()
100
        );
101
    }
102
103
    /**
104
     *
105
     */
106
    public function notIn(TermCollection $collection)
107
    {
108
        return array_udiff(
109
            $collection->getTerms(),
110
            $this->getTerms(),
111
            $this->diffCallback()
112
        );
113
    }
114
115
    /**
116
     *
117
     */
118
    public function diff(TermCollection $collection): array
119
    {
120
        $primary = $this->getTerms();
121
        $secondary = $collection->getTerms();
122
        $cb = $this->diffCallback();
123
124
        /*
125
         * if both primary and secondary are empty this will return false
126
         * because the "array_diff" family of functions returns an empty array
127
         * if the first array provided is empty itself. if both arrays are
128
         * empty this will return an empty array as there is no difference.
129
         */
130
        return !empty($primary)
131
            ? array_udiff($primary, $secondary, $cb)
132
            : array_udiff($secondary, $primary, $cb);
133
    }
134
135
    /**
136
     *
137
     */
138
    public function isDiff(TermCollection $collection): bool
139
    {
140
        return (bool) $this->diff($collection);
141
    }
142
143
    /**
144
     *
145
     */
146
    public static function fromQuery(WP_Term_Query $query): TermCollection
147
    {
148
        return new static(...$query->get_terms());
149
    }
150
151
    /**
152
     *
153
     */
154
    public static function create(array $args): TermCollection
155
    {
156
        return static::fromQuery(new WP_Term_Query($args));
157
    }
158
159
    /**
160
     *
161
     */
162
    public static function fromIds(int ...$ids): TermCollection
163
    {
164
        return static::create([
165
            'include' => $ids,
166
            'hide_empty' => false,
167
        ]);
168
    }
169
}
170