Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Fix #32, change adapter for TokenReflectinoAnalysis #39

Merged
merged 3 commits into from
Apr 14, 2015
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
34 changes: 34 additions & 0 deletions features/optimize_use.feature
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,38 @@ Feature: Optimize use

return $service;
"""
Scenario: Organize use for file without namespace and other uses
Given a PHP File named "src/Foo.php" with:
"""
<?php

class Foo
{
public function operation()
{
return new \Bar\Qux\Adapter();
}
}
"""
When I use refactoring "optimize-use" with:
| arg | value |
| file | src/Foo.php |
Then the PHP File "src/Foo.php" should be refactored:
"""
--- a/vfs://project/src/Foo.php
+++ b/vfs://project/src/Foo.php
@@ -1,4 +1,6 @@
<?php

+use Bar\Qux\Adapter;
+
class Foo
{
@@ -5,5 +5,5 @@
public function operation()
{
- return new \Bar\Qux\Adapter();
+ return new Adapter();
}
}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use TokenReflection\Broker;
use TokenReflection\Broker\Backend\Memory;
use TokenReflection\ReflectionNamespace;

class StaticCodeAnalysis extends CodeAnalysis
{
Expand Down Expand Up @@ -143,11 +144,12 @@ public function findClasses(File $file)

$file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true);
foreach ($file->getNamespaces() as $namespace) {
$noNamespace = ReflectionNamespace::NO_NAMESPACE_NAME === $namespace->getName();
foreach ($namespace->getClasses() as $class) {
$classes[] = new PhpClass(
PhpName::createDeclarationName($class->getName()),
$class->getStartLine(),
$namespace->getStartLine()
$noNamespace ? 0 : $namespace->getStartLine()
);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/QafooLabs/Refactoring/Application/OptimizeUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public function refactor(File $file)
$occurances = $this->phpNameScanner->findNames($file);
$class = $classes[0];

$appendNewLine = 0 === $class->namespaceDeclarationLine();
$lastUseStatementLine = $class->namespaceDeclarationLine() + 2;
$usedNames = array();
$fqcns = array();

foreach ($occurances as $occurance) {
$name = $occurance->name();

if ($name->type() === PhpName::TYPE_NAMESPACE) {
if ($name->type() === PhpName::TYPE_NAMESPACE || $name->type() === PhpName::TYPE_CLASS) {
continue;
}

Expand All @@ -67,7 +68,13 @@ public function refactor(File $file)
$buffer->replaceString($occurance->declarationLine(), '\\'.$name->fullyQualifiedName(), $name->shortname());

if (!in_array($name->fullyQualifiedName(), $usedNames)) {
$buffer->append($lastUseStatementLine, array(sprintf('use %s;', $name->fullyQualifiedName())));
$lines = array(sprintf('use %s;', $name->fullyQualifiedName()));
if ($appendNewLine) {
$appendNewLine = FALSE;
$lines[] = '';
}

$buffer->append($lastUseStatementLine, $lines);
$lastUseStatementLine++;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace QafooLabs\Refactoring\Adapters\TokenReflection;

use PHPUnit_Framework_TestCase;
use QafooLabs\Refactoring\Domain\Model\File;

class StaticCodeAnalysisTest extends PHPUnit_Framework_TestCase
{
public function testNamespaceDeclarationForFileWithoutNamespace_isInLine0()
{
$file = new File('without-namespace.php', <<<'PHP'
<?php

class WithoutNamespace
{

}
PHP
);

$analysis = new StaticCodeAnalysis();
$classes = $analysis->findClasses($file);
$class = $classes[0];

$this->assertEquals(0, $class->namespaceDeclarationLine(), 'namespace declaration line for file without namespace');
}
}