FileReader   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readJsonFile() 0 5 1
A readFile() 0 10 2
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