forked from adoy/vim-php-refactoring-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactoring-toolbox.txt
executable file
·483 lines (385 loc) · 15 KB
/
refactoring-toolbox.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
*refactoring-toolbox*
__________ _____ __ .__ ___________ .__ ___.
\______ \ _____/ ____\____ _____/ |_ ___________|__| ____ ____ \__ ___/___ ____ | |\_ |__ _______ ___
| _// __ \ __\\__ \ _/ ___\ __\/ _ \_ __ \ |/ \ / ___\ | | / _ \ / _ \| | | __ \ / _ \ \/ /
| | \ ___/| | / __ \\ \___| | ( <_> ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > <
|____|_ /\___ >__| (____ /\___ >__| \____/|__| |__|___| /\___ / |____| \____/ \____/|____/___ /\____/__/\_ \
\/ \/ \/ \/ \//_____/ \/ \/
===============================================================================
CONTENTS *refactoring-toolbox-contents*
1. Intro................................|refactoring-toolbox-intro|
2. Configuration........................|refactoring-toolbox-configuration|
3. Mappings.............................|refactoring-toolbox-mappings|
4. Examples.............................|refactoring-toolbox-examples|
5. Playground...........................|refactoring-toolbox-playground|
===============================================================================
INTRO *refactoring-toolbox-intro*
Fork of `adoy/vim-php-refactoring-toolbox`
Big thanks to Pierrick Charron (github.com/adoy)
Refactoring ToolBox.
A set of mappings which help you to refactor code in consistent way across languages.
Supported languages:
- PHP
- Shell
- Extract Method
- JavaScript
- Extract Method
- Extract Variable
- Inline Variable
- Rename Variable
- TypeScript
- Extract Method
- Extract Variable
- Inline Variable
- Rename Variable
===============================================================================
CONFIGURATION *refactoring-toolbox-configuration*
If you want to disable the default mapping just add this line in your `~/.vimrc` file
```
let g:refactoring_toolbox_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:refactoring_toolbox_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:refactoring_toolbox_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:refactoring_toolbox_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:refactoring_toolbox_auto_validate_visibility = 1
```
To change the default visibility add one/both of those lines in your `~/.vimrc` file
```
let g:refactoring_toolbox_default_property_visibility = 'private'
let g:refactoring_toolbox_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:refactoring_toolbox_make_setter_fluent = 1
" to enable but be prompted when creating the setter
let g:refactoring_toolbox_make_setter_fluent = 2
```
If you want to rename directory with mapping `rd` you need to install phpactor/phpactor
===============================================================================
Mappings *refactoring-toolbox-mappings*
Global
nmap <Leader>rd <Plug>refactoring_toolbox_RenameDirectory
vmap <Leader>ec :call PhpExtractConst()<Enter>
nmap <Leader>eu :call PhpExtractUse()<Enter>
nmap <Leader>np :call PhpCreateProperty()<Enter>
nmap <Leader>du :call PhpDetectUnusedUseStatements()<Enter>
vmap <Leader>== :call PhpAlignAssigns()<Enter>
nmap <Leader>da :call PhpDocAll()<Enter>
PHP filetype
vmap <LocalLeader>em <Plug>refactoring_toolbox_php_ExtractMethod
nmap <LocalLeader>rm <Plug>refactoring_toolbox_php_RenameMethod
vmap <LocalLeader>ev <Plug>refactoring_toolbox_php_ExtractVariable
nmap <LocalLeader>rv <Plug>refactoring_toolbox_php_RenameVariable
nmap <LocalLeader>rlv <Plug>refactoring_toolbox_php_RenameLocalVariable
nmap <LocalLeader>iv <Plug>refactoring_toolbox_php_InlineVariable
nmap <LocalLeader>ep <Plug>refactoring_toolbox_php_ExtractProperty
nmap <LocalLeader>rp <Plug>refactoring_toolbox_php_RenameProperty
nmap <LocalLeader>rcv <Plug>refactoring_toolbox_php_RenameClassVariable
nmap <LocalLeader>sg <Plug>refactoring_toolbox_php_SettersAndGetters
nmap <LocalLeader>cog <Plug>refactoring_toolbox_php_OnlyGetters
vmap <LocalLeader>nc <Plug>refactoring_toolbox_php_NewClass
===============================================================================
Examples *refactoring-toolbox-examples*
1. Rename Local Variable........................................|rename-local-variable|
2. Rename Class Variable........................................|rename-class-variable|
3. Rename Method................................................|rename-method|
4. Extract Use Statement........................................|extract-use-statement|
5. Extract Class Property.......................................|extract-class-property|
6. Extract Method...............................................|extract-method|
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
===============================================================================
Rename Local Variable *rename-local-variable*
<?php
function helloWorld($foobar = null) {
echo "Hello " . $foobar;
} ↑
<LocalLeader>rlv in normal mode, specify the new $name
<?php
function helloWorld($name = null) {
echo "Hello " . $name;
} ↑
===============================================================================
Rename Class Variable *rename-class-variable*
<?php
class HelloWorld {
private $foobar;
public function __construct($name) {
$this->foobar = $name;
}
public function sayHello() {
echo $this->foobar;
} ↑
}
<LocalLeader>rcv in normal mode, specify the new $name
<?php
class HelloWorld {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function sayHello() {
echo $this->name;
}
}
===============================================================================
Rename method *rename-method*
<?php
class HelloWorld {
public function sayHello() {
echo $this->sayHello();
} ↑
}
<LocalLeader>rm in normal mode, specify the new method name
<?php
class HelloWorld {
public function newMethodName() {
echo $this->newMethodName();
} ↑
}
===============================================================================
Extract Use Statement *extract-use-statement*
<?php
$obj1 = new Foo\Bar\Baz;
$obj2 = new Foo\Bar\Baz;
↑
<Leader>eu in normal mode
<?php
use Foo\Bar\Baz;
$obj1 = Baz;
$obj2 = Baz;
===============================================================================
Extract Class Property *extract-class-property*
<?php
class Dir {
public function __construct($path) {
$realpath = $path;
} ↑
}
<Leader>ep in normal mode will extract the local variable and create a property inside the current class.
<?php
class Dir {
private $realpath;
public function __construct($path) {
$this->realpath = $path;
} ↑
}
===============================================================================
Extract Method *extract-method*
<?php
class HelloWorld {
public function sayHello($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 <LocalLeader>em. You'll be prompted for a method name. Enter a method name and press enter
<?php
class HelloWorld {
public function sayHello($firstName = null) {
$sentence = $this->prepareSentence($firstName);
echo $sentence;
}
private function prepareSentence($firstName)
{
$sentence = 'Hello';
if ($firstName) {
$sentence .= ' ' . $firstName;
}
return $sentence;
}
}
===============================================================================
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 `<LocalLeader>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.
===============================================================================
Detect unused "use" statements *detect-unused-use*
<Leader>du will detect all unused "use" statements in your code so that you can remove them.
===============================================================================
Align assignments *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 *create-set-get*
<?php
class Foo {
private $bar;
}
Hit `<LocalLeader>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.
<?php
class Foo {
private $bar;
public function setBar($bar)
{
$this->bar = $bar;
}
public function getBar()
{
return $this->bar;
}
}
===============================================================================
Document all *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.
===============================================================================
Playground *refactoring-toolbox-playground*
Here you have some code within you can try some of the available tools:
<?php
namespace AdoY\PHP\Refactoring\Toolbox;
use I\Am\Really\Useless as NobodyLovesMe;
use I\Am\Usefull as Lover;
class Playground
{
private $renameMe = 10;
/**
* Place your cursor on a local variable and press <LocalLeader>rlv
* to rename a function local variable
*/
public function testRenameLocalVariable($renameMe)
{
$renameMe = 'renameMe will be renamed';
$renameMeAlso = $renameMe;
$this->renameMe = 'If will be renamed in the next test';
}
/**
* Place your cursor on a class variable and press <LocalLeader>rcv
* to rename a property (class variable)
*/
public function testRenameClassVariable($renameMe)
{
$this->renameMe = 'RenameMe rename every usage of this property in the current class';
$renameMe = 'I\'m not renamed';
}
/**
* Place your cursor on a method name and press <LocalLeader>rm
* to rename a method
*/
public function testRenameMethod()
{
$this->testRenameMethod();
}
/**
* Place your cursor on a Fully qualified class name and press <Leader>eu
* to create an alias and place the new Use statement on top of the file
*/
public function testExtractUse(\Fully\Qualified\Classname $obj)
{
if (!$obj instanceof \Fully\Qualified\Classname) {
Throw new Exception('$obj is not a \Fully\Qualified\Classname');
}
return new \Fully\Qualified\AnOtherClassname;
}
/**
* 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>ec to create a constant and replace every occurence of this
* by the constant usage
*/
public function testExtractConst()
{
$dix = 1001;
$string = 'FOOBAR';
}
/**
* Place your cursor on the "localVariableWanabeAClassVariable" variable
* and press <Leader>ep to promote this variable as class property
*/
public function testExtractClassProperty($newval)
{
$localVariableWanabeAClassVariable = $newval;
}
/**
* Select different block of code and extract it to different methods using
* <LocalLeader>em
*/
public function testExtractMethod($message)
{
// Make a very cool wave with the message
for ($i = 0; $i < strlen($message); $i++) {
$message[$i] = $i % 2 ? strtoupper($message[$i]) : strtolower($message[$i]);
}
// Put the message in a fancy box
$borderTopAndBottom = '+' . str_repeat('=', strlen($message)+2) . '+';
$newMessage = $borderTopAndBottom . PHP_EOL;
$newMessage .= '| ' . $message . ' |' . PHP_EOL;
$newMessage .= $borderTopAndBottom . PHP_EOL;
return $newMessage;
}
/**
* Press <Leader>np to create a property
*/
public function testCreateNewProperty()
{
$this->notCreated;
}
/**
* Press <Leader>du to detect unused use statements
*/
public function testDetectUnusedStatements()
{
new Lover;
}
/**
* Select the inner function block
* and press <Leader>== to align all assignements
*/
public function testAlignAssigns()
{
$oneVar = 'Foo';
$anOtherVar = 'Bar';
$oneVar += 'Baz';
}
}