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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# docs
/.couscous

# php-cs-fixer
/.php_cs.cache
./.php_cs
5 changes: 5 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
$finder = PhpCsFixer\Finder::create()
->in('lib')
->in('tests')
->exclude([
'Assets/Cache',
'Assets/Projects',
'Assets/Workspace',
])
;

return PhpCsFixer\Config::create()
Expand Down
20 changes: 12 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ Changelog
Features:

- [vim-plugin] Add new `GotoDefinition[Vsplit|Hsplit|Tab]` functions.
- [code-builder] Initial support for nullable types. Thanks @einenlum!
- [vim-plugin] FZF integration for list inputs (#769). Thanks @elythyr!
- [vim-plugin] FZF multiple selection (#773). Thanks @elythyr!
- [vim-plugin] Maintain correct cursor position after certain text diffs (#770), Thanks @elythyr!
- [code-tranform] Generate static methods if the call was static (#25).
@einenlum
- [code-builder] Initial support for nullable types - @einenlum
- [vim-plugin] FZF integration for list inputs (#769) - @elythyr
- [vim-plugin] FZF multiple selection (#773). @elythyr
- [vim-plugin] Maintain correct cursor position after certain text diffs (#770) - @elythyr
- [code-transform|rpc] Generate multiple accessors for a class - @elythyr
- [code-tranform] Generate static methods if the call was static (#25) - @einenlum

Bug fixes:

- [completion] Signature helper does not work on interfaces (#752). Thanks
@taluu!
- [completion] Signature helper does not work on interfaces (#752) - @taluu
- [code-builder] Class import doesn't work with single element namespace
#760
- [code-builder] Variant is not passed to class generator (#766)

BC Break:

- [code-transform] Generate accessors is now a class action and allows
generation of multiple accessors.

## 2019-03-03 0.12.0

BC Break:
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.

18 changes: 9 additions & 9 deletions doc/refactorings.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Fixes:
- [Add Missing Assignments](#add-missing-assignments)
- [Complete Constructor](#complete-constructor)
- [Fix namespace and/or class name](#fix-namespace-or-class-name)
- [Generate Accessor](#generate-accessor)
- [Generate Accessors](#generate-accessors)
- [Generate Method](#generate-method)
- [Implement Contracts](#implement-contracts)
- [Import Class](#import-class)
Expand Down Expand Up @@ -669,14 +669,14 @@ interface FoobarInterface
}
```

Generate Accessor
-----------------
Generate Accessors
------------------

Generate an accessor for a class property.
Generate accessors for a class.

- **Command**: _RPC only_
- **VIM plugin**: _Property context menu > Generate accessor_.
- **VIM function**:`:call phpactor#ContextMenu()`
- **VIM plugin**: _Class context menu > Generate accessor_.
- **VIM function**:`:call phpactor#GenerateAccessor()`

### Motivation

Expand All @@ -691,16 +691,16 @@ Cursor position shown as `<>`:
```php
<?php

class Foobar
class Foo<>bar
{
/**
* @var Barfoo
*/
private $bar<>foo;
private $barfoo;
}
```

After:
After selecting [one or more accessors](/vim-plugin.html#fzf-multi-selection)

```php
<?php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ private function registerRpc(ContainerBuilder $container)

$container->register('code_transform.rpc.handler.generate_accessor', function (Container $container) {
return new GenerateAccessorHandler(
$container->get(WorseReflectionExtension::SERVICE_REFLECTOR),
$container->get('code_transform.refactor.generate_accessor')
);
}, [ RpcExtension::TAG_RPC_HANDLER => ['name' => GenerateAccessorHandler::NAME] ]);
Expand Down
92 changes: 81 additions & 11 deletions lib/Extension/CodeTransformExtra/Rpc/GenerateAccessorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,40 @@

namespace Phpactor\Extension\CodeTransformExtra\Rpc;

use InvalidArgumentException;
use Phpactor\CodeTransform\Domain\Refactor\GenerateAccessor;
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\Reflection\ReflectionClass;
use Phpactor\WorseReflection\Core\Reflection\ReflectionProperty;
use Phpactor\WorseReflection\Reflector;

class GenerateAccessorHandler extends AbstractHandler
{
const NAME = 'generate_accessor';
const PARAM_OFFSET = 'offset';
const PARAM_NAMES = 'names';
const PARAM_SOURCE = 'source';
const PARAM_PATH = 'path';
const PARAM_OFFSET = 'offset';

/**
* @var Reflector
*/
private $reflector;

/**
* @var GenerateAccessor
*/
private $generateAccessor;

public function __construct(
Reflector $reflector,
GenerateAccessor $generateAccessor
) {
$this->reflector = $reflector;
$this->generateAccessor = $generateAccessor;
}

Expand All @@ -33,6 +46,9 @@ public function name(): string

public function configure(Resolver $resolver)
{
$resolver->setDefaults([
self::PARAM_NAMES => null,
]);
$resolver->setRequired([
self::PARAM_PATH,
self::PARAM_SOURCE,
Expand All @@ -42,18 +58,72 @@ public function configure(Resolver $resolver)

public function handle(array $arguments)
{
$sourceCode = $this->generateAccessor->generateAccessor(
SourceCode::fromStringAndPath(
$arguments[self::PARAM_SOURCE],
$arguments[self::PARAM_PATH]
),
$arguments[self::PARAM_OFFSET]
);
$class = $this->class($arguments[self::PARAM_SOURCE]);

$this->requireInput(ListInput::fromNameLabelChoices(
self::PARAM_NAMES,
sprintf('Properties from "%s"', $class->name()),
$this->propertiesChoices($class)
)->withMultiple(true));

if ($this->hasMissingArguments($arguments)) {
return $this->createInputCallback($arguments);
}

$originalSource = $arguments[self::PARAM_SOURCE];
$newSource = SourceCode::fromStringAndPath($originalSource, $arguments[self::PARAM_PATH]);

foreach ((array) $arguments[self::PARAM_NAMES] as $propertyName) {
$newSource = $this->generateAccessor->generate(
$newSource,
$propertyName,
$arguments[self::PARAM_OFFSET]
);
}

return UpdateFileSourceResponse::fromPathOldAndNewSource(
$sourceCode->path(),
$arguments[self::PARAM_SOURCE],
(string) $sourceCode
$newSource->path(),
$originalSource,
(string) $newSource
);
}

/**
* @param string $source
*
* @return ReflectionClass
*/
private function class($source)
{
$classes = $this->reflector->reflectClassesIn($source);

if ($classes->count() === 0) {
throw new InvalidArgumentException(
'No classes in source file'
);
}

if ($classes->count() > 1) {
throw new InvalidArgumentException(
'Currently will only generates accessor by name in files with one class'
);
}

return $classes->first();
}

private function propertiesChoices(ReflectionClass $class): array
{
// Select only those from the current class because the accessor generator
// is not able to work with the parent class at the time
$properties = $class->properties()->belongingTo($class->name());

$propertiesNames = array_map(function (ReflectionProperty $property) {
return $property->name();
}, iterator_to_array($properties));

natsort($propertiesNames);

return array_combine($propertiesNames, $propertiesNames);
}
}
13 changes: 11 additions & 2 deletions lib/Extension/ContextMenu/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
"offset": "%offset%",
"source": "%source%"
}
},
"generate_accessor": {
"action": "generate_accessor",
"parameters": {
"path": "%path%",
"source": "%source%",
"offset": "%offset%"
}
}
},
"property": {
Expand All @@ -187,8 +195,9 @@
"action": "generate_accessor",
"parameters": {
"path": "%path%",
"offset": "%offset%",
"source": "%source%"
"names": "%symbol%",
"source": "%source%",
"offset": "%offset%"
}
},
"goto_definition": {
Expand Down
6 changes: 5 additions & 1 deletion plugin/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ function! phpactor#GetClassFullName()
endfunction

function! phpactor#ChangeVisibility()
call phpactor#rpc("change_visibility", { "offset": phpactor#_offset(), "source": phpactor#_source(), "path": expand('%:p')})
call phpactor#rpc("change_visibility", { "offset": phpactor#_offset(), "source": phpactor#_source(), "path": expand('%:p') })
endfunction

function! phpactor#GenerateAccessors()
call phpactor#rpc("generate_accessor", { "source": phpactor#_source(), "path": expand('%:p'), 'offset': phpactor#_offset() })
endfunction

"""""""""""""""""""""""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function testComplete(string $source, array $expected)
return $one['name'] <=> $two['name'];
});

if (!$expected) {
$this->assertEmpty($suggestions);
}

foreach ($expected as $index => $expectedSuggestion) {
$this->assertArraySubset($expectedSuggestion, $suggestions[$index]);
}
Expand Down
Loading