Passed
Pull Request — master (#12)
by Jeroen
03:07
created

FileLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A checkAndLoad() 0 19 4
1
<?php
2
3
namespace Edyan\Neuralyzer\Utils;
4
5
class FileLoader
6
{
7
    /**
8
     * Checks if a PHP sourcefile is readable and loads it.
9
     *
10
     * @param string $filename
11
     *
12
     * @return string
13
     *
14
     * @throws \Exception
15
     */
16
    public static function checkAndLoad($filename)
17
    {
18
        $includePathFilename = \stream_resolve_include_path($filename);
19
20
        // As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function.
21
        // We don't want to load the Test.php file here, so skip it if it found that.
22
        // PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the
23
        // current working directory.
24
        $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename;
25
26
        $isReadable = @\fopen($includePathFilename, 'r') !== false;
27
28
        if (!$includePathFilename || !$isReadable || $includePathFilename === $localFile) {
29
            throw new \Exception(\sprintf('Cannot open file "%s".' . "\n", $filename));
30
        }
31
32
        require_once $includePathFilename;
33
34
        return $includePathFilename;
35
    }
36
}
37