Passed
Branch feature/first-release (43e0cc)
by Andrea Marco
10:48
created

Filename::calculateSize()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\Sources;
4
5
use Traversable;
6
7
use function is_string;
8
9
/**
10
 * The filename source.
11
 *
12
 * @property-read string $source
13
 */
14
class Filename extends Source
15
{
16
    /**
17
     * Retrieve the JSON fragments
18
     *
19
     * @return Traversable<int, string>
20
     */
21 2
    public function getIterator(): Traversable
22
    {
23 2
        $handle = fopen($this->source, 'rb');
24
25
        try {
26 2
            yield from new JsonResource($handle, $this->config);
27
        } finally {
28 2
            $handle && fclose($handle);
29
        }
30
    }
31
32
    /**
33
     * Determine whether the JSON source can be handled
34
     *
35
     * @return bool
36
     */
37 128
    public function matches(): bool
38
    {
39 128
        return is_string($this->source) && is_file($this->source);
40
    }
41
42
    /**
43
     * Retrieve the calculated size of the JSON source
44
     *
45
     * @return int|null
46
     */
47
    protected function calculateSize(): ?int
48
    {
49
        return filesize($this->source) ?: null;
50
    }
51
}
52