Skip to content

Commit e5263dc

Browse files
committed
Add extract variable
1 parent 77ea02d commit e5263dc

File tree

4 files changed

+123
-6
lines changed

4 files changed

+123
-6
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PHP Refactoring Toolbox for VIM
99
* Rename Method
1010
* Extract Use
1111
* Extract Const
12+
* Extract Variable
1213
* Extract Class Property
1314
* Extract Method
1415
* Create Property
@@ -79,6 +80,7 @@ let g:vim_php_refactoring_make_setter_fluent = 2
7980
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
8081
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
8182
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
83+
vnoremap <unique> <Leader>ev :call PhpExtractVariable()<CR>
8284
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
8385
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
8486
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
@@ -250,6 +252,43 @@ class HelloWorld {
250252
}
251253
```
252254

255+
### Extract Variable
256+
257+
``` php
258+
<?php
259+
260+
class HelloWorld {
261+
private function prepareSentence($firstName)
262+
{
263+
$sentence = 'Hello';
264+
if ('foo' === $firstName) {
265+
$sentence .= ' ' . $firstName;
266+
}
267+
return $sentence;
268+
}
269+
}
270+
```
271+
272+
Select in visual mode (V) the code you want to extract in a variable and hit `<Leader>ev`.
273+
You'll be prompted for a variable name. Enter a variable name and press enter
274+
275+
``` php
276+
<?php
277+
278+
class HelloWorld {
279+
private function prepareSentence($firstName)
280+
{
281+
$sentence = 'Hello';
282+
$firstNameIsValid = 'foo' === $firstName;
283+
284+
if ($firstNameIsValid) {
285+
$sentence .= ' ' . $firstName;
286+
}
287+
return $sentence;
288+
}
289+
}
290+
```
291+
253292
### Create Property
254293

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

doc/refactoring-toolbox.txt

+42-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ nnoremap <unique> <Leader>rcv :call PhpRenameClassVariable()<CR>
3030
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
3131
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
3232
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
33+
vnoremap <unique> <Leader>ev :call PhpExtractVariable()<CR>
3334
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
3435
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
3536
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
@@ -47,11 +48,12 @@ Examples *refactoring-to
4748
4. Extract Use Statement........................................|extract-use-statement|
4849
5. Extract Class Property.......................................|extract-class-property|
4950
6. Extract Method...............................................|extract-method|
50-
7. Create Property..............................................|create-property|
51-
8. Detect Unused Use Statements.................................|detect-unused-use|
52-
9. Align assignments............................................|align-assignments|
53-
10. Create Setters and Getters..................................|create-set-get|
54-
11. Document all................................................|document-all|
51+
7. Extract Variable.............................................|extract-variable|
52+
8. Create Property..............................................|create-property|
53+
9. Detect Unused Use Statements.................................|detect-unused-use|
54+
10. Align assignments...........................................|align-assignments|
55+
11. Create Setters and Getters..................................|create-set-get|
56+
12. Document all................................................|document-all|
5557

5658
Note: ↑ Is the position of your cursor
5759

@@ -191,7 +193,41 @@ class HelloWorld {
191193
}
192194

193195
===============================================================================
194-
Create Property *create-property*
196+
Extract Variable *extract-variable*
197+
198+
<?php
199+
200+
class HelloWorld {
201+
private function prepareSentence($firstName)
202+
{
203+
$sentence = 'Hello';
204+
if ('foo' === $firstName) {
205+
$sentence .= ' ' . $firstName;
206+
}
207+
return $sentence;
208+
}
209+
}
210+
211+
Select in visual mode (V) the code you want to extract in a variable and hit `<Leader>ev`.
212+
You'll be prompted for a variable name. Enter a variable name and press enter
213+
214+
<?php
215+
216+
class HelloWorld {
217+
private function prepareSentence($firstName)
218+
{
219+
$sentence = 'Hello';
220+
$firstNameIsValid = 'foo' === $firstName;
221+
222+
if ($firstNameIsValid) {
223+
$sentence .= ' ' . $firstName;
224+
}
225+
return $sentence;
226+
}
227+
}
228+
229+
===============================================================================
230+
Create Property *create-property*
195231

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

playground.php

+13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ public function testExtractConst()
6363
$string = 'FOOBAR';
6464
}
6565

66+
/**
67+
* Select the content you want to place in the content with the visual mode
68+
* (you could use viw on int or va' on string)
69+
* and then press <Leader>ev to create a variable and replace every occurrences of this
70+
* by the variable usage
71+
*/
72+
public function testExtractVariable()
73+
{
74+
if (!$obj instanceof \Fully\Qualified\Classname) {
75+
Throw new Exception('$obj is not a \Fully\Qualified\Classname');
76+
}
77+
}
78+
6679
/**
6780
* Place your cursor on the "localVariableWanabeAClassVariable" variable
6881
* and press <Leader>ep to promote this variable as class property

plugin/php-refactoring-toolbox.vim

+29
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ if g:vim_php_refactoring_use_default_mapping == 1
6262
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
6363
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
6464
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
65+
vnoremap <unique> <Leader>ev :call PhpExtractVariable()<CR>
6566
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
6667
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
6768
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
@@ -252,6 +253,34 @@ function! PhpExtractConst() " {{{
252253
endfunction
253254
" }}}
254255

256+
function! PhpExtractVariable() " {{{
257+
if visualmode() != 'v'
258+
call s:PhpEchoError('Extract variable only works in Visual mode, not in Visual Line or Visual block')
259+
return
260+
endif
261+
let l:name = inputdialog("Name of new variable: ")
262+
" go to select and copy and delete
263+
normal! gvx
264+
" add marker
265+
normal! mr
266+
" type variable name
267+
exec 'normal! i$'.l:name
268+
" go to start on selection
269+
normal! `r
270+
let l:indentChars = indent(line('.'))
271+
" got to line to write assignment
272+
normal! O
273+
" type variable assignment
274+
exec 'normal! i'.repeat(' ', l:indentChars).'$'.l:name.' = '
275+
" paste selection
276+
normal! pa;
277+
" add empty line after assignment
278+
normal! o
279+
" go to start on selection
280+
normal! `r
281+
endfunction
282+
" }}}
283+
255284
function! PhpExtractClassProperty() " {{{
256285
normal! mr
257286
let l:name = substitute(expand('<cword>'), '^\$*', '', '')

0 commit comments

Comments
 (0)