Passed
Push — main ( 7cb561...8aa2e0 )
by Breno
01:52
created

ComposerClassMap   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 22 3
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace FlexFqcnFinder\Finder;
5
6
use Composer\Autoload\ClassLoader;
7
use FlexFqcnFinder\FqcnFinderInterface;
8
use InvalidArgumentException;
9
10
/**
11
 * Including all available classes (vendor + your project); use:
12
 * $ composer dump-autoload -o
13
 */
14
class ComposerClassMap implements FqcnFinderInterface
15
{
16
    /**
17
     * @var string The path to your composer 'vendor/autoload.php'
18
     */
19
    protected $composerAutoloadPath;
20
21
    public function __construct(string $composerAutoloadPath)
22
    {
23
        $this->composerAutoloadPath = $composerAutoloadPath;
24
    }
25
26
    public function find(): array
27
    {
28
        if (!file_exists($this->composerAutoloadPath)) {
29
            throw new InvalidArgumentException(
30
                sprintf(
31
                    "Cannot get Composer class map. Invalid path: %s.",
32
                    $this->composerAutoloadPath
33
                )
34
            );
35
        }
36
37
        $classLoader = require($this->composerAutoloadPath);
38
        if (! $classLoader instanceof ClassLoader) {
39
            throw new InvalidArgumentException(
40
                sprintf(
41
                    "Cannot get Composer class map. Invalid composer autoload: %s.",
42
                    $this->composerAutoloadPath
43
                )
44
            );
45
        }
46
47
        return array_keys($classLoader->getClassMap());
48
    }
49
}
50