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

CodeLoader::isDeclared()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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