FileReader::readJsonFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Static Analysis Results Baseliner (sarb).
5
 *
6
 * (c) Dave Liddament
7
 *
8
 * For the full copyright and licence information please view the LICENSE file distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File;
14
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\FileName;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\JsonUtils;
17
18
final class FileReader
19
{
20
    /**
21
     * Returns string containing contents of the file.
22
     *
23
     * @throws FileAccessException
24
     */
25
    public function readFile(FileName $fileName): string
26
    {
27
        $fileNameAsString = $fileName->getFileName();
28
29
        $fileContents = @file_get_contents($fileNameAsString);
30
        if (false === $fileContents) {
31
            throw FileAccessException::readFileException($fileName);
32
        }
33
34
        return $fileContents;
35
    }
36
37
    /**
38
     * Returns array representing the contents of the file. Assumes the file must be JSON.
39
     *
40
     * @throws FileAccessException
41
     * @throws InvalidContentTypeException
42
     *
43
     * @return array<mixed>
44
     */
45
    public function readJsonFile(FileName $fileName): array
46
    {
47
        $fileContents = $this->readFile($fileName);
48
49
        return JsonUtils::toArray($fileContents);
50
    }
51
}
52