Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
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
79 changes: 70 additions & 9 deletions lib/Adapter/WorseReflection/Refactor/WorseExtractMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Microsoft\PhpParser\Node\Statement\ReturnStatement;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\CodeBuilder\Domain\Updater;
use Phpactor\TextDocument\TextDocumentEdits;
use Phpactor\TextDocument\TextDocumentUri;
use Phpactor\TextDocument\TextEdit;
use Phpactor\WorseReflection\Reflector;
use Microsoft\PhpParser\Parser;
use Phpactor\CodeBuilder\Domain\BuilderFactory;
Expand All @@ -22,6 +25,8 @@
use Phpactor\CodeTransform\Domain\Exception\TransformException;
use Phpactor\CodeTransform\Domain\Utils\TextUtils;
use Phpactor\WorseReflection\Core\Reflection\ReflectionMethod;
use function iterator_to_array;
use function prev;

class WorseExtractMethod implements ExtractMethod
{
Expand Down Expand Up @@ -53,8 +58,68 @@ public function __construct(Reflector $reflector, BuilderFactory $factory, Updat
$this->factory = $factory;
}

public function extractMethod(SourceCode $source, int $offsetStart, int $offsetEnd, string $name): SourceCode
public function canExtractMethod(SourceCode $source, int $offsetStart, int $offsetEnd): bool
{
if ($offsetEnd == $offsetStart) {
return false;
}
$node = $this->parser->parseSourceFile($source->__toString());
$endNode = $node->getDescendantNodeAtPosition($offsetEnd);
$startNode = $node->getDescendantNodeAtPosition($offsetStart);

if (
$endNode instanceof CompoundStatementNode &&
$endNode->openBrace->getEndPosition() < $offsetEnd &&
$endNode->closeBrace->getStartPosition() >= $offsetEnd &&
count($endNode->statements) > 0
) {
$stmt = end($endNode->statements);
assert($stmt instanceof Node);
while ($stmt && $stmt->getEndPosition() > $offsetEnd) {
$stmt = prev($endNode->statements);
}

$endNode = $stmt ?? $endNode;
assert($endNode instanceof Node);
}

while ($endNode->parent && !($endNode->parent instanceof CompoundStatementNode)) {
$endNode = $endNode->parent;
}

if (
$startNode instanceof CompoundStatementNode &&
$startNode->openBrace->getEndPosition() <= $offsetStart &&
$startNode->closeBrace->getStartPosition() > $offsetStart &&
count($startNode->statements) > 0
) {
$stmt = current($startNode->statements);
assert($stmt instanceof Node);
while ($stmt && $stmt->getStart() < $offsetStart) {
$stmt = next($startNode->statements);
}

$startNode = $stmt ?? $startNode;
assert($startNode instanceof Node);
}

while ($startNode->parent && !($startNode->parent instanceof CompoundStatementNode)) {
$startNode = $startNode->parent;
}

if ($startNode->parent != $endNode->parent) {
return false;
}

return true;
}

public function extractMethod(SourceCode $source, int $offsetStart, int $offsetEnd, string $name): TextDocumentEdits
{
if (!$this->canExtractMethod($source, $offsetStart, $offsetEnd)) {
throw new TransformException('Cannot extract method. Check if start and end statements are in different scopes.');
}

$isExpression = $this->isSelectionAnExpression($source, $offsetStart, $offsetEnd);

$selection = $source->extractSelection($offsetStart, $offsetEnd);
Expand Down Expand Up @@ -89,14 +154,10 @@ public function extractMethod(SourceCode $source, int $offsetStart, int $offsetE
$replacement = rtrim($replacement, ';');
}

$source = $source->replaceSelection(
$replacement,
$offsetStart,
$offsetEnd
);

return $source->withSource(
(string) $this->updater->textEditsFor($prototype, Code::fromString((string) $source))->apply($source)
return new TextDocumentEdits(
TextDocumentUri::fromString($source->path()),
$this->updater->textEditsFor($prototype, Code::fromString((string) $source))
->add(TextEdit::create($offsetStart, $offsetEnd - $offsetStart, $replacement))
);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Domain/Refactor/ExtractMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Phpactor\CodeTransform\Domain\Refactor;

use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\TextDocument\TextDocumentEdits;

interface ExtractMethod
{
public function extractMethod(SourceCode $source, int $offsetStart, int $offsetEnd, string $name): SourceCode;
public function canExtractMethod(SourceCode $source, int $offsetStart, int $offsetEnd): bool;
public function extractMethod(SourceCode $source, int $offsetStart, int $offsetEnd, string $name): TextDocumentEdits;
}
41 changes: 37 additions & 4 deletions tests/Adapter/WorseReflection/Refactor/WorseExtractMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Phpactor\CodeTransform\Tests\Adapter\WorseReflection\WorseTestCase;
use Phpactor\CodeTransform\Adapter\WorseReflection\Refactor\WorseExtractMethod;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\WorseReflection\Core\SourceCode as WorseSourceCode;
use Phpactor\CodeBuilder\Adapter\WorseReflection\WorseBuilderFactory;
use Exception;

Expand All @@ -13,7 +14,7 @@ class WorseExtractMethodTest extends WorseTestCase
/**
* @dataProvider provideExtractMethod
*/
public function testExtractMethod(string $test, $name, $expectedExceptionMessage = null): void
public function testExtractMethod(string $test, ?string $name, ?string $expectedExceptionMessage = null): void
{
list($source, $expected, $offsetStart, $offsetEnd) = $this->sourceExpectedAndOffset(__DIR__ . '/fixtures/' . $test);

Expand All @@ -22,15 +23,23 @@ public function testExtractMethod(string $test, $name, $expectedExceptionMessage
$this->expectExceptionMessage($expectedExceptionMessage);
}

$reflector = $this->reflectorForWorkspace($source);
$worseSourceCode = WorseSourceCode::fromPathAndString('file:///source', $source);
$reflector = $this->reflectorForWorkspace($worseSourceCode);

$factory = new WorseBuilderFactory($reflector);
$extractMethod = new WorseExtractMethod($reflector, $factory, $this->updater());
$transformed = $extractMethod->extractMethod(SourceCode::fromString($source), $offsetStart, $offsetEnd, $name);

$sourceCode = SourceCode::fromStringAndPath($source, 'file:///source');
$textDocumentEdits = $extractMethod->extractMethod($sourceCode, $offsetStart, $offsetEnd, $name);

$transformed = SourceCode::fromStringAndPath(
(string) $textDocumentEdits->textEdits()->apply($sourceCode),
$textDocumentEdits->uri()->path()
);
$this->assertEquals(trim($expected), trim($transformed));
}

public function provideExtractMethod()
public function provideExtractMethod(): array
{
return [
'no free variables' => [
Expand Down Expand Up @@ -126,6 +135,30 @@ public function provideExtractMethod()
'extractMethod23.test',
'newMethod',
],
'different scopes 1' => [
'extractMethod24.test',
'newMethod',
'Cannot extract method. Check if start and end statements are in different scopes.'
],
'different scopes 2' => [
'extractMethod25.test',
'newMethod',
'Cannot extract method. Check if start and end statements are in different scopes.'
],
'different scopes 3' => [
'extractMethod26.test',
'newMethod',
'Cannot extract method. Check if start and end statements are in different scopes.'
],
'empty text selection' => [
'extractMethod27.test',
'newMethod',
],
'empty selection 2' => [
'extractMethod28.test',
'newMethod',
'Cannot extract method. Check if start and end statements are in different scopes.'
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// File: source
<?php

class One {
public function hello() {
$a = function() {
<>$expression1 = 1;
};
$expression2 = 2;<>
}
}
// File: expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// File: source
<?php

class One {
public function hello() {
{
<>$b = 4;
}
$c = 5;<>
}
}
// File: expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// File: source
<?php

class One {
public function hello() {
{
{
$a = 11;
<>
$b = 22;
}
$c = 33;
<>
$d = 44;
}
}
}
// File: expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// File: source
<?php

class One {
public function hello() {
{
{
$a = 11;
$b = 22;
}<>
<>
}
}
}
// File: expected
<?php

class One {
public function hello() {
{
{
$a = 11;
$b = 22;
}$this->newMethod();
}
}

private function newMethod()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// File: source
<?php

class One {
public function hello() {
{
{
$a = 11;
$b = 22;
}<><>
}
}
}
// File: expected