File::getFileContents()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Utilities;
6
7
use SimpleSAML\SAML2\Exception\RuntimeException;
8
9
use function file_get_contents;
10
use function is_readable;
11
use function sprintf;
12
13
/**
14
 * Various File Utilities
15
 */
16
class File
17
{
18
    /**
19
     * @param string $file full absolute path to the file
20
     */
21
    public static function getFileContents(string $file): string
22
    {
23
        if (!is_readable($file)) {
24
            throw new RuntimeException(sprintf(
25
                'File "%s" does not exist or is not readable',
26
                $file,
27
            ));
28
        }
29
30
        $contents = file_get_contents($file);
31
        if ($contents === false) {
32
            throw new RuntimeException(sprintf(
33
                'Could not read from existing and readable file "%s"',
34
                $file,
35
            ));
36
        }
37
38
        return $contents;
39
    }
40
}
41