Completed
Push — master ( 81ce9d...1672d9 )
by Oleg
03:54
created

CodeLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadCode() 0 11 3
A isDeclared() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Util;
5
6
class CodeLoader
7
{
8
    private static $declared = [];
9
10 13
    public static function loadCode(string $code, string $fileName): void
11
    {
12 13
        if (!self::isDeclared($code)) {
13 13
            if (strpos($fileName, '/') !== false) {
14
                $tmpFile = $fileName;
15
            } else {
16 13
                $tmpFile = sys_get_temp_dir() . "/$fileName";
17
            }
18 13
            file_put_contents($tmpFile, $code);
19 13
            include $tmpFile;
20 13
            self::$declared[] = md5($code);
21
        }
22 13
    }
23
24 13
    private static function isDeclared(string $code): bool
25
    {
26 13
        return in_array(md5($code), self::$declared);
27
    }
28
}
29