↓ Skip to Content Start of content

php-extract-function.el

Source
https://gist.github.com/Sodaware/5104696/

A function for refactoring PHP code. Moves the currently marked region to a new function and replaces it with a call to that function.

php-extract-function.el

(defun php-extract-function (start end name)
  "Moves the currently marked text to a new function"

  ;; Prompt for new method name
  (interactive "r\nsNew Function Name: ")

  ;; Kill selected region
  (kill-region start end)

  ;; Insert call to new function
  (insert "\n" name "();\n")

  ;; Set a marker so we can jump back to this line
  (point-to-register 1)

  ;; Move to end of current function
  (php-end-of-defun)

  ;; Insert new function
  (insert "\n\nfunction " name "()\n{\n"
	  (car kill-ring-yank-pointer)
	  "\n}\n")

  ;; Jump back to where function was snipped from
  (jump-to-register 1))