Passed
Push — master ( 1a30b3...a4b88d )
by Fran
13:09
created

Autoloader::autoload()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PSFS;
6
7
8
require_once 'bootstrap.php';
9
10
defined("BASE_DIR") or define("BASE_DIR", dirname(__DIR__, preg_match('/vendor/', __DIR__) ? 4 : 1));
11
bootstrap::load();
12
13
class Autoloader
14
{
15 4
    public static function register(): void
16
    {
17 4
        spl_autoload_register([self::class, 'autoload']);
18
    }
19
20 7
    public static function autoload(string $class): void
21
    {
22 7
        if (str_starts_with($class, 'PSFS\\')) {
23 2
            $relativeClass = substr($class, strlen('PSFS\\'));
24
25 2
            $file = SOURCE_DIR . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
26
27 2
            if (file_exists($file)) {
28
                require_once $file;
29 2
            } else if (class_exists('PSFS\\base\\Logger')) {
30 2
                \PSFS\base\Logger::log("[Autoloader] Class $class not found at $file", LOG_WARNING);
31
            }
32
        }
33
    }
34
}
35
36
// Registro automático del autoloader (puedes comentarlo si lo haces desde bootstrap)
37
if (!defined('SOURCE_DIR')) {
38
    define('SOURCE_DIR', dirname(__DIR__) . '/src');
39
}
40
41
Autoloader::register();
42