diff --git a/doc/refactoring-toolbox.txt b/doc/refactoring-toolbox.txt new file mode 100755 index 0000000..a42729d --- /dev/null +++ b/doc/refactoring-toolbox.txt @@ -0,0 +1,367 @@ +*refactoring-toolbox* + +__________ _____ __ .__ ___________ .__ ___. +\______ \ _____/ ____\____ _____/ |_ ___________|__| ____ ____ \__ ___/___ ____ | |\_ |__ _______ ___ + | _// __ \ __\\__ \ _/ ___\ __\/ _ \_ __ \ |/ \ / ___\ | | / _ \ / _ \| | | __ \ / _ \ \/ / + | | \ ___/| | / __ \\ \___| | ( <_> ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > < + |____|_ /\___ >__| (____ /\___ >__| \____/|__| |__|___| /\___ / |____| \____/ \____/|____/___ /\____/__/\_ \ + \/ \/ \/ \/ \//_____/ \/ \/ + +=============================================================================== +CONTENTS *refactoring-toolbox-contents* + + 1. Intro........................................|refactoring-toolbox-intro| + 2. Mappings.....................................|refactoring-toolbox-mappings| + 2. Examples.....................................|refactoring-toolbox-examples| + 2. Playground...................................|refactoring-toolbox-playground| + +=============================================================================== +INTRO *refactoring-toolbox-intro* + +Vim PHP Refactoring ToolBox. + +A set of commands which help you to refactor PHP code. + +=============================================================================== +Default Mappings *refactoring-toolbox-mappings* + +nnoremap rlv :call PhpRenameLocalVariable() +nnoremap rcv :call PhpRenameClassVariable() +nnoremap rm :call PhpRenameMethod() +nnoremap eu :call PhpExtractUse() +vnoremap ec :call PhpExtractConst() +nnoremap ep :call PhpExtractClassProperty() +vnoremap em :call PhpExtractMethod() +nnoremap np :call PhpCreateProperty() +nnoremap du :call PhpDetectUnusedUseStatements() +vnoremap == :call PhpAlignAssigns() +nnoremap sg :call PhpCreateSettersAndGetters() +nnoremap da :call PhpDocAll() + +=============================================================================== +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. 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| + +Note: ↑ Is the position of your cursor + +=============================================================================== +Rename Local Variable *rename-local-variable* + +rlv in normal mode, specify the new $name + +foobar = $name; + } + public function sayHello() { + echo $this->foobar; + } ↑ +} + +rcv in normal mode, specify the new $name + +name = $name; + } + public function sayHello() { + echo $this->name; + } +} + +=============================================================================== +Rename method *rename-method* + +sayHello(); + } ↑ +} + +rm in normal mode, specify the new method name + +newMethodName(); + } ↑ +} + +=============================================================================== +Extract Use Statement *extract-use-statement* + +eu in normal mode + +ep in normal mode will extract the local variable and create a property inside the current class. + +realpath = $path; + } ↑ +} + +=============================================================================== +Extract Method *extract-method* + +em. You'll be prompted for a method name. Enter a method name and press enter + +prepareSentence($firstName); + echo $sentence; + } + + private function prepareSentence($firstName) + { + $sentence = 'Hello'; + if ($firstName) { + $sentence .= ' ' . $firstName; + } + return $sentence; + } +} + +=============================================================================== +Create Property *create-property* + +np will create a new property in your current class. + +=============================================================================== +Detect unused "use" statements *detect-unused-use* + + +du will detect all unused "use" statements in your code so that you can remove them. + +=============================================================================== +Align assignments *align-assignments* + + +== + +sg and you'll be prompted if you want to create setters and getters for existing properties. + +bar = $bar; + } + + public function getBar() + { + return $this->bar; + } +} + +=============================================================================== +Document all *document-all* + +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: + +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 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 rm + * to rename a method + */ + public function testRenameMethod() + { + $this->testRenameMethod(); + } + /** + * Place your cursor on a Fully qualified class name and press 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 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 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 + * 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 np to create a property + */ + public function testCreateNewProperty() + { + $this->notCreated; + } + /** + * Press du to detect unused use statements + */ + public function testDetectUnusedStatements() + { + new Lover; + } + /** + * Select the inner function block + * and press == to align all assignements + */ + public function testAlignAssigns() + { + $oneVar = 'Foo'; + $anOtherVar = 'Bar'; + $oneVar += 'Baz'; + } +} + +