Failed Conditions
Pull Request — master (#91)
by Matthias
02:13
created

GuessFromComposerAutoloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ComposerRequireChecker\DependencyGuesser;
4
5
6
use Composer\Autoload\ClassLoader;
7
8
class GuessFromComposerAutoloader implements GuesserInterface
9
{
10
11
12
    /** @var ClassLoader */
13
    private $composerAutoloader;
14
15
    private $configVendorDir;
16
17 1
    public function __construct(string $composerJsonPath)
18
    {
19 1
        $composerJson = json_decode(file_get_contents($composerJsonPath), true);
20 1
        $this->configVendorDir = dirname($composerJsonPath) . '/' . ($composerJson['config']['vendor-dir'] ?? 'vendor');
21 1
        $this->composerAutoloader = include $this->configVendorDir . '/autoload.php';
22 1
    }
23
24 1
    public function __invoke(string $symbolName): \Generator
25
    {
26 1
        $fullFileName = $this->composerAutoloader->findFile(ltrim($symbolName, '\\/ '));
27 1
        if ($fullFileName) {
28 1
            $fileName = ltrim(substr(realpath($fullFileName), strlen($this->configVendorDir)), '\\/');
29 1
            $packageName = preg_replace('/^([^\/]+\/[^\/]+).*/', '$1', $fileName);
30 1
            yield $packageName;
31
        }
32 1
    }
33
34
}
35