Completed
Push — master ( fb988b...6625be )
by Emmanuel
07:10 queued 05:31
created

FileLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 30
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A checkAndLoad() 0 19 4
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author    Emmanuel Dyan
8
 * @copyright 2018 Emmanuel Dyan
9
 *
10
 * @package edyan/neuralyzer
11
 *
12
 * @license GNU General Public License v2.0
13
 *
14
 * @link https://github.com/edyan/neuralyzer
15
 */
16
17
namespace Edyan\Neuralyzer\Utils;
18
19
/**
20
 * Class FileLoader
21
 * @package edyan/neuralyzer
22
 */
23
class FileLoader
24
{
25
    /**
26
     * Checks if a PHP source file is readable and loads it.
27
     *
28
     * @param string $filename
29
     *
30
     * @return string
31
     *
32
     * @throws \Exception
33
     */
34 2
    public static function checkAndLoad(string $filename): string
35
    {
36 2
        $includePathFilename = \stream_resolve_include_path($filename);
37
38
        // As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function.
39
        // We don't want to load the Test.php file here, so skip it if it found that.
40
        // PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the
41
        // current working directory.
42 2
        $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename;
43
44 2
        $isReadable = @\fopen($includePathFilename, 'r') !== false;
45
46 2
        if (!$includePathFilename || !$isReadable || $includePathFilename === $localFile) {
47 1
            throw new \Exception(\sprintf('Cannot open file "%s".' . "\n", $filename));
48
        }
49
50 1
        require_once $includePathFilename;
51
52 1
        return $includePathFilename;
53
    }
54
}
55