Loader::loadOnce()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester\Utils;
6
7
use hanneskod\readmetester\Exception\InvalidPhpCodeException;
8
9
final class Loader
10
{
11
    /** @var array<string, bool> */
12
    private static array $loaded = [];
13
14
    public static function loadRaw(string $code): mixed
0 ignored issues
show
Bug introduced by
The type hanneskod\readmetester\Utils\mixed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
    {
16
        return eval($code);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
17
    }
18
19
    /** @throws InvalidPhpCodeException on php error in code */
20
    public static function load(string $code): mixed
21
    {
22
        try {
23
            return self::loadRaw($code);
24
        } catch (\Throwable $exception) {
25
            throw new InvalidPhpCodeException($exception->getMessage(), $code);
26
        }
27
    }
28
29
    public static function loadOnce(string $code): void
30
    {
31
        $key = md5($code);
32
33
        if (!isset(self::$loaded[$key])) {
34
            self::$loaded[$key] = true;
35
            self::load($code);
36
        }
37
    }
38
}
39