AbstractCollection::first()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services;
6
7
use ArrayIterator;
8
use ArrayObject;
9
use Countable;
10
use IteratorAggregate;
11
use stdClass;
12
13
/**
14
 * @template TItem of object
15
 * @implements IteratorAggregate<int, TItem>
16
 */
17
abstract class AbstractCollection implements Countable, IteratorAggregate
18
{
19
    /**
20
     * @param stdClass $content
21
     * @return TItem
0 ignored issues
show
Bug introduced by
The type PhpCfdi\Finkok\Services\TItem 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...
22
     */
23
    abstract protected function createItemFromStdClass(stdClass $content): object;
24
25
    /** @var ArrayObject<int, TItem> */
26
    protected $collection;
27
28
    /** @param array<stdClass> $collection */
29 85
    public function __construct(array $collection)
30
    {
31 85
        $this->collection = new ArrayObject();
32 85
        foreach ($collection as $content) {
33 59
            $this->collection->append($this->createItemFromStdClass($content));
34
        }
35
    }
36
37
    /**
38
     * @param int $index
39
     * @return TItem
40
     */
41 16
    public function get(int $index): object
42
    {
43 16
        if (! isset($this->collection[$index])) {
44 2
            return $this->createItemFromStdClass((object) []);
45
        }
46 14
        return $this->collection[$index];
47
    }
48
49 37
    public function count(): int
50
    {
51 37
        return $this->collection->count();
52
    }
53
54
    /** @return TItem */
55 9
    public function first(): object
56
    {
57 9
        return $this->get(0);
58
    }
59
60
    /** @return ArrayIterator<int, TItem> */
61 43
    public function getIterator(): ArrayIterator
62
    {
63 43
        return $this->collection->getIterator();
64
    }
65
}
66