Completed
Push — master ( 961aa4...4462c2 )
by Matthias
03:43
created

extractFilesFromDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ComposerRequireChecker\FileLocator;
4
5
use Generator;
6
7
final class LocateComposerPackageSourceFiles
8
{
9
    public function __invoke(string $composerJsonPath) : Generator
10
    {
11
        $packageDir   = dirname($composerJsonPath);
12
        $composerData = json_decode(file_get_contents($composerJsonPath), true);
13
14
        yield from $this->locateFilesInClassmapDefinitions(
15
            $this->getFilePaths($composerData['autoload']['classmap'] ?? [], $packageDir)
16
        );
17
        yield from $this->locateFilesInFilesInFilesDefinitions(
18
            $this->getFilePaths($composerData['autoload']['files'] ?? [], $packageDir)
19
        );
20
        yield from $this->locateFilesInPsr0Definitions(
21
            $this->getFilePaths($composerData['autoload']['psr-0'] ?? [], $packageDir)
22
        );
23
        yield from $this->locateFilesInPsr4Definitions(
24
            $this->getFilePaths($composerData['autoload']['psr-4'] ?? [], $packageDir)
25
        );
26
    }
27
28
    private function getFilePaths(array $sourceDirs, string $packageDir) : array
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
29
    {
30
        return array_values(array_map(
31
            function (string $sourceDir) use ($packageDir) {
32
                return $packageDir . '/' . ltrim($sourceDir, '/');
33
            },
34
            $sourceDirs
35
        ));
36
    }
37
38
    private function locateFilesInPsr0Definitions(array $locations) : Generator
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
39
    {
40
        yield from $this->locateFilesInFilesInFilesDefinitions($locations);
41
    }
42
43
    private function locateFilesInPsr4Definitions(array $locations) : Generator
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
44
    {
45
        yield from $this->locateFilesInFilesInFilesDefinitions($locations);
46
    }
47
48
    private function locateFilesInClassmapDefinitions(array $locations) : Generator
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
49
    {
50
        yield from $this->locateFilesInFilesInFilesDefinitions($locations);
51
    }
52
53
    private function locateFilesInFilesInFilesDefinitions(array $locations) : Generator
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
54
    {
55
        foreach ($locations as $location) {
56
            if (is_file($location)) {
57
                yield $location;
58
            }
59
60
            yield from $this->extractFilesFromDirectory($location);
61
        }
62
    }
63
64
    private function extractFilesFromDirectory(string $directory) : Generator
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
65
    {
66
        yield from (new LocateAllFilesByExtension())->__invoke(new \ArrayIterator([$directory]), '.php');
67
    }
68
}
69