Skip to content

Commit 257c78a

Browse files
committed
Merge pull request #15 from vivoconunxino/master
Adding documentation. Thanks for this contribution :-)
2 parents 6fa868a + 12586b0 commit 257c78a

File tree

1 file changed

+367
-0
lines changed

1 file changed

+367
-0
lines changed

doc/refactoring-toolbox.txt

Lines changed: 367 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,367 @@
1+
*refactoring-toolbox*
2+
3+
__________ _____ __ .__ ___________ .__ ___.
4+
\______ \ _____/ ____\____ _____/ |_ ___________|__| ____ ____ \__ ___/___ ____ | |\_ |__ _______ ___
5+
| _// __ \ __\\__ \ _/ ___\ __\/ _ \_ __ \ |/ \ / ___\ | | / _ \ / _ \| | | __ \ / _ \ \/ /
6+
| | \ ___/| | / __ \\ \___| | ( <_> ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > <
7+
|____|_ /\___ >__| (____ /\___ >__| \____/|__| |__|___| /\___ / |____| \____/ \____/|____/___ /\____/__/\_ \
8+
\/ \/ \/ \/ \//_____/ \/ \/
9+
10+
===============================================================================
11+
CONTENTS *refactoring-toolbox-contents*
12+
13+
1. Intro........................................|refactoring-toolbox-intro|
14+
2. Mappings.....................................|refactoring-toolbox-mappings|
15+
2. Examples.....................................|refactoring-toolbox-examples|
16+
2. Playground...................................|refactoring-toolbox-playground|
17+
18+
===============================================================================
19+
INTRO *refactoring-toolbox-intro*
20+
21+
Vim PHP Refactoring ToolBox.
22+
23+
A set of commands which help you to refactor PHP code.
24+
25+
===============================================================================
26+
Default Mappings *refactoring-toolbox-mappings*
27+
28+
nnoremap <unique> <Leader>rlv :call PhpRenameLocalVariable()<CR>
29+
nnoremap <unique> <Leader>rcv :call PhpRenameClassVariable()<CR>
30+
nnoremap <unique> <Leader>rm :call PhpRenameMethod()<CR>
31+
nnoremap <unique> <Leader>eu :call PhpExtractUse()<CR>
32+
vnoremap <unique> <Leader>ec :call PhpExtractConst()<CR>
33+
nnoremap <unique> <Leader>ep :call PhpExtractClassProperty()<CR>
34+
vnoremap <unique> <Leader>em :call PhpExtractMethod()<CR>
35+
nnoremap <unique> <Leader>np :call PhpCreateProperty()<CR>
36+
nnoremap <unique> <Leader>du :call PhpDetectUnusedUseStatements()<CR>
37+
vnoremap <unique> <Leader>== :call PhpAlignAssigns()<CR>
38+
nnoremap <unique> <Leader>sg :call PhpCreateSettersAndGetters()<CR>
39+
nnoremap <unique> <Leader>da :call PhpDocAll()<CR>
40+
41+
===============================================================================
42+
Examples *refactoring-toolbox-examples*
43+
44+
1. Rename Local Variable........................................|rename-local-variable|
45+
2. Rename Class Variable........................................|rename-class-variable|
46+
3. Rename Method................................................|rename-method|
47+
4. Extract Use Statement........................................|extract-use-statement|
48+
5. Extract Class Property.......................................|extract-class-property|
49+
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|
55+
56+
Note: ↑ Is the position of your cursor
57+
58+
===============================================================================
59+
Rename Local Variable *rename-local-variable*
60+
61+
<?php
62+
function helloWorld($foobar = null) {
63+
echo "Hello " . $foobar;
64+
} ↑
65+
66+
<Leader>rlv in normal mode, specify the new $name
67+
68+
<?php
69+
function helloWorld($name = null) {
70+
echo "Hello " . $name;
71+
} ↑
72+
73+
===============================================================================
74+
Rename Class Variable *rename-class-variable*
75+
76+
<?php
77+
class HelloWorld {
78+
private $foobar;
79+
public function __construct($name) {
80+
$this->foobar = $name;
81+
}
82+
public function sayHello() {
83+
echo $this->foobar;
84+
} ↑
85+
}
86+
87+
<Leader>rcv in normal mode, specify the new $name
88+
89+
<?php
90+
class HelloWorld {
91+
private $name;
92+
public function __construct($name) {
93+
$this->name = $name;
94+
}
95+
public function sayHello() {
96+
echo $this->name;
97+
}
98+
}
99+
100+
===============================================================================
101+
Rename method *rename-method*
102+
103+
<?php
104+
class HelloWorld {
105+
public function sayHello() {
106+
echo $this->sayHello();
107+
} ↑
108+
}
109+
110+
<Leader>rm in normal mode, specify the new method name
111+
112+
<?php
113+
class HelloWorld {
114+
public function newMethodName() {
115+
echo $this->newMethodName();
116+
} ↑
117+
}
118+
119+
===============================================================================
120+
Extract Use Statement *extract-use-statement*
121+
122+
<?php
123+
$obj1 = new Foo\Bar\Baz;
124+
$obj2 = new Foo\Bar\Baz;
125+
126+
127+
<Leader>eu in normal mode
128+
129+
<?php
130+
131+
use Foo\Bar\Baz;
132+
133+
$obj1 = Baz;
134+
$obj2 = Baz;
135+
136+
===============================================================================
137+
Extract Class Property *extract-class-property*
138+
139+
<?php
140+
141+
class Dir {
142+
public function __construct($path) {
143+
$realpath = $path;
144+
} ↑
145+
}
146+
147+
<Leader>ep in normal mode will extract the local variable and create a property inside the current class.
148+
149+
<?php
150+
151+
class Dir {
152+
private $realpath;
153+
public function __construct($path) {
154+
$this->realpath = $path;
155+
} ↑
156+
}
157+
158+
===============================================================================
159+
Extract Method *extract-method*
160+
161+
<?php
162+
163+
class HelloWorld {
164+
public function sayHello($firstName = null) {
165+
$sentence = 'Hello';
166+
if ($firstName) {
167+
$sentence .= ' ' . $firstName;
168+
}
169+
echo $sentence;
170+
}
171+
}
172+
173+
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
174+
175+
<?php
176+
177+
class HelloWorld {
178+
public function sayHello($firstName = null) {
179+
$sentence = $this->prepareSentence($firstName);
180+
echo $sentence;
181+
}
182+
183+
private function prepareSentence($firstName)
184+
{
185+
$sentence = 'Hello';
186+
if ($firstName) {
187+
$sentence .= ' ' . $firstName;
188+
}
189+
return $sentence;
190+
}
191+
}
192+
193+
===============================================================================
194+
Create Property *create-property*
195+
196+
<Leader>np will create a new property in your current class.
197+
198+
===============================================================================
199+
Detect unused "use" statements *detect-unused-use*
200+
201+
202+
<Leader>du will detect all unused "use" statements in your code so that you can remove them.
203+
204+
===============================================================================
205+
Align assignments *align-assignments*
206+
207+
208+
<?php
209+
210+
$oneVar = 'Foo';
211+
$anOtherVar = 'Bar';
212+
$oneVar += 'Baz';
213+
214+
Select the code you want to align and then hit <Leader>==
215+
216+
<?php
217+
218+
$oneVar = 'Foo';
219+
$anOtherVar = 'Bar';
220+
$oneVar += 'Baz';
221+
222+
===============================================================================
223+
Create setters and getters *create-set-get*
224+
225+
226+
<?php
227+
228+
class Foo {
229+
private $bar;
230+
}
231+
232+
Hit <Leader>sg and you'll be prompted if you want to create setters and getters for existing properties.
233+
234+
<?php
235+
236+
class Foo {
237+
private $bar;
238+
239+
public function setBar($bar)
240+
{
241+
$this->bar = $bar;
242+
}
243+
244+
public function getBar()
245+
{
246+
return $this->bar;
247+
}
248+
}
249+
250+
===============================================================================
251+
Document all *document-all*
252+
253+
<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.
254+
255+
===============================================================================
256+
Playground *refactoring-toolbox-playground*
257+
258+
Here you have some code within you can try some of the available tools:
259+
260+
<?php
261+
namespace AdoY\PHP\Refactoring\Toolbox;
262+
use I\Am\Really\Useless as NobodyLovesMe;
263+
use I\Am\Usefull as Lover;
264+
class Playground
265+
{
266+
private $renameMe = 10;
267+
/**
268+
* Place your cursor on a local variable and press <Leader>rlv
269+
* to rename a function local variable
270+
*/
271+
public function testRenameLocalVariable($renameMe)
272+
{
273+
$renameMe = 'renameMe will be renamed';
274+
$renameMeAlso = $renameMe;
275+
$this->renameMe = 'If will be renamed in the next test';
276+
}
277+
/**
278+
* Place your cursor on a class variable and press <Leader>rcv
279+
* to rename a property (class variable)
280+
*/
281+
public function testRenameClassVariable($renameMe)
282+
{
283+
$this->renameMe = 'RenameMe rename every usage of this property in the current class';
284+
$renameMe = 'I\'m not renamed';
285+
}
286+
/**
287+
* Place your cursor on a method name and press <Leader>rm
288+
* to rename a method
289+
*/
290+
public function testRenameMethod()
291+
{
292+
$this->testRenameMethod();
293+
}
294+
/**
295+
* Place your cursor on a Fully qualified class name and press <Leader>eu
296+
* to create an alias and place the new Use statement on top of the file
297+
*/
298+
public function testExtractUse(\Fully\Qualified\Classname $obj)
299+
{
300+
if (!$obj instanceof \Fully\Qualified\Classname) {
301+
Throw new Exception('$obj is not a \Fully\Qualified\Classname');
302+
}
303+
return new \Fully\Qualified\AnOtherClassname;
304+
}
305+
/**
306+
* Select the content you want to place in the content with the visual mode
307+
* (you could use viw on int or va' on string)
308+
* and then press <Leader>ec to create a constant and replace every occurence of this
309+
* by the constant usage
310+
*/
311+
public function testExtractConst()
312+
{
313+
$dix = 1001;
314+
$string = 'FOOBAR';
315+
}
316+
/**
317+
* Place your cursor on the "localVariableWanabeAClassVariable" variable
318+
* and press <Leader>ep to promote this variable as class property
319+
*/
320+
public function testExtractClassProperty($newval)
321+
{
322+
$localVariableWanabeAClassVariable = $newval;
323+
}
324+
/**
325+
* Select different block of code and extract it to different methods using
326+
* <Leader>em
327+
*/
328+
public function testExtractMethod($message)
329+
{
330+
// Make a very cool wave with the message
331+
for ($i = 0; $i < strlen($message); $i++) {
332+
$message[$i] = $i % 2 ? strtoupper($message[$i]) : strtolower($message[$i]);
333+
}
334+
// Put the message in a fancy box
335+
$borderTopAndBottom = '+' . str_repeat('=', strlen($message)+2) . '+';
336+
$newMessage = $borderTopAndBottom . PHP_EOL;
337+
$newMessage .= '| ' . $message . ' |' . PHP_EOL;
338+
$newMessage .= $borderTopAndBottom . PHP_EOL;
339+
return $newMessage;
340+
}
341+
/**
342+
* Press <Leader>np to create a property
343+
*/
344+
public function testCreateNewProperty()
345+
{
346+
$this->notCreated;
347+
}
348+
/**
349+
* Press <Leader>du to detect unused use statements
350+
*/
351+
public function testDetectUnusedStatements()
352+
{
353+
new Lover;
354+
}
355+
/**
356+
* Select the inner function block
357+
* and press <Leader>== to align all assignements
358+
*/
359+
public function testAlignAssigns()
360+
{
361+
$oneVar = 'Foo';
362+
$anOtherVar = 'Bar';
363+
$oneVar += 'Baz';
364+
}
365+
}
366+
367+

0 commit comments

Comments
 (0)