Completed
Push — master ( 53cbf4...c4dd08 )
by Matthias
04:10
created

LocateComposerPackageSourceFiles   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 69
ccs 41
cts 42
cp 0.9762
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 1
A locateFilesInPsr0Definitions() 0 4 1
A locateFilesInPsr4Definitions() 0 4 1
A locateFilesInClassmapDefinitions() 0 4 1
A locateFilesInFilesInFilesDefinitions() 0 10 4
A extractFilesFromDirectory() 0 4 1
A getFilePaths() 0 16 1
1
<?php
2
3
namespace ComposerRequireChecker\FileLocator;
4
5
use Generator;
6
7
final class LocateComposerPackageSourceFiles
8
{
9 6
    public function __invoke(string $composerJsonPath) : Generator
10
    {
11 6
        $packageDir   = dirname($composerJsonPath);
12 6
        $composerData = json_decode(file_get_contents($composerJsonPath), true);
13
14 6
        yield from $this->locateFilesInClassmapDefinitions(
15 6
            $this->getFilePaths($composerData['autoload']['classmap'] ?? [], $packageDir)
16
        );
17 6
        yield from $this->locateFilesInFilesInFilesDefinitions(
18 6
            $this->getFilePaths($composerData['autoload']['files'] ?? [], $packageDir)
19
        );
20 6
        yield from $this->locateFilesInPsr0Definitions(
21 6
            $this->getFilePaths($composerData['autoload']['psr-0'] ?? [], $packageDir)
22
        );
23 6
        yield from $this->locateFilesInPsr4Definitions(
24 6
            $this->getFilePaths($composerData['autoload']['psr-4'] ?? [], $packageDir)
25
        );
26 6
    }
27
28 6
    private function getFilePaths(array $sourceDirs, string $packageDir) : array
29
    {
30 6
        $flattened = array_reduce(
31
            $sourceDirs,
32
            function (array $sourceDirs, $sourceDir) {
33 6
                return array_merge($sourceDirs, (array)$sourceDir);
34 6
            },
35 6
            []
36
        );
37 6
        return array_values(array_map(
38 6
            function (string $sourceDir) use ($packageDir) {
39 6
                return $packageDir . '/' . ltrim($sourceDir, '/');
40 6
            },
41 6
            $flattened
42
        ));
43
    }
44
45 6
    private function locateFilesInPsr0Definitions(array $locations) : Generator
46
    {
47 6
        yield from $this->locateFilesInFilesInFilesDefinitions($locations);
48 6
    }
49
50 6
    private function locateFilesInPsr4Definitions(array $locations) : Generator
51
    {
52 6
        yield from $this->locateFilesInFilesInFilesDefinitions($locations);
53 6
    }
54
55 6
    private function locateFilesInClassmapDefinitions(array $locations) : Generator
56
    {
57 6
        yield from $this->locateFilesInFilesInFilesDefinitions($locations);
58 6
    }
59
60 6
    private function locateFilesInFilesInFilesDefinitions(array $locations) : Generator
61
    {
62 6
        foreach ($locations as $location) {
63 6
            if (is_file($location)) {
64 2
                yield $location;
65 4
            } elseif (is_dir($location)) {
66 4
                yield from $this->extractFilesFromDirectory($location);
67
            }
68
        }
69 6
    }
70
71 4
    private function extractFilesFromDirectory(string $directory) : Generator
72
    {
73 4
        yield from (new LocateAllFilesByExtension())->__invoke(new \ArrayIterator([$directory]), '.php');
74 4
    }
75
}
76