From 30e8afd244da3fd4743bbe4d1242c86c3df8401f Mon Sep 17 00:00:00 2001 From: dantleech Date: Sun, 21 Jan 2018 17:09:29 +0100 Subject: [PATCH 1/2] Imporved simple location --- lib/Adapter/Simple/ClassScanner.php | 3 ++- lib/Adapter/Simple/SimpleClassToFile.php | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Adapter/Simple/ClassScanner.php b/lib/Adapter/Simple/ClassScanner.php index 1a945d86..d3027c24 100644 --- a/lib/Adapter/Simple/ClassScanner.php +++ b/lib/Adapter/Simple/ClassScanner.php @@ -46,7 +46,8 @@ public function getClassNameFromFile($file) } } - if ($tokens[$i][0] === \T_CLASS) { + $token = $tokens[$i][0]; + if ($token === \T_INTERFACE || $token === \T_CLASS || $token === \T_TRAIT) { for ($j = $i + 1; $j < count($tokens); $j++) { if ($tokens[$j][0] === \T_STRING) { $class = $tokens[$i + 2][1]; diff --git a/lib/Adapter/Simple/SimpleClassToFile.php b/lib/Adapter/Simple/SimpleClassToFile.php index 8653f1fd..05d13bad 100644 --- a/lib/Adapter/Simple/SimpleClassToFile.php +++ b/lib/Adapter/Simple/SimpleClassToFile.php @@ -6,6 +6,9 @@ use Phpactor\ClassFileConverter\Domain\FilePathCandidates; use Phpactor\ClassFileConverter\Domain\ClassName; use Phpactor\ClassFileConverter\Domain\FilePath; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; +use RegexIterator; class SimpleClassToFile implements ClassToFile { @@ -29,15 +32,19 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates { $candidates = []; $pattern = sprintf( - '%s/**/%s.php', - $this->cwd, + '{^.*/%s.php$}', $className->name() ); - foreach (glob($pattern) as $phpFile) { + + $iterator = new RecursiveDirectoryIterator($this->cwd); + $iterator = new RecursiveIteratorIterator($iterator); + $iterator = new RegexIterator($iterator, $pattern); + + foreach ($iterator as $phpFile) { if (ClassName::fromString( - $this->classScanner->getClassNameFromFile($phpFile) + $this->classScanner->getClassNameFromFile($phpFile->getPathName()) ) == $className) { - $candidates[] = FilePath::fromString($phpFile); + $candidates[] = FilePath::fromString($phpFile->getPathName()); } } From 77f0add6d661202d67c79a34c5244dc49738767d Mon Sep 17 00:00:00 2001 From: dantleech Date: Sun, 21 Jan 2018 17:15:44 +0100 Subject: [PATCH 2/2] Added tests --- .../Simple/SimpleClassToFileTest.php | 9 +++++++++ .../Simple/SimpleFileToClassTest.php | 18 ++++++++++++++++++ .../Simple/project/lib/FoobarInterface.php | 7 +++++++ .../Simple/project/lib/FoobarTrait.php | 7 +++++++ .../Simple/project/lib/NamespaceHere/Hallo.php | 7 +++++++ 5 files changed, 48 insertions(+) create mode 100644 tests/Integration/Simple/project/lib/FoobarInterface.php create mode 100644 tests/Integration/Simple/project/lib/FoobarTrait.php create mode 100644 tests/Integration/Simple/project/lib/NamespaceHere/Hallo.php diff --git a/tests/Integration/Simple/SimpleClassToFileTest.php b/tests/Integration/Simple/SimpleClassToFileTest.php index b74ad8c7..a4548769 100644 --- a/tests/Integration/Simple/SimpleClassToFileTest.php +++ b/tests/Integration/Simple/SimpleClassToFileTest.php @@ -33,6 +33,15 @@ public function testClassToFile() ]), $candidates); } + public function testClassToFileDeeper() + { + $candidates = $this->classToFile->classToFileCandidates(ClassName::fromString('Acme\\NamespaceHere\\Hallo')); + + $this->assertEquals(FilePathCandidates::fromFilePaths([ + FilePath::fromString(__DIR__ . '/../workspace/lib/NamespaceHere/Hallo.php') + ]), $candidates); + } + public function testClassToNoCandidates() { $candidates = $this->classToFile->classToFileCandidates(ClassName::fromString('Zog\\Foobar')); diff --git a/tests/Integration/Simple/SimpleFileToClassTest.php b/tests/Integration/Simple/SimpleFileToClassTest.php index dd406543..2e463e55 100644 --- a/tests/Integration/Simple/SimpleFileToClassTest.php +++ b/tests/Integration/Simple/SimpleFileToClassTest.php @@ -32,6 +32,24 @@ public function testFileToClass() ]), $candidates); } + public function testFileToInterface() + { + $candidates = $this->fileToClass->fileToClassCandidates(FilePath::fromString(__DIR__ . '/project/lib/FoobarInterface.php')); + + $this->assertEquals(ClassNameCandidates::fromClassNames([ + ClassName::fromString('Acme\\FoobarInterface') + ]), $candidates); + } + + public function testFileToTrait() + { + $candidates = $this->fileToClass->fileToClassCandidates(FilePath::fromString(__DIR__ . '/project/lib/FoobarTrait.php')); + + $this->assertEquals(ClassNameCandidates::fromClassNames([ + ClassName::fromString('Acme\\FoobarTrait') + ]), $candidates); + } + public function testFileToNoCandidates() { $candidates = $this->fileToClass->fileToClassCandidates(FilePath::fromString(__DIR__ . '/project/lib/NoClasses.php')); diff --git a/tests/Integration/Simple/project/lib/FoobarInterface.php b/tests/Integration/Simple/project/lib/FoobarInterface.php new file mode 100644 index 00000000..0f6be8df --- /dev/null +++ b/tests/Integration/Simple/project/lib/FoobarInterface.php @@ -0,0 +1,7 @@ +