Passed
Pull Request — master (#91)
by Matthias
04:10
created

GuessFromComposerAutoloader::normalizePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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
     */
14
    private $composerAutoloader;
15
16
    private $configVendorDir;
17
18 1
    public function __construct(string $composerJsonPath)
19
    {
20 1
        $composerJson = json_decode(file_get_contents($composerJsonPath), true);
21 1
        $this->configVendorDir = $this->normalizePath(dirname($composerJsonPath) . '/' . ($composerJson['config']['vendor-dir'] ?? 'vendor'));
22 1
        $this->composerAutoloader = include $this->configVendorDir . '/autoload.php';
23 1
    }
24
25 1
    public function __invoke(string $symbolName): \Generator
26
    {
27 1
        $fullFileName = $this->composerAutoloader->findFile(ltrim($symbolName, '\\/ '));
28 1
        if ($fullFileName) {
29 1
            $fileName = $this->normalizePath(ltrim(substr(realpath($fullFileName), strlen($this->configVendorDir)), '\\/'));
30 1
            $packageName = preg_replace('/^([^\/]+\/[^\/]+).*/', '$1', $fileName);
31 1
            yield $packageName;
32
        }
33 1
    }
34
35 1
    private function normalizePath(string $path): string
36
    {
37 1
        return str_replace('\\', '/', $path);
38
    }
39
40
}
41