Conditions | 5 |
Paths | 4 |
Total Lines | 25 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Tests | 7 |
CRAP Score | 5 |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
35 | public static function checkAndLoad(string $filename): string |
||
36 | 2 | { |
|
37 | $includePathFilename = \stream_resolve_include_path($filename); |
||
38 | // As a fallback, PHP looks in the directory of the file executing the |
||
39 | // stream_resolve_include_path function. |
||
40 | // |
||
41 | // We don't want to load the Test.php file here, so skip it if it found that. |
||
42 | 2 | // PHP prioritizes the include_path setting, so if the current directory is in there, |
|
43 | // it will first look in the current working directory. |
||
44 | 2 | $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename; |
|
45 | |||
46 | 2 | $isReadable = true; |
|
47 | 1 | try { |
|
48 | \fopen($includePathFilename, 'r') !== false; |
||
49 | } catch (\TypeError $e) { |
||
50 | 1 | $isReadable = false; |
|
51 | } |
||
52 | 1 | ||
53 | if (! $includePathFilename || ! $isReadable || $includePathFilename === $localFile) { |
||
54 | throw new \Exception(\sprintf('Cannot open file "%s".' . "\n", $filename)); |
||
55 | } |
||
56 | |||
57 | require_once $includePathFilename; |
||
58 | |||
59 | return $includePathFilename; |
||
60 | } |
||
62 |