Skip to content

Latest commit

History

65 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

VIM Php Refactoring Toolbox

License: MIT

PHP Refactoring Toolbox for VIM

  • Rename Local Variable
  • Rename Class Variable
  • Rename Method
  • Extract Use
  • Extract Const
  • Extract Class Property
  • Extract Method
  • Create Property
  • Detect Unused Use Statements
  • Align Assigns
  • Create setters and getters
  • Document all code

Installation

  • vim-plug: Plug 'adoy/vim-php-refactoring-toolbox'
  • vundle: Plugin 'adoy/vim-php-refactoring-toolbox'
  • pathogen: git clone https://github.com/adoy/vim-php-refactoring-toolbox.git ~/.vim/bundle/
  • or just copy the plugin/php-refactoring-toolbox.vim in your ~/.vim/plugin folder

If you want to disable the default mapping just add this line in your ~/.vimrc file

let g:vim_php_refactoring_use_default_mapping = 0

If you want to disable the user validation at the getter/setter creation, just add this line in your ~/.vimrc file

let g:vim_php_refactoring_auto_validate_sg = 1

If you want to disable the user validation at getter only creation, just add this line in your ~/.vimrc file

let g:vim_php_refactoring_auto_validate_g = 1

If you want to disable the user validation for all rename features, just add this line in your ~/.vimrc file

let g:vim_php_refactoring_auto_validate_rename = 1

If you want to disable the user validation for the visibility (private/public) add this line in your ~/.vimrc file

let g:vim_php_refactoring_auto_validate_visibility = 1

To change the default visibility add one/both of those lines in your ~/.vimrc file

let g:vim_php_refactoring_default_property_visibility = 'private'
let g:vim_php_refactoring_default_method_visibility = 'private'

To enable fluent setters add either of these lines to your ~/.vimrc file

" default is 0 -- disabled
" to enable for all setters
let g:vim_php_refactoring_make_setter_fluent = 1
" to enable but be prompted when creating the setter
let g:vim_php_refactoring_make_setter_fluent = 2

Default Mappings

nnoremap <unique> <Leader>rlv :call PhpRenameLocalVariable()<CR>
nnoremap <unique> <Leader>rcv :call PhpRenameClassVariable()<CR>
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
nnoremap <unique> <Leader>du :call PhpDetectUnusedUseStatements()<CR>
vnoremap <unique> <Leader>== :call PhpAlignAssigns()<CR>
nnoremap <unique> <Leader>sg :call PhpCreateSettersAndGetters()<CR>
nnoremap <unique> <Leader>cog :call PhpCreateGetters()<CR>
nnoremap <unique> <Leader>da :call PhpDocAll()<CR>

Playground.php

You'll find in this project a playground.php file. You can use this file to start playing with this refactoring plugin.

Examples

↑ Is the position of your cursor

Rename Local Variable

<?phpfunctionhelloWorld($foobar = null) {
echo"Hello " . $foobar;
} ↑

<Leader>rlv in normal mode, specify the new $name

<?phpfunctionhelloWorld($name = null) {
echo"Hello " . $name;
} ↑

Rename Class Variable

<?phpclass HelloWorld {
private$foobar;
publicfunction__construct($name) {
$this->foobar = $name;
}
publicfunctionsayHello() {
echo$this->foobar;
} ↑
}

<Leader>rcv in normal mode, specify the new $name

<?phpclass HelloWorld {
private$name;
publicfunction__construct($name) {
$this->name = $name;
}
publicfunctionsayHello() {
echo$this->name;
}
}

Rename method

<?phpclass HelloWorld {
publicfunctionsayHello() {
echo$this->sayHello();
} ↑
}

<Leader>rm in normal mode, specify the new method name

<?phpclass HelloWorld {
publicfunctionnewMethodName() {
echo$this->newMethodName();
} ↑
}

Extract Use Statement

<?php$obj1 = newFoo\Bar\Baz;
$obj2 = newFoo\Bar\Baz;
↑

<Leader>eu in normal mode

<?phpuseFoo\Bar\Baz;
$obj1 = Baz;
$obj2 = Baz;

Extract Class Property

<?phpclass Dir {
publicfunction__construct($path) {
$realpath = $path;
} ↑
}

<Leader>ep in normal mode will extract the local variable and create a property inside the current class.

<?phpclass Dir {
private$realpath;
publicfunction__construct($path) {
$this->realpath = $path;
} ↑
}

Extract Method

<?phpclass HelloWorld {
publicfunctionsayHello($firstName = null) {
$sentence = 'Hello';
if ($firstName) {
$sentence .= '' . $firstName;
}
echo$sentence;
}
}

Select in visual mode (V) the code you want to extract in an other method and hit <Leader>em. You'll be prompted for a method name. Enter a method name and press enter

<?phpclass HelloWorld {
publicfunctionsayHello($firstName = null) {
$sentence = $this->prepareSentence($firstName);
echo$sentence;
}
privatefunctionprepareSentence($firstName)
{
$sentence = 'Hello';
if ($firstName) {
$sentence .= '' . $firstName;
}
return$sentence;
}
}

Create Property

<Leader>np will create a new property in your current class.

Detect unused "use" statements

<Leader>du will detect all unused "use" statements in your code so that you can remove them.

Align assignments

<?php$oneVar = 'Foo';
$anOtherVar = 'Bar';
$oneVar += 'Baz';

Select the code you want to align and then hit <Leader>==

<?php$oneVar = 'Foo';
$anOtherVar = 'Bar';
$oneVar += 'Baz';

Create setters and getters

<?phpclass Foo {
private$bar;
}

Hit <Leader>sg and you'll be prompted if you want to create setters and getters for existing properties and if you want to make the setter fluent.

<?phpclass Foo {
private$bar;
publicfunctionsetBar($bar)
{
$this->bar = $bar;
return$this; // If you opted for a fluent setter at the prompt.
}
publicfunctiongetBar()
{
return$this->bar;
}
}

Create only getters

<?phpclass Foo {
private$bar;
}

Hit <Leader>cog and you will be prompted if you want only getters for existing properties

<?phpclass Foo {
private$bar;
publicfunctiongetBar()
{
return$this->bar;
}
}

Document all

<Leader>da will call your documentation plugin (by default Php Documentor for vim https://github.com/tobyS/pdv) for every uncommented classes, methods, functions and properties.

About

VIM Php Refactoring Toolbox

Topics

Resources

Stars

182 stars

Watchers

12 watching

Forks

Releases

Packages

Contributors

Languages