PostCollection::diffCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\System\Schema\Post;
4
5
use WP_Post;
6
use WP_Query;
0 ignored issues
show
Bug introduced by
The type WP_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 PostCollection
9
{
10
    /**
11
     * @var WP_Post[]
12
     */
13
    protected array $posts = [];
14
15
    /**
16
     * @param WP_Post $posts
17
     */
18
    public function __construct(WP_Post ...$posts)
19
    {
20
        $this->posts = $posts;
21
    }
22
23
    /**
24
     * Get the value of posts
25
     *
26
     * @return WP_Post[]
27
     */
28
    public function getPosts(): array
29
    {
30
        return $this->posts;
31
    }
32
33
    public function get(string $property)
34
    {
35
        return array_map(function (WP_Post $post) use ($property) {
36
            return $post->{$property};
37
        }, $this->posts);
38
    }
39
40
    public function getIds()
41
    {
42
        return $this->get('ID');
43
    }
44
45
    public function getNames()
46
    {
47
        return $this->get('post_name');
48
    }
49
50
    public function getTitles()
51
    {
52
        return $this->get('post_title');
53
    }
54
55
    public function getPostTypes()
56
    {
57
        return $this->get('post_type');
58
    }
59
60
    public function isEmpty(): bool
61
    {
62
        return empty($this->posts);
63
    }
64
65
    public function append(WP_Post $post)
66
    {
67
        $this->posts[] = $post;
68
    }
69
70
    protected function diffCallback()
71
    {
72
        return function (WP_Post $post1, WP_Post $post2) {
73
            return $post1->ID - $post2->ID;
74
        };
75
    }
76
77
    public function without(PostCollection $collection)
78
    {
79
        return array_udiff(
80
            $this->getPosts(),
81
            $collection->getPosts(),
82
            $this->diffCallback()
83
        );
84
    }
85
86
    public function notIn(PostCollection $collection)
87
    {
88
        return array_udiff(
89
            $collection->getPosts(),
90
            $this->getPosts(),
91
            $this->diffCallback()
92
        );
93
    }
94
95
    public function diff(PostCollection $collection): array
96
    {
97
        $primary = $this->getPosts();
98
        $secondary = $collection->getPosts();
99
        $cb = $this->diffCallback();
100
101
        /*
102
         * if both primary and secondary are empty this will return false
103
         * because the "array_diff" family of functions returns an empty array
104
         * if the first array provided is empty itself. if both arrays are
105
         * empty this will return an empty array as there is no difference.
106
         */
107
        return !empty($primary)
108
            ? array_udiff($primary, $secondary, $cb)
109
            : array_udiff($secondary, $primary, $cb);
110
    }
111
112
    public function isDiff(PostCollection $collection): bool
113
    {
114
        return (bool) $this->diff($collection);
115
    }
116
117
    public static function fromQuery(WP_Query $query): PostCollection
118
    {
119
        $query->set('fields', 'all');
120
121
        return new self(...$query->get_posts());
122
    }
123
124
    public static function create(array $args): PostCollection
125
    {
126
        return static::fromQuery(new WP_Query($args));
127
    }
128
129
    public static function fromIds(int ...$ids): PostCollection
130
    {
131
        return static::create([
132
            'post_type' => 'any',
133
            'post__in' => $ids,
134
            'posts_per_page' => -1,
135
        ]);
136
    }
137
}
138