Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Improvements:

- [WorseReflection] Resolve UseNameVariables (e.g. context menu `use ($f<>oo)`). #466
- [Application] Improved status (show current version) #481
- [CodeTransform] Better handling of new file generation

Bug fixes:

Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/Extension/ClassMover/Rpc/ClassCopyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ClassCopyHandler extends AbstractHandler
const PARAM_SOURCE_PATH = 'source_path';
const PARAM_DEST_PATH = 'dest_path';


/**
* @var ClassCopy
*/
Expand Down
12 changes: 7 additions & 5 deletions lib/Extension/CodeTransform/Application/ClassInflect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phpactor\Extension\CodeTransform\Application;

use Phpactor\CodeTransform\Domain\ClassName;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Phpactor;
use Phpactor\Extension\Core\Application\Helper\FilesystemHelper;
use Phpactor\WorseReflection\Core\Exception\NotFound;
Expand All @@ -13,7 +14,7 @@ class ClassInflect extends AbstractClassGenerator
public function generateFromExisting(string $srcPath, string $dest, string $variant = 'default', bool $overwrite = false): array
{
$srcPath = Phpactor::normalizePath($srcPath);
$newPaths = [];
$newCodes = [];

if (false === Glob::isDynamic($srcPath) && false === file_exists($srcPath)) {
throw new \RuntimeException(sprintf(
Expand All @@ -28,16 +29,16 @@ public function generateFromExisting(string $srcPath, string $dest, string $vari
}

try {
$this->doGenerateFromExisting($globSrc, $globDest, $variant, $overwrite);
$newCodes[] = $this->doGenerateFromExisting($globSrc, $globDest, $variant, $overwrite);
} catch (NotFound $e) {
$this->logger()->error($e->getMessage());
}
}

return $newPaths;
return $newCodes;
}

private function doGenerateFromExisting(string $src, string $dest, string $variant, bool $overwrite): string
private function doGenerateFromExisting(string $src, string $dest, string $variant, bool $overwrite): SourceCode
{
$srcClassName = $this->normalizer->normalizeToClass($src);
$destClassName = $this->normalizer->normalizeToClass($dest);
Expand All @@ -48,9 +49,10 @@ private function doGenerateFromExisting(string $src, string $dest, string $varia
);

$filePath = $this->normalizer->normalizeToFile($destClassName);
$code = $code->withPath($filePath);

$this->writeFile($filePath, (string) $code, $overwrite);

return (string) $filePath;
return $code;
}
}
7 changes: 5 additions & 2 deletions lib/Extension/CodeTransform/Application/ClassNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
namespace Phpactor\Extension\CodeTransform\Application;

use Phpactor\CodeTransform\Domain\ClassName;
use Phpactor\CodeTransform\Domain\SourceCode;

class ClassNew extends AbstractClassGenerator
{
public function generate(string $src, string $variant = 'default', bool $overwrite = false): string
public function generate(string $src, string $variant = 'default', bool $overwrite = false): SourceCode
{
$className = $this->normalizer->normalizeToClass($src);

$code = $this->generators->get($variant)->generateNew(ClassName::fromString((string) $className));
$filePath = $this->normalizer->normalizeToFile($className);

$code = $code->withPath($filePath);

$this->writeFile($filePath, (string) $code, $overwrite);

return $filePath;
return $code;
}
}
11 changes: 7 additions & 4 deletions lib/Extension/CodeTransform/Rpc/AbstractClassGenerateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Phpactor\Extension\CodeTransform\Rpc;

use Phpactor\Application\ClassGenerator;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\CodeTransform\Application\ClassNew;
use Phpactor\Extension\Rpc\Response\Input\TextInput;
use Phpactor\Extension\Rpc\Response\Input\ChoiceInput;
use Phpactor\Extension\Rpc\Response\InputCallbackResponse;
Expand All @@ -13,6 +15,7 @@
use Phpactor\Extension\Rpc\Response\Input\ConfirmInput;
use Phpactor\Extension\CodeTransform\Application\AbstractClassGenerator;
use Phpactor\Extension\Rpc\Handler\AbstractHandler;
use Phpactor\Extension\Rpc\Response\ReplaceFileSourceResponse;

abstract class AbstractClassGenerateHandler extends AbstractHandler
{
Expand All @@ -22,7 +25,7 @@ abstract class AbstractClassGenerateHandler extends AbstractHandler
const PARAM_OVERWRITE = 'overwrite';

/**
* @var ClassGenerator
* @var AbstractClassGenerator
*/
protected $classGenerator;

Expand All @@ -41,7 +44,7 @@ public function defaultParameters(): array
];
}

abstract protected function generate(array $arguments);
abstract protected function generate(array $arguments): SourceCode;

abstract protected function newMessage(): string;

Expand Down Expand Up @@ -76,7 +79,7 @@ public function handle(array $arguments)
}

try {
$newPath = $this->generate($arguments);
$code = $this->generate($arguments);
} catch (FileAlreadyExists $e) {
return InputCallbackResponse::fromCallbackAndInputs(
Request::fromNameAndParameters(
Expand All @@ -97,6 +100,6 @@ public function handle(array $arguments)
);
}

return OpenFileResponse::fromPath($newPath);
return ReplaceFileSourceResponse::fromPathAndSource($code->path(), (string) $code);
}
}
11 changes: 6 additions & 5 deletions lib/Extension/CodeTransform/Rpc/ClassInflectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Phpactor\Extension\CodeTransform\Rpc;

use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\Rpc\Handler;
use Webmozart\Glob\Glob;

class ClassInflectHandler extends AbstractClassGenerateHandler
{
const NAME = 'class_inflect';

protected function generate(array $arguments)
protected function generate(array $arguments): SourceCode
{
if (Glob::isDynamic($arguments['current_path'])) {
throw new \RuntimeException(sprintf(
Expand All @@ -18,21 +19,21 @@ protected function generate(array $arguments)
));
}

$newPaths = $this->classGenerator->generateFromExisting(
$newCodes = $this->classGenerator->generateFromExisting(
$arguments[self::PARAM_CURRENT_PATH],
$arguments[self::PARAM_NEW_PATH],
$arguments[self::PARAM_VARIANT],
(bool) $arguments[self::PARAM_OVERWRITE]
);

if (count($newPaths) !== 1) {
if (count($newCodes) !== 1) {
throw new \RuntimeException(sprintf(
'Expected 1 path from class generator, got %s',
count($newPaths)
count($newCodes)
));
}

return reset($newPaths);
return reset($newCodes);
}

public function name(): string
Expand Down
4 changes: 3 additions & 1 deletion lib/Extension/CodeTransform/Rpc/ClassNewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Phpactor\Extension\CodeTransform\Rpc;

use Phpactor\CodeTransform\Domain\SourceCode;

class ClassNewHandler extends AbstractClassGenerateHandler
{
const NAME = 'class_new';

protected function generate(array $arguments)
protected function generate(array $arguments): SourceCode
{
return $this->classGenerator->generate($arguments['new_path'], $arguments['variant'], (bool) $arguments['overwrite']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phpactor\Tests\Unit\Extension\CoreTransform\Rpc;

use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\Rpc\Handler;
use Phpactor\Extension\CodeTransform\Rpc\ClassInflectHandler;
use Phpactor\Extension\CodeTransform\Application\ClassInflect;
Expand All @@ -12,6 +13,7 @@
use Phpactor\Extension\Rpc\Response\OpenFileResponse;
use Phpactor\Extension\Rpc\Response\EchoResponse;
use Phpactor\Extension\Rpc\Response\Input\ConfirmInput;
use Phpactor\Extension\Rpc\Response\ReplaceFileSourceResponse;
use Phpactor\Tests\Unit\Extension\Rpc\Handler\HandlerTestCase;

class ClassInflectHandlerTest extends HandlerTestCase
Expand Down Expand Up @@ -136,15 +138,15 @@ public function testGenerate()
self::NEW_PATH,
self::VARIANT,
false
)->willReturn([ self::NEW_PATH ]);
)->willReturn([ SourceCode::fromStringAndPath('fo', self::NEW_PATH) ]);

$action = $this->handle('class_inflect', [
'current_path' => self::CURRENT_PATH,
'new_path' => self::NEW_PATH,
'variant' => self::VARIANT,
]);

$this->assertInstanceOf(OpenFileResponse::class, $action);
$this->assertInstanceOf(ReplaceFileSourceResponse::class, $action);
$this->assertEquals(self::NEW_PATH, $action->path());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phpactor\Tests\Unit\Extension\CoreTransform\Rpc;

use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\Rpc\Handler;
use Phpactor\Extension\CodeTransform\Rpc\ClassNewHandler;
use Phpactor\Extension\CodeTransform\Application\ClassNew;
Expand All @@ -12,6 +13,7 @@
use Phpactor\Extension\Rpc\Response\OpenFileResponse;
use Phpactor\Extension\Rpc\Response\EchoResponse;
use Phpactor\Extension\Rpc\Response\Input\ConfirmInput;
use Phpactor\Extension\Rpc\Response\ReplaceFileSourceResponse;
use Phpactor\Tests\Unit\Extension\Rpc\Handler\HandlerTestCase;

class ClassNewHandlerTest extends HandlerTestCase
Expand Down Expand Up @@ -100,15 +102,15 @@ public function testGenerate()
self::NEW_PATH,
self::VARIANT,
false
)->willReturn(self::NEW_PATH);
)->willReturn(SourceCode::fromStringAndPath('fo', self::NEW_PATH));

$action = $this->handle('class_new', [
'current_path' => self::CURRENT_PATH,
'new_path' => self::NEW_PATH,
'variant' => self::VARIANT,
]);

$this->assertInstanceOf(OpenFileResponse::class, $action);
$this->assertInstanceOf(ReplaceFileSourceResponse::class, $action);
$this->assertEquals(self::NEW_PATH, $action->path());
}
}