Test Failed
Push — master ( bc18b2...889641 )
by Chris
15:33
created

PostQueryIterator::maybeStartLoop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Leonidas\Library\System\Schema\Post;
4
5
use Countable;
6
use Iterator;
7
use Leonidas\Contracts\System\Schema\Post\PostConversionArchiveInterface;
8
use Leonidas\Contracts\System\Schema\Post\PostConverterInterface;
9
use Leonidas\Library\System\Schema\Post\Abstracts\ManagesPostConversionsTrait;
10
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...
11
12
class PostQueryIterator implements Iterator, Countable
13
{
14
    use ManagesPostConversionsTrait;
15
16
    protected WP_Query $query;
17
18
    public function __construct(
19
        WP_Query $query,
20
        PostConverterInterface $converter,
21
        ?PostConversionArchiveInterface $archive = null
22
    ) {
23
        $this->query = $query;
24
        $this->converter = $converter;
25
        $this->conversionArchive = $archive;
26
    }
27
28
    public function current(): object
29
    {
30
        $this->maybeStartLoop();
31
32
        return $this->getConvertedPost($GLOBALS['post']);
33
    }
34
35
    public function key(): int
36
    {
37
        return $this->query->current_post;
38
    }
39
40
    public function next(): void
41
    {
42
        $this->maybeStartLoop();
43
44
        if (!$this->loopEnded()) {
45
            $this->nextPost();
46
        }
47
    }
48
49
    public function rewind(): void
50
    {
51
        $this->query->rewind_posts();
52
    }
53
54
    public function valid(): bool
55
    {
56
        if (!$valid = $this->query->have_posts()) {
57
            $this->resetData();
58
        }
59
60
        return $valid;
61
    }
62
63
    public function count(): int
64
    {
65
        return count($this->query->posts);
66
    }
67
68
    protected function maybeStartLoop(): void
69
    {
70
        if (!$this->loopStarted()) {
71
            $this->nextPost();
72
        }
73
    }
74
75
    protected function loopStarted(): bool
76
    {
77
        return $this->key() > -1;
78
    }
79
80
    protected function loopEnded(): bool
81
    {
82
        return $this->key() === $this->count() - 1;
83
    }
84
85
    protected function nextPost(): void
86
    {
87
        $this->query->the_post();
88
    }
89
90
    protected function resetData(): void
91
    {
92
        wp_reset_postdata();
0 ignored issues
show
Bug introduced by
The function wp_reset_postdata 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

92
        /** @scrutinizer ignore-call */ 
93
        wp_reset_postdata();
Loading history...
93
    }
94
}
95