File tree Expand file tree Collapse file tree 7 files changed +62
-6
lines changed
Expand file tree Collapse file tree 7 files changed +62
-6
lines changed Original file line number Diff line number Diff line change @@ -46,7 +46,8 @@ public function getClassNameFromFile($file)
4646 }
4747 }
4848
49- if ($ tokens [$ i ][0 ] === \T_CLASS ) {
49+ $ token = $ tokens [$ i ][0 ];
50+ if ($ token === \T_INTERFACE || $ token === \T_CLASS || $ token === \T_TRAIT ) {
5051 for ($ j = $ i + 1 ; $ j < count ($ tokens ); $ j ++) {
5152 if ($ tokens [$ j ][0 ] === \T_STRING ) {
5253 $ class = $ tokens [$ i + 2 ][1 ];
Original file line number Diff line number Diff line change 66use Phpactor \ClassFileConverter \Domain \FilePathCandidates ;
77use Phpactor \ClassFileConverter \Domain \ClassName ;
88use Phpactor \ClassFileConverter \Domain \FilePath ;
9+ use RecursiveDirectoryIterator ;
10+ use RecursiveIteratorIterator ;
11+ use RegexIterator ;
912
1013class SimpleClassToFile implements ClassToFile
1114{
@@ -29,15 +32,19 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates
2932 {
3033 $ candidates = [];
3134 $ pattern = sprintf (
32- '%s/**/%s.php ' ,
33- $ this ->cwd ,
35+ '{^.*/%s.php$} ' ,
3436 $ className ->name ()
3537 );
36- foreach (glob ($ pattern ) as $ phpFile ) {
38+
39+ $ iterator = new RecursiveDirectoryIterator ($ this ->cwd );
40+ $ iterator = new RecursiveIteratorIterator ($ iterator );
41+ $ iterator = new RegexIterator ($ iterator , $ pattern );
42+
43+ foreach ($ iterator as $ phpFile ) {
3744 if (ClassName::fromString (
38- $ this ->classScanner ->getClassNameFromFile ($ phpFile )
45+ $ this ->classScanner ->getClassNameFromFile ($ phpFile-> getPathName () )
3946 ) == $ className ) {
40- $ candidates [] = FilePath::fromString ($ phpFile );
47+ $ candidates [] = FilePath::fromString ($ phpFile-> getPathName () );
4148 }
4249 }
4350
Original file line number Diff line number Diff line change @@ -33,6 +33,15 @@ public function testClassToFile()
3333 ]), $ candidates );
3434 }
3535
36+ public function testClassToFileDeeper ()
37+ {
38+ $ candidates = $ this ->classToFile ->classToFileCandidates (ClassName::fromString ('Acme \\NamespaceHere \\Hallo ' ));
39+
40+ $ this ->assertEquals (FilePathCandidates::fromFilePaths ([
41+ FilePath::fromString (__DIR__ . '/../workspace/lib/NamespaceHere/Hallo.php ' )
42+ ]), $ candidates );
43+ }
44+
3645 public function testClassToNoCandidates ()
3746 {
3847 $ candidates = $ this ->classToFile ->classToFileCandidates (ClassName::fromString ('Zog \\Foobar ' ));
Original file line number Diff line number Diff line change @@ -32,6 +32,24 @@ public function testFileToClass()
3232 ]), $ candidates );
3333 }
3434
35+ public function testFileToInterface ()
36+ {
37+ $ candidates = $ this ->fileToClass ->fileToClassCandidates (FilePath::fromString (__DIR__ . '/project/lib/FoobarInterface.php ' ));
38+
39+ $ this ->assertEquals (ClassNameCandidates::fromClassNames ([
40+ ClassName::fromString ('Acme \\FoobarInterface ' )
41+ ]), $ candidates );
42+ }
43+
44+ public function testFileToTrait ()
45+ {
46+ $ candidates = $ this ->fileToClass ->fileToClassCandidates (FilePath::fromString (__DIR__ . '/project/lib/FoobarTrait.php ' ));
47+
48+ $ this ->assertEquals (ClassNameCandidates::fromClassNames ([
49+ ClassName::fromString ('Acme \\FoobarTrait ' )
50+ ]), $ candidates );
51+ }
52+
3553 public function testFileToNoCandidates ()
3654 {
3755 $ candidates = $ this ->fileToClass ->fileToClassCandidates (FilePath::fromString (__DIR__ . '/project/lib/NoClasses.php ' ));
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Acme ;
4+
5+ interface FoobarInterface
6+ {
7+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Acme ;
4+
5+ trait FoobarTrait
6+ {
7+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Acme \NamespaceHere ;
4+
5+ class Hallo
6+ {
7+ }
You can’t perform that action at this time.
0 commit comments