ComposerClassToFile::getFileCandidates()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.4106
c 0
b 0
f 0
cc 7
nc 7
nop 2
1
<?php
2
3
namespace Phpactor\ClassFileConverter\Adapter\Composer;
4
5
use Phpactor\ClassFileConverter\Domain\ClassToFile;
6
use Composer\Autoload\ClassLoader;
7
use Phpactor\ClassFileConverter\Domain\ClassName;
8
use Phpactor\ClassFileConverter\Domain\FilePathCandidates;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12
class ComposerClassToFile implements ClassToFile
13
{
14
    /**
15
     * @var ClassLoader
16
     */
17
    private $classLoader;
18
19
    /**
20
     * @var LoggerInterface
21
     */
22
    private $logger;
23
24
    public function __construct(ClassLoader $classLoader, LoggerInterface $logger = null)
25
    {
26
        $this->classLoader = $classLoader;
27
        $this->logger = $logger ?: new NullLogger();
28
    }
29
30
    public function classToFileCandidates(ClassName $className): FilePathCandidates
31
    {
32
        $candidates = [];
33
        foreach ($this->getStrategies() as $strategy) {
34
            list($prefixes, $inflector) = $strategy;
35
            $this->resolveFile($candidates, $prefixes, $inflector, $className);
36
        }
37
38
        // order with the longest prefixes first
39
        uksort($candidates, function ($prefix1, $prefix2) {
40
            return strlen($prefix2) <=> strlen($prefix1);
41
        });
42
43
        // flatten to a single array
44
        $candidates = array_reduce($candidates, function ($candidates, $paths) {
45
            return array_merge($candidates, $paths);
46
        }, []);
47
48
        return FilePathCandidates::fromFilePaths($candidates);
49
    }
50
51 View Code Duplication
    private function getStrategies(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        return [
54
            [
55
                $this->classLoader->getPrefixesPsr4(),
56
                new Psr4NameInflector(),
57
            ],
58
            [
59
                $this->classLoader->getPrefixes(),
60
                new Psr0NameInflector(),
61
            ],
62
            [
63
                $this->classLoader->getClassMap(),
64
                new ClassmapNameInflector(),
65
            ],
66
            [
67
                $this->classLoader->getFallbackDirs(),
68
                new Psr0NameInflector(),
69
            ],
70
            [
71
                $this->classLoader->getFallbackDirsPsr4(),
72
                // PSR0 name inflector works here as there is no prefix
73
                new Psr0NameInflector(),
74
            ],
75
        ];
76
    }
77
78
    private function resolveFile(&$candidates, array $prefixes, NameInflector $inflector, ClassName $className)
79
    {
80
        $fileCandidates = $this->getFileCandidates($className, $prefixes);
81
82
        foreach ($fileCandidates as $prefix => $files) {
83
            $prefixCandidates = [];
84
            foreach ($files as $file) {
85
                $prefixCandidates[] = $inflector->inflectToRelativePath($prefix, $className, $file);
86
            }
87
88
            if (!isset($candidates[$prefix])) {
89
                $candidates[$prefix] = [];
90
            }
91
92
            $candidates[$prefix] = array_merge($candidates[$prefix], $prefixCandidates);
93
        }
94
    }
95
96
    private function getFileCandidates(ClassName $className, array $prefixes)
97
    {
98
        $candidates = [];
99
100
        foreach ($prefixes as $prefix => $paths) {
101
            if (is_int($prefix)) {
102
                $prefix = '';
103
            }
104
105
            if ($prefix && false === $className->beginsWith($prefix)) {
106
                continue;
107
            }
108
109
            if (!isset($candidates[$prefix])) {
110
                $candidates[$prefix] = [];
111
            }
112
113
            $paths = (array) $paths;
114
            $paths = array_map(function ($path) {
115
                if (!file_exists($path)) {
116
                    $this->logger->warning(sprintf(
117
                        'Composer mapped path "%s" does not exist',
118
                        $path
119
                    ));
120
121
                    return $path;
122
                }
123
124
                return realpath($path);
125
            }, $paths);
126
127
            $candidates[$prefix] = array_merge($candidates[$prefix], $paths);
128
        }
129
130
        return $candidates;
131
    }
132
}
133