Composer::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Loader\PathResolver;
5
6
use Innmind\Compose\{
7
    Loader\PathResolver,
8
    Exception\ValueNotSupported
9
};
10
use Innmind\Url\{
11
    PathInterface,
12
    Path
13
};
14
use Innmind\Immutable\{
15
    Str,
16
    Sequence
17
};
18
use Composer\Autoload\ClassLoader;
19
20
final class Composer implements PathResolver
21
{
22
    private $vendorPath;
23
24
    public function __construct()
25
    {
26 17
        $composer = Sequence::of(...get_declared_classes())->filter(static function(string $class): bool {
27 17
            return (string) Str::of($class)->substring(0, 22) === 'ComposerAutoloaderInit';
28 17
        });
29
30
        //use the last as there may be 2 classes, the first being the global autoloader
31 17
        $refl = new \ReflectionClass((string) $composer->last());
32 17
        $vendorPath = dirname(dirname($refl->getFileName()));
33
34 17
        $this->vendorPath = Str::of($vendorPath)->append('/');
35 17
    }
36
37 13
    public function __invoke(PathInterface $from, PathInterface $to): PathInterface
38
    {
39 13
        $target = Str::of((string) $to);
40
41 13
        if (!$target->matches('|^@[a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+/.+|')) {
42 8
            throw new ValueNotSupported((string) $to);
43
        }
44
45 5
        return new Path(
46 5
            (string) $this->vendorPath->append((string) $target->substring(1))
47
        );
48
    }
49
}
50