JsonFileParser::getFileContentsAsArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace UltraLite\ConfigReader\FileParser;
3
4
use UltraLite\ConfigReader\ConfigReaderException\ConfigFileUnparsable;
5
use UltraLite\ConfigReader\FileParser;
6
7
class JsonFileParser implements FileParser
8
{
9
    public function supportsFileExtension(string $fileExtension): bool
10
    {
11
        return $fileExtension === 'json';
12
    }
13
14
    /**
15
     * @throws ConfigFileUnparsable
16
     */
17
    public function getFileContentsAsArray(string $path): array
18
    {
19
        $fileContents = file_get_contents($path);
20
        $fileContentsAsArray = json_decode($fileContents, true);
21
        if (!is_array($fileContentsAsArray)) {
22
            throw ConfigFileUnparsable::constructFromPath($path);
23
        }
24
        return $fileContentsAsArray;
25
    }
26
}
27