RecursivePagination   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 105
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addToQueue() 0 14 5
A getNextPage() 0 23 5
A setExpression() 0 19 6
1
<?php
2
3
namespace Xparse\RecursivePagination;
4
5
6
use Xparse\ElementFinder\ElementFinderInterface;
7
use Xparse\Parser\Parser;
8
use Xparse\Parser\ParserInterface;
9
10
/**
11
 *
12
 */
13
class RecursivePagination
14
{
15
16
    /**
17
     * @var array
18
     */
19
    protected $queue = [];
20
21
    /**
22
     * @var Parser
23
     */
24
    protected $parser;
25
26
    /**
27
     * @var array
28
     */
29
    protected $elementSelector = [];
30
31
32
    /**
33
     * @param string|array $expression
34
     * @throws \Exception
35
     */
36 27
    public function __construct(ParserInterface $parser, $expression)
37
    {
38 27
        $this->parser = $parser;
0 ignored issues
show
Documentation Bug introduced by
$parser is of type object<Xparse\Parser\ParserInterface>, but the property $parser was declared to be of type object<Xparse\Parser\Parser>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
39 27
        $this->setExpression($expression);
40 18
    }
41
42
43
    /**
44
     * @param array|string $links
45
     * @return $this
46
     * @throws \InvalidArgumentException
47
     */
48 18
    public function addToQueue($links, bool $state = false): self
49
    {
50 18
        if (!is_string($links) and !is_array($links)) {
51 3
            throw new \InvalidArgumentException('Links should be an array or a string');
52
        }
53 15
        foreach ((array)$links as $url) {
54 15
            if (!is_string($url)) {
55 3
                throw new \InvalidArgumentException('url should be a string');
56
            }
57 15
            $this->queue[$url] = $state;
58
        }
59
60 12
        return $this;
61
    }
62
63
64
    /**
65
     * @throws \Exception
66
     * @throws \InvalidArgumentException
67
     */
68 12
    public function getNextPage(): ?ElementFinderInterface
69
    {
70
71 12
        $link = array_search(false, $this->queue, true);
72 12
        if ($link === false) {
73 12
            return null;
74
        }
75 12
        $this->queue[$link] = true;
76 12
        $page = $this->parser->get($link);
77 12
        if ($page === null) {
78
            return null;
79
        }
80 12
        foreach ($this->elementSelector as $expression => $state) {
81 12
            $queueLinks = $page->value($expression)->all();
82 12
            $countQueueLinks = count($queueLinks);
83 12
            if ($countQueueLinks > 0) {
84 12
                $queueLinks = array_combine($queueLinks, array_fill(0, $countQueueLinks, false));
85
                /** @noinspection SlowArrayOperationsInLoopInspection */
86 12
                $this->queue = array_merge($queueLinks, $this->queue);
87
            }
88
        }
89 12
        return $page;
90
    }
91
92
93
    /**
94
     * @param string|array $expression
95
     * @throws \InvalidArgumentException
96
     */
97 27
    private function setExpression($expression): void
98
    {
99
100 27
        if (!is_string($expression) and !is_array($expression)) {
101 3
            throw new \InvalidArgumentException('Invalid expression, should be array or string');
102
        }
103
104 24
        $expression = array_filter((array)$expression);
105
106 24
        if (count($expression) === 0) {
107 3
            throw new \InvalidArgumentException('Expression might be not empty');
108
        }
109 21
        foreach ($expression as $path) {
110 21
            if (!is_string($path)) {
111 3
                throw new \InvalidArgumentException('Invalid expression, should be a string');
112
            }
113 21
            $this->elementSelector[$path] = true;
114
        }
115 18
    }
116
117
}