Completed
Push — master ( f81cf6...7fbbb0 )
by Oleg
03:40
created

CodeLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 10
c 0
b 0
f 0
ccs 0
cts 11
cp 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
final class CodeLoader
7
{
8
    private static $declared = [];
9
10
    public static function loadCode(string $code, string $fileName): void
11
    {
12
        if (!self::isDeclared($code)) {
13
            if (strpos($fileName, '/') !== false) {
14
                $tmpFile = $fileName;
15
            } else {
16
                $tmpFile = sys_get_temp_dir() . "/$fileName";
17
            }
18
            file_put_contents($tmpFile, $code);
19
            include $tmpFile;
20
            self::$declared[] = md5($code);
21
        }
22
    }
23
24
    private static function isDeclared(string $code): bool
25
    {
26
        return in_array(md5($code), self::$declared);
27
    }
28
}
29