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

Autoloader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A autoload() 0 11 4
A register() 0 3 1
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