Skip to content

Commit 29def61

Browse files
authored
Merge pull request #6 from phpactor/simple_improve
Simple improve
2 parents f752d27 + 77f0add commit 29def61

File tree

7 files changed

+62
-6
lines changed

7 files changed

+62
-6
lines changed

lib/Adapter/Simple/ClassScanner.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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];

lib/Adapter/Simple/SimpleClassToFile.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Phpactor\ClassFileConverter\Domain\FilePathCandidates;
77
use Phpactor\ClassFileConverter\Domain\ClassName;
88
use Phpactor\ClassFileConverter\Domain\FilePath;
9+
use RecursiveDirectoryIterator;
10+
use RecursiveIteratorIterator;
11+
use RegexIterator;
912

1013
class 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

tests/Integration/Simple/SimpleClassToFileTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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'));

tests/Integration/Simple/SimpleFileToClassTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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'));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Acme;
4+
5+
interface FoobarInterface
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Acme;
4+
5+
trait FoobarTrait
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Acme\NamespaceHere;
4+
5+
class Hallo
6+
{
7+
}

0 commit comments

Comments
 (0)