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

ComposerClassMap::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 22
rs 9.8666
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