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

PostTermConstrainer::getTerms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leonidas\Library\System\Post\Constraints;
4
5
use Leonidas\Contracts\Http\ConstrainerInterface;
6
use Leonidas\Traits\ExpectsPostTrait;
7
use Psr\Http\Message\ServerRequestInterface;
8
use WP_Post;
9
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...
10
11
class PostTermConstrainer implements ConstrainerInterface
12
{
13
    use ExpectsPostTrait;
0 ignored issues
show
Bug introduced by
The trait Leonidas\Traits\ExpectsPostTrait requires the property $ID which is not provided by Leonidas\Library\System\...nts\PostTermConstrainer.
Loading history...
14
15
    /**
16
     * @var string
17
     */
18
    protected $taxonomy;
19
20
    /**
21
     * @var WP_Term[]
22
     */
23
    protected $terms = [];
24
25
    /**
26
     * @var bool
27
     */
28
    protected $matchAll = false;
29
30
    /**
31
     *
32
     */
33
    public function __construct(string $taxonomy, int ...$terms)
34
    {
35
        $this->taxonomy = $taxonomy;
36
        $this->terms = $terms;
0 ignored issues
show
Documentation Bug introduced by
It seems like $terms of type array<integer,integer> is incompatible with the declared type WP_Term[] of property $terms.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
    }
38
39
    /**
40
     * Get the value of terms
41
     *
42
     * @return array
43
     */
44
    public function getTerms(): array
45
    {
46
        return $this->terms;
47
    }
48
49
    /**
50
     *
51
     */
52
    public function addTerm(int $term)
53
    {
54
        $this->terms[] = $term;
55
    }
56
57
    /**
58
     * Get the value of matchAll
59
     *
60
     * @return bool
61
     */
62
    public function shouldMatchAll(): bool
63
    {
64
        return $this->matchAll;
65
    }
66
67
    /**
68
     * Set the value of matchAll
69
     *
70
     * @param bool $matchAll
71
     *
72
     * @return self
73
     */
74
    public function setMatchAll(bool $matchAll)
75
    {
76
        $this->matchAll = $matchAll;
77
78
        return $this;
79
    }
80
81
    /**
82
     *
83
     */
84
    public function requestMeetsCriteria(ServerRequestInterface $request): bool
85
    {
86
        $post = $this->getPost($request);
87
88
        return $this->matchAll ?
89
            $this->matchesAllTerms($post) :
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type null; however, parameter $post of Leonidas\Library\System\...iner::matchesAllTerms() does only seem to accept WP_Post, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            $this->matchesAllTerms(/** @scrutinizer ignore-type */ $post) :
Loading history...
90
            $this->matchesSingleTerm($post);
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type null; however, parameter $post of Leonidas\Library\System\...er::matchesSingleTerm() does only seem to accept WP_Post, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
            $this->matchesSingleTerm(/** @scrutinizer ignore-type */ $post);
Loading history...
91
    }
92
93
    /**
94
     *
95
     */
96
    protected function matchesSingleTerm(WP_Post $post): bool
97
    {
98
        foreach ($this->terms as $term) {
99
            if (has_term($term, $this->taxonomy, $post)) {
0 ignored issues
show
Bug introduced by
The function has_term was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
            if (/** @scrutinizer ignore-call */ has_term($term, $this->taxonomy, $post)) {
Loading history...
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     *
109
     */
110
    protected function matchesAllTerms(WP_Post $post): bool
111
    {
112
        foreach ($this->terms as $term) {
113
            if (!has_term($term, $this->taxonomy, $post)) {
0 ignored issues
show
Bug introduced by
The function has_term was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            if (!/** @scrutinizer ignore-call */ has_term($term, $this->taxonomy, $post)) {
Loading history...
114
                return false;
115
            }
116
        }
117
118
        return true;
119
    }
120
}
121