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

CodeLoader::isDeclared()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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