Completed
Pull Request — master (#9)
by Matthias
02:30
created

LocateComposerPackageSourceFiles   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 62
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 1
A getFilePaths() 0 9 1
A locateFilesInPsr0Definitions() 0 4 1
A locateFilesInPsr4Definitions() 0 4 1
A locateFilesInClassmapDefinitions() 0 4 1
A locateFilesInFilesInFilesDefinitions() 0 10 3
A extractFilesFromDirectory() 0 4 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