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 @@ -28,6 +28,7 @@ Improvements:

Bug fixes:

- [code-transform] Generate accessor doesn't work on selected property (regression)
- [vim-plugin] Configuration was not global (#964) - @elythyr
- [class-mover] `$` was removed when renaming static variables (#925) -
@dantleech
Expand Down
38 changes: 38 additions & 0 deletions lib/Extension/CodeTransformExtra/Rpc/GenerateAccessorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use InvalidArgumentException;
use Phpactor\CodeTransform\Domain\Refactor\GenerateAccessor;
use Phpactor\Extension\Rpc\Response;
use Phpactor\Extension\Rpc\Response\Input\ListInput;
use Phpactor\MapResolver\Resolver;
use Phpactor\Extension\Rpc\Response\UpdateFileSourceResponse;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\Rpc\Handler\AbstractHandler;
use Phpactor\WorseReflection\Core\Inference\Symbol;
use Phpactor\WorseReflection\Core\Inference\SymbolContext;
use Phpactor\WorseReflection\Core\Reflection\ReflectionClass;
use Phpactor\WorseReflection\Core\Reflection\ReflectionProperty;
use Phpactor\WorseReflection\Reflector;
Expand Down Expand Up @@ -57,6 +60,26 @@ public function configure(Resolver $resolver)
}

public function handle(array $arguments)
{
if ($context = $this->getPropertyContext($arguments)) {
return $this->handleSingle($context, $arguments);
}

return $this->handleClass($arguments);
}

private function getPropertyContext(array $arguments): ?SymbolContext
{
$offset = $this->reflector->reflectOffset($arguments[self::PARAM_SOURCE], $arguments[self::PARAM_OFFSET]);

if ($offset->symbolContext()->symbol()->symbolType() === Symbol::PROPERTY) {
return $offset->symbolContext();
}

return null;
}

private function handleClass(array $arguments): Response
{
$class = $this->class($arguments[self::PARAM_SOURCE]);

Expand Down Expand Up @@ -126,4 +149,19 @@ private function propertiesChoices(ReflectionClass $class): array

return array_combine($propertiesNames, $propertiesNames);
}

private function handleSingle(SymbolContext $context, array $arguments)
{
$newSource = $this->generateAccessor->generate(
SourceCode::fromStringAndPath($arguments[self::PARAM_SOURCE], $arguments[self::PARAM_PATH]),
$context->symbol()->name(),
$arguments[self::PARAM_OFFSET]
);

return UpdateFileSourceResponse::fromPathOldAndNewSource(
$newSource->path(),
$arguments[self::PARAM_SOURCE],
(string) $newSource
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Phpactor\Extension\Rpc\Response\UpdateFileSourceResponse;
use Phpactor\CodeTransform\Domain\Refactor\GenerateAccessor;
use Phpactor\Extension\CodeTransformExtra\Rpc\GenerateAccessorHandler;
use Phpactor\TestUtils\ExtractOffset;
use Phpactor\Tests\Unit\Extension\Rpc\HandlerTestCase;
use Phpactor\WorseReflection\ReflectorBuilder;

Expand Down Expand Up @@ -74,6 +75,32 @@ public function testSuggestsPossibleProperties()
$this->assertEquals(self::PROPERTIES_CHOICES, $input->choices());
}

public function testGeneratesAccessorIfSpecificPropertyIsSelected()
{
[ $source, $offset ] = ExtractOffset::fromSource(<<<'EOT'
<?php

class
{
private $<>foo;
}
EOT
);

$this->generateAccessor->generate($source, 'foo', $offset)
->willReturn(SourceCode::fromStringAndPath('new source', self::PATH))
->shouldBeCalledTimes(1);

$action = $this->handle(self::GENERATE_ACCESSOR_ACTION, [
'source' => $source,
'path' => self::PATH,
'offset' => $offset,
]);

/** @var InputCallbackResponse $action */
$this->assertInstanceOf(UpdateFileSourceResponse::class, $action);
}

public function testGenerateAccessorFromAPropertyName()
{
$oldSource = SourceCode::fromStringAndPath(self::SOURCE, self::PATH);
Expand Down