Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extract variable #36

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e5263dc
Add extract variable
alquerci Sep 27, 2022
5804b8b
fixup! Add extract variable
alquerci Sep 28, 2022
29cb21b
Add test with vader
alquerci Sep 28, 2022
73d4f01
fixup! Add extract variable
alquerci Sep 30, 2022
729840d
fixup! Add extract variable
alquerci Sep 30, 2022
7ebf157
fixup! Add extract variable
alquerci Sep 30, 2022
c54ad48
fixup! Add extract variable
alquerci Sep 30, 2022
2529304
fixup! Add extract variable
alquerci Sep 30, 2022
53a2afd
fixup! Add extract variable
alquerci Sep 30, 2022
ae701a4
fixup! Add extract variable
alquerci Sep 30, 2022
3e416c4
fixup! Add extract variable
alquerci Sep 30, 2022
4db7114
fixup! Add extract variable
alquerci Sep 30, 2022
2943869
fixup! Add extract variable
alquerci Sep 30, 2022
8455984
fixup! Add extract variable
alquerci Sep 30, 2022
fe107eb
fixup! Add extract variable
alquerci Sep 30, 2022
6f1e39e
fixup! Add extract variable
alquerci Oct 1, 2022
cbde6a2
fixup! Add extract variable
alquerci Oct 1, 2022
e335391
fixup! Add extract variable
alquerci Oct 1, 2022
44cf732
fixup! Add extract variable
alquerci Oct 1, 2022
c12fd45
fixup! Add extract variable
alquerci Oct 1, 2022
6723e0f
fixup! Add extract variable
alquerci Oct 1, 2022
501ac68
fixup! Add extract variable
alquerci Oct 1, 2022
cac41b8
fixup! Add extract variable
alquerci Oct 1, 2022
eeb5895
fixup! Add extract variable
alquerci Oct 1, 2022
c945369
fixup! Add extract variable
alquerci Oct 1, 2022
d0944a2
fixup! Add extract variable
alquerci Oct 1, 2022
fbcb4be
fixup! Add extract variable
alquerci Oct 1, 2022
331a6ee
fixup! Add extract variable
alquerci Oct 1, 2022
6ecef64
fixup! Add extract variable
alquerci Oct 1, 2022
8935547
fixup! Add extract variable
alquerci Oct 1, 2022
db5e97a
fixup! Add extract variable
alquerci Oct 1, 2022
70c20d6
fixup! Add extract variable
alquerci Oct 1, 2022
cc7c293
fixup! Add extract variable
alquerci Oct 1, 2022
d616354
fixup! Add extract variable
alquerci Oct 1, 2022
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
46 changes: 46 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,41 @@ 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';
$shouldDisplayName = 'foo' === $firstName;

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

### Create Property

`<Leader>np` will create a new property in your current class.
Expand Down Expand Up @@ -340,3 +377,12 @@ class Foo {
`<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.


## Running tests

```
bin/test
```

### How to write tests?

See https://github.com/junegunn/vader.vim
31 changes: 31 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /bin/sh -eu

installVader ()
{
vaderdir='vendor/vader.vim'
vaderCommit='6fff477431ac3191c69a3a5e5f187925466e275a'

test -d "$vaderdir" || {
{
git clone \
--branch=master \
--single-branch \
https://github.com/junegunn/vader.vim.git \
"$vaderdir"

git \
--work-tree="$vaderdir" \
--git-dir="$vaderdir/.git" \
reset --hard \
"$vaderCommit" --
} || {
rm -rf "$vaderdir"

exit 1
}
}
}

installVader

vim -esNu test/fixtures/vimrc -c 'Vader! test/**'
46 changes: 40 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,39 @@ 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';
$shouldDisplayName = 'foo' === $firstName;

if ($shouldDisplayName) {
$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
83 changes: 83 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,88 @@ 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

" input
let l:name = inputdialog('Name of new variable: ')
let l:defaultUpwardMove = 1
let l:lineUpwardForAssignment = inputdialog('Line upward for assignment (default is '.l:defaultUpwardMove.'): ')
if empty(l:lineUpwardForAssignment)
let l:lineUpwardForAssignment = l:defaultUpwardMove
endif

" 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:startLine = line('.')
let l:startCol = col('.')

" go to line to write assignment
call cursor(line('.') - l:lineUpwardForAssignment, 0)
let l:indentChars = indent(nextnonblank(line('.') + 1))
let l:needBlankLineAfter = v:false

" line ends with ,
while ',' == trim(getline(line('.')))[-1:]
" backward one line
call cursor(line('.') - 1, 0)
endwhile

" line ends with [
if '[' == trim(getline(line('.')))[-1:]
" backward one line
call cursor(line('.') - 1, 0)
endif

if empty(trim(getline(line('.'))))
let l:currentLine = line('.')
let l:currentCol = col('.')

call cursor(nextnonblank(l:currentLine), 0)
let l:indentChars = indent(line('.'))

call cursor(prevnonblank(l:currentLine), l:currentCol)

let l:lineUpwardForAssignment = l:currentLine - l:startLine
endif

if 1 == l:lineUpwardForAssignment
let l:needBlankLineAfter = v:true
endif

" type variable assignment
let l:prefixAssign = repeat(' ', l:indentChars).'$'.l:name.' = '
call append(line('.'), l:prefixAssign)

" move cursor at the after the equal sign
call cursor(line('.') + 1, 0)
normal! $

" paste selection and add semi-colon
normal! pa;

if l:needBlankLineAfter
call append(line('.'), '')
endif

" go to start on selection
normal! `r
endfunction
" }}}

function! PhpExtractClassProperty() " {{{
normal! mr
let l:name = substitute(expand('<cword>'), '^\$*', '', '')
Expand Down
25 changes: 25 additions & 0 deletions test/extract_variable/from_array_definition/first_element.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Given php:
<?php

$sentence = 'Hello';

$foo = [
'bar' => 1234,
];

Do:
/1234\<CR>
viw
;ev
baz\<CR>
\<CR>

Expect:
<?php

$sentence = 'Hello';
$baz = 1234;

$foo = [
'bar' => $baz,
];
27 changes: 27 additions & 0 deletions test/extract_variable/from_array_definition/second_element.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Given php:
<?php

$sentence = 'Hello';

$foo = [
'boz' => 42,
'bar' => 1234,
];

Do:
/1234\<CR>
viw
;ev
baz\<CR>
\<CR>

Expect:
<?php

$sentence = 'Hello';
$baz = 1234;

$foo = [
'boz' => 42,
'bar' => $baz,
];
29 changes: 29 additions & 0 deletions test/extract_variable/from_array_definition/third_element.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Given php:
<?php

$sentence = 'Hello';

$foo = [
'boz' => 42,
'foo' => 12,
'bar' => 1234,
];

Do:
/1234\<CR>
viw
;ev
baz\<CR>
\<CR>

Expect:
<?php

$sentence = 'Hello';
$baz = 1234;

$foo = [
'boz' => 42,
'foo' => 12,
'bar' => $baz,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Given php:
<?php

$sentence = 'Hello';

$foo = [
'bar' => 1234,
];

Do:
/1234\<CR>
viw
;ev
baz\<CR>
3\<CR>

Expect:
<?php

$sentence = 'Hello';
$baz = 1234;

$foo = [
'bar' => $baz,
];
Loading