1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phpactor\ClassFileConverter\Adapter\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\Autoload\ClassLoader; |
6
|
|
|
use Phpactor\ClassFileConverter\Domain\FilePath; |
7
|
|
|
use Phpactor\ClassFileConverter\Domain\FileToClass; |
8
|
|
|
use Phpactor\ClassFileConverter\Domain\ClassNameCandidates; |
9
|
|
|
use Webmozart\PathUtil\Path; |
10
|
|
|
|
11
|
|
|
final class ComposerFileToClass implements FileToClass |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ClassLoader |
15
|
|
|
*/ |
16
|
|
|
private $classLoader; |
17
|
|
|
|
18
|
|
|
public function __construct(ClassLoader $classLoader) |
19
|
|
|
{ |
20
|
|
|
$this->classLoader = $classLoader; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function fileToClassCandidates(FilePath $filePath): ClassNameCandidates |
24
|
|
|
{ |
25
|
|
|
if (false === $filePath->isAbsolute()) { |
26
|
|
|
throw new \InvalidArgumentException(sprintf( |
27
|
|
|
'Path must be absolute' |
28
|
|
|
)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$classNames = []; |
32
|
|
|
|
33
|
|
|
foreach ($this->getStrategies() as $strategy) { |
34
|
|
|
list($prefixes, $inflector) = $strategy; |
35
|
|
|
$candidates = $this->populateCandidates($filePath, $prefixes); |
36
|
|
|
|
37
|
|
|
foreach ($candidates as $candidate) { |
38
|
|
|
list($pathPrefix, $classPrefix) = $candidate; |
39
|
|
|
$classNames[] = $inflector->inflectToClassName($filePath, $pathPrefix, $classPrefix); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return ClassNameCandidates::fromClassNames($classNames); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
private function getStrategies(): array |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
[ |
50
|
|
|
$this->classLoader->getClassMap(), |
51
|
|
|
new ClassmapNameInflector(), |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
$this->classLoader->getPrefixesPsr4(), |
55
|
|
|
new Psr4NameInflector(), |
56
|
|
|
], |
57
|
|
|
[ |
58
|
|
|
$this->classLoader->getPrefixes(), |
59
|
|
|
new Psr0NameInflector(), |
60
|
|
|
], |
61
|
|
|
[ |
62
|
|
|
$this->classLoader->getFallbackDirs(), |
63
|
|
|
new Psr0NameInflector(), |
64
|
|
|
], |
65
|
|
|
[ |
66
|
|
|
$this->classLoader->getFallbackDirsPsr4(), |
67
|
|
|
new Psr4NameInflector(), |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function populateCandidates(FilePath $filePath, array $prefixes) |
73
|
|
|
{ |
74
|
|
|
$candidates = []; |
75
|
|
|
foreach ($prefixes as $classPrefix => $pathPrefixes) { |
76
|
|
|
|
77
|
|
|
$pathPrefixes = (array) $pathPrefixes; |
78
|
|
|
|
79
|
|
|
// remove any relativeness from the paths |
80
|
|
|
// |
81
|
|
|
// TODO: realpath will return void if the path does not exist |
82
|
|
|
// we should not depend on the file path existing. |
83
|
|
|
$pathPrefixes = array_map(function ($pathPrefix) { |
84
|
|
|
return Path::canonicalize($pathPrefix); |
85
|
|
|
}, $pathPrefixes); |
86
|
|
|
|
87
|
|
|
foreach ($pathPrefixes as $pathPrefix) { |
88
|
|
|
|
89
|
|
|
if ((string) $filePath == $pathPrefix) { |
90
|
|
|
$candidates[] = [ $pathPrefix, $classPrefix ]; |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
if (strpos($filePath, $pathPrefix) !== 0) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$candidates[] = [ |
100
|
|
|
$pathPrefix, |
101
|
|
|
$classPrefix, |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
usort($candidates, function (array $leftCandidate, array $rightCandidate): int { |
107
|
|
|
return strlen($rightCandidate[0]) <=> strlen($leftCandidate[0]); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
return $candidates; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.