From a21decefabb06398af548e761efa51a4b622ef26 Mon Sep 17 00:00:00 2001 From: przepompownia Date: Tue, 12 Apr 2022 22:58:38 +0200 Subject: [PATCH 1/2] Add getNamespaceSeparator() to NameInflector --- lib/Adapter/Composer/ClassmapNameInflector.php | 7 +++++++ lib/Adapter/Composer/ComposerClassToFile.php | 14 ++++---------- lib/Adapter/Composer/NameInflector.php | 2 ++ lib/Adapter/Composer/Psr0NameInflector.php | 5 +++++ lib/Adapter/Composer/Psr4NameInflector.php | 5 +++++ 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/Adapter/Composer/ClassmapNameInflector.php b/lib/Adapter/Composer/ClassmapNameInflector.php index c4e63ba8..e45c30f1 100644 --- a/lib/Adapter/Composer/ClassmapNameInflector.php +++ b/lib/Adapter/Composer/ClassmapNameInflector.php @@ -7,6 +7,8 @@ final class ClassmapNameInflector implements NameInflector { + public const NAMESPACE_SEPARATOR = ClassName::DEFAULT_NAMESPACE_SEPARATOR; + public function inflectToRelativePath(string $prefix, ClassName $className, string $mappedPath): FilePath { return FilePath::fromString($mappedPath); @@ -16,4 +18,9 @@ public function inflectToClassName(FilePath $filePath, string $pathPrefix, strin { return ClassName::fromString($classPrefix); } + + public function getNamespaceSeparator(): string + { + return self::NAMESPACE_SEPARATOR; + } } diff --git a/lib/Adapter/Composer/ComposerClassToFile.php b/lib/Adapter/Composer/ComposerClassToFile.php index 96ca8333..7c1cca39 100644 --- a/lib/Adapter/Composer/ComposerClassToFile.php +++ b/lib/Adapter/Composer/ComposerClassToFile.php @@ -25,8 +25,8 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates { $candidates = []; foreach ($this->getStrategies() as $strategy) { - list($prefixes, $inflector, $separator) = $strategy; - $this->resolveFile($candidates, $prefixes, $inflector, $className, $separator); + [$prefixes, $inflector] = $strategy; + $this->resolveFile($candidates, $prefixes, $inflector, $className, $inflector->getNamespaceSeparator()); } // order with the longest prefixes first @@ -48,28 +48,23 @@ private function getStrategies(): array [ $this->classLoader->getPrefixesPsr4(), new Psr4NameInflector(), - Psr4NameInflector::NAMESPACE_SEPARATOR, ], [ $this->classLoader->getPrefixes(), new Psr0NameInflector(), - Psr0NameInflector::NAMESPACE_SEPARATOR, ], [ $this->classLoader->getClassMap(), new ClassmapNameInflector(), - Psr4NameInflector::NAMESPACE_SEPARATOR, ], [ $this->classLoader->getFallbackDirs(), new Psr0NameInflector(), - Psr0NameInflector::NAMESPACE_SEPARATOR, ], [ $this->classLoader->getFallbackDirsPsr4(), // PSR0 name inflector works here as there is no prefix new Psr0NameInflector(), - Psr0NameInflector::NAMESPACE_SEPARATOR, ], ]; } @@ -78,10 +73,9 @@ private function resolveFile( &$candidates, array $prefixes, NameInflector $inflector, - ClassName $className, - string $separator + ClassName $className ): void { - $fileCandidates = $this->getFileCandidates($className, $prefixes, $separator); + $fileCandidates = $this->getFileCandidates($className, $prefixes, $inflector->getNamespaceSeparator()); foreach ($fileCandidates as $prefix => $files) { $prefixCandidates = []; diff --git a/lib/Adapter/Composer/NameInflector.php b/lib/Adapter/Composer/NameInflector.php index ffc1e718..bba82345 100644 --- a/lib/Adapter/Composer/NameInflector.php +++ b/lib/Adapter/Composer/NameInflector.php @@ -10,4 +10,6 @@ interface NameInflector public function inflectToRelativePath(string $prefix, ClassName $className, string $mappedPath): FilePath; public function inflectToClassName(FilePath $filePath, string $pathPrefix, string $classPrefix): ClassName; + + public function getNamespaceSeparator(): string; } diff --git a/lib/Adapter/Composer/Psr0NameInflector.php b/lib/Adapter/Composer/Psr0NameInflector.php index e6162ebd..733b6359 100644 --- a/lib/Adapter/Composer/Psr0NameInflector.php +++ b/lib/Adapter/Composer/Psr0NameInflector.php @@ -31,4 +31,9 @@ public function inflectToClassName(FilePath $filePath, string $pathPrefix, strin return ClassName::fromString($className); } + + public function getNamespaceSeparator(): string + { + return self::NAMESPACE_SEPARATOR; + } } diff --git a/lib/Adapter/Composer/Psr4NameInflector.php b/lib/Adapter/Composer/Psr4NameInflector.php index 02721881..8087d5e8 100644 --- a/lib/Adapter/Composer/Psr4NameInflector.php +++ b/lib/Adapter/Composer/Psr4NameInflector.php @@ -30,4 +30,9 @@ public function inflectToClassName(FilePath $filePath, string $pathPrefix, strin return ClassName::fromString($className); } + + public function getNamespaceSeparator(): string + { + return self::NAMESPACE_SEPARATOR; + } } From 7ba60aab02f2b0535cb7b821d3bb8963a6340c51 Mon Sep 17 00:00:00 2001 From: przepompownia Date: Tue, 12 Apr 2022 23:01:56 +0200 Subject: [PATCH 2/2] Drop redundant parameter --- lib/Adapter/Composer/ComposerClassToFile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Adapter/Composer/ComposerClassToFile.php b/lib/Adapter/Composer/ComposerClassToFile.php index 7c1cca39..5c89daa0 100644 --- a/lib/Adapter/Composer/ComposerClassToFile.php +++ b/lib/Adapter/Composer/ComposerClassToFile.php @@ -26,7 +26,7 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates $candidates = []; foreach ($this->getStrategies() as $strategy) { [$prefixes, $inflector] = $strategy; - $this->resolveFile($candidates, $prefixes, $inflector, $className, $inflector->getNamespaceSeparator()); + $this->resolveFile($candidates, $prefixes, $inflector, $className); } // order with the longest prefixes first