ProviderIterator::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eclipxe\XlsxExporter\Providers;
6
7
use Countable;
8
use Eclipxe\XlsxExporter\ProviderInterface;
9
use Eclipxe\XlsxExporter\Utils\ProviderGetValue;
10
use Iterator;
11
12
/**
13
 * ProviderIterator is a facade to be able to use any Iterator as a Provider
14
 *
15
 * The iterator current method must return an array, an ArrayAccess or an object,
16
 * the code will check if the key is set in the element, if it is not set then
17
 * will return null
18
 *
19
 * As the Iterator does not know the length of the elements by itself then it is
20
 * desirable to provide the total count from the constructor.
21
 * If a negative number is provided then the function will traverse the hole iterator
22
 * to count the total elements.
23
 */
24
class ProviderIterator implements ProviderInterface
25
{
26
    private Iterator $iterator;
27
28
    private int $count;
29
30
    /**
31
     * ProviderIterator constructor.
32
     * @param Iterator $iterator
33
     * @param int $count The total count of records, -1 to obtain
34
     */
35 5
    public function __construct(Iterator $iterator, int $count = -1)
36
    {
37 5
        $this->iterator = $iterator;
38 5
        if (! is_int($count) || $count < 0) {
39 2
            if ($this->iterator instanceof Countable) {
40 1
                $count = $this->iterator->count();
0 ignored issues
show
Bug introduced by
The method count() does not exist on Iterator. It seems like you code against a sub-type of Iterator such as PHPUnit\TextUI\XmlConfig...\FileCollectionIterator or SplDoublyLinkedList or HttpMessage or HttpRequestPool or PHPUnit\TextUI\XmlConfig...ctoryCollectionIterator or RdKafka\Metadata\Collection or PHPUnit\TextUI\XmlConfig...tFileCollectionIterator or Yaf_Config_Simple or PHPUnit\TextUI\XmlConfig...SuiteCollectionIterator or SplFixedArray or SplObjectStorage or Yaf\Session or SQLiteResult or Cassandra\UserTypeValue or Imagick or PHPUnit\TextUI\XmlConfig...nsionCollectionIterator or PHPUnit\TextUI\XmlConfig...ctoryCollectionIterator or Cassandra\Collection or SimpleXMLElement or TheSeer\Tokenizer\TokenCollection or PHPUnit\TextUI\XmlConfig...GroupCollectionIterator or http\Message or Yaf_Session or EngineWorks\ProgressStatus\ObserversSet or SplPriorityQueue or Cassandra\Rows or PHPUnit\TextUI\XmlConfig...stantCollectionIterator or Cassandra\Map or Yaf\Config\Simple or Yaf\Config\Ini or MongoCursor or Yaf_Config_Ini or PHPUnit\TextUI\XmlConfig...ctoryCollectionIterator or SplHeap or PHPUnit\TextUI\XmlConfig...ttingCollectionIterator or Cassandra\Set or MongoGridFSCursor or Cassandra\Tuple or PHPUnit\TextUI\XmlConfig...iableCollectionIterator or CachingIterator or Phar or ArrayIterator or GlobIterator or Phar or Phar or RecursiveCachingIterator or SimpleXMLElement or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

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

40
                /** @scrutinizer ignore-call */ 
41
                $count = $this->iterator->count();
Loading history...
41
            } else {
42 1
                $count = iterator_count($this->iterator);
43 1
                $this->iterator->rewind();
44
            }
45
        }
46 5
        $this->count = $count;
47
    }
48
49 5
    public function get(string $key)
50
    {
51 5
        return ProviderGetValue::get($this->iterator->current(), $key);
52
    }
53
54 5
    public function next(): void
55
    {
56 5
        $this->iterator->next();
57
    }
58
59 5
    public function valid(): bool
60
    {
61 5
        return $this->iterator->valid();
62
    }
63
64 5
    public function count(): int
65
    {
66 5
        return $this->count;
67
    }
68
}
69