JsonResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 37
ccs 10
cts 10
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 3 1
A getIterator() 0 5 3
A calculateSize() 0 6 2
1
<?php
2
3
namespace Cerbero\JsonParser\Sources;
4
5
use Traversable;
6
7
use function is_string;
8
use function is_resource;
9
10
/**
11
 * The resource source.
12
 *
13
 * @property-read resource $source
14
 */
15
class JsonResource extends Source
16
{
17
    /**
18
     * Retrieve the JSON fragments
19
     *
20
     * @return Traversable<int, string>
21
     */
22 26
    public function getIterator(): Traversable
23
    {
24 26
        while (!feof($this->source)) {
25 26
            if (is_string($chunk = fread($this->source, $this->config->bytes))) {
26 26
                yield $chunk;
27
            }
28
        }
29
    }
30
31
    /**
32
     * Determine whether the JSON source can be handled
33
     *
34
     * @return bool
35
     */
36 2
    public function matches(): bool
37
    {
38 2
        return is_resource($this->source);
39
    }
40
41
    /**
42
     * Retrieve the calculated size of the JSON source
43
     *
44
     * @return int|null
45
     */
46 1
    protected function calculateSize(): ?int
47
    {
48 1
        $stats = fstat($this->source);
49 1
        $size = $stats['size'] ?? null;
50
51 1
        return $size ?: null;
52
    }
53
}
54