ShortClassNames   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
A __construct() 0 12 1
A registerAutoloader() 0 4 1
A aliasClass() 0 14 2
1
<?php
2
3
namespace Sdbruder\TinkerTools;
4
5
class ShortClassNames
6
{
7
    /** @var \Illuminate\Support\Collection */
8
    public $classes;
9
10
    public static function register(string $classMapPath = null)
11
    {
12
        $classMapPath = $classMapPath ?? base_path('vendor/composer/autoload_classmap.php');
13
14
        (new static($classMapPath))->registerAutoloader();
15
    }
16
17
    public function __construct(string $classMapPath)
18
    {
19
        $classFiles = include $classMapPath;
20
21
        $this->classes = collect($classFiles)
22
            ->map(function (string $path, string $fqcn) {
23
                $name = last(explode('\\', $fqcn));
24
                return compact('fqcn', 'name');
25
            })
26
            ->filter()
27
            ->values();
28
    }
29
30
    public function registerAutoloader()
31
    {
32
        spl_autoload_register([$this, 'aliasClass']);
33
    }
34
35
    public function aliasClass($findClass)
36
    {
37
        $key = $this->classes->search(function ($class) use ($findClass) {
38
            return $class['name'] === $findClass;
39
        });
40
41
        if (! $key) {
42
            return;
43
        }
44
        
45
        $class = $this->classes[$key];
46
47
        class_alias($class['fqcn'], $class['name']);
48
    }
49
50
}
51