Skip to content

Simple improve #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Adapter/Simple/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
17 changes: 12 additions & 5 deletions lib/Adapter/Simple/SimpleClassToFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/Simple/SimpleClassToFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Simple/SimpleFileToClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/Simple/project/lib/FoobarInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme;

interface FoobarInterface
{
}
7 changes: 7 additions & 0 deletions tests/Integration/Simple/project/lib/FoobarTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme;

trait FoobarTrait
{
}
7 changes: 7 additions & 0 deletions tests/Integration/Simple/project/lib/NamespaceHere/Hallo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme\NamespaceHere;

class Hallo
{
}