Skip to content

Commit

Permalink
Add extract variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alquerci committed Sep 27, 2022
1 parent 77ea02d commit e5263dc
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 6 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PHP Refactoring Toolbox for VIM
* Rename Method
* Extract Use
* Extract Const
* Extract Variable
* Extract Class Property
* Extract Method
* Create Property
Expand Down Expand Up @@ -79,6 +80,7 @@ let g:vim_php_refactoring_make_setter_fluent = 2
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
vnoremap <unique> <Leader>ev :call PhpExtractVariable()<CR>
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
Expand Down Expand Up @@ -250,6 +252,43 @@ class HelloWorld {
}
```

### Extract Variable

``` php
<?php

class HelloWorld {
private function prepareSentence($firstName)
{
$sentence = 'Hello';
if ('foo' === $firstName) {
$sentence .= ' ' . $firstName;
}
return $sentence;
}
}
```

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

``` php
<?php

class HelloWorld {
private function prepareSentence($firstName)
{
$sentence = 'Hello';
$firstNameIsValid = 'foo' === $firstName;

if ($firstNameIsValid) {
$sentence .= ' ' . $firstName;
}
return $sentence;
}
}
```

### Create Property

`<Leader>np` will create a new property in your current class.
Expand Down
48 changes: 42 additions & 6 deletions doc/refactoring-toolbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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>
vnoremap <unique> <Leader>ev :call PhpExtractVariable()<CR>
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
Expand All @@ -47,11 +48,12 @@ Examples *refactoring-to
4. Extract Use Statement........................................|extract-use-statement|
5. Extract Class Property.......................................|extract-class-property|
6. Extract Method...............................................|extract-method|
7. Create Property..............................................|create-property|
8. Detect Unused Use Statements.................................|detect-unused-use|
9. Align assignments............................................|align-assignments|
10. Create Setters and Getters..................................|create-set-get|
11. Document all................................................|document-all|
7. Extract Variable.............................................|extract-variable|
8. Create Property..............................................|create-property|
9. Detect Unused Use Statements.................................|detect-unused-use|
10. Align assignments...........................................|align-assignments|
11. Create Setters and Getters..................................|create-set-get|
12. Document all................................................|document-all|

Note: ↑ Is the position of your cursor

Expand Down Expand Up @@ -191,7 +193,41 @@ class HelloWorld {
}

===============================================================================
Create Property *create-property*
Extract Variable *extract-variable*

<?php

class HelloWorld {
private function prepareSentence($firstName)
{
$sentence = 'Hello';
if ('foo' === $firstName) {
$sentence .= ' ' . $firstName;
}
return $sentence;
}
}

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

<?php

class HelloWorld {
private function prepareSentence($firstName)
{
$sentence = 'Hello';
$firstNameIsValid = 'foo' === $firstName;

if ($firstNameIsValid) {
$sentence .= ' ' . $firstName;
}
return $sentence;
}
}

===============================================================================
Create Property *create-property*

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

Expand Down
13 changes: 13 additions & 0 deletions playground.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public function testExtractConst()
$string = 'FOOBAR';
}

/**
* Select the content you want to place in the content with the visual mode
* (you could use viw on int or va' on string)
* and then press <Leader>ev to create a variable and replace every occurrences of this
* by the variable usage
*/
public function testExtractVariable()
{
if (!$obj instanceof \Fully\Qualified\Classname) {
Throw new Exception('$obj is not a \Fully\Qualified\Classname');
}
}

/**
* Place your cursor on the "localVariableWanabeAClassVariable" variable
* and press <Leader>ep to promote this variable as class property
Expand Down
29 changes: 29 additions & 0 deletions plugin/php-refactoring-toolbox.vim
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if g:vim_php_refactoring_use_default_mapping == 1
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
vnoremap <unique> <Leader>ev :call PhpExtractVariable()<CR>
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
Expand Down Expand Up @@ -252,6 +253,34 @@ function! PhpExtractConst() " {{{
endfunction
" }}}

function! PhpExtractVariable() " {{{
if visualmode() != 'v'
call s:PhpEchoError('Extract variable only works in Visual mode, not in Visual Line or Visual block')
return
endif
let l:name = inputdialog("Name of new variable: ")
" go to select and copy and delete
normal! gvx
" add marker
normal! mr
" type variable name
exec 'normal! i$'.l:name
" go to start on selection
normal! `r
let l:indentChars = indent(line('.'))
" got to line to write assignment
normal! O
" type variable assignment
exec 'normal! i'.repeat(' ', l:indentChars).'$'.l:name.' = '
" paste selection
normal! pa;
" add empty line after assignment
normal! o
" go to start on selection
normal! `r
endfunction
" }}}

function! PhpExtractClassProperty() " {{{
normal! mr
let l:name = substitute(expand('<cword>'), '^\$*', '', '')
Expand Down

0 comments on commit e5263dc

Please sign in to comment.