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

CodeLoader::loadCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 0
cts 8
cp 0
crap 12
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