Skip to content

Commit

Permalink
Setup tests and define the minor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed May 18, 2014
1 parent 5926a70 commit db2bff4
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.cask/
7 changes: 7 additions & 0 deletions Cask
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(source melpa)

(package "php-refactor-mode" "0.0.1" "Execute some common PHP refactorings")

(development
(depends-on "ecukes")
(depends-on "espuds"))
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# Emacs PHP Refactoring
# php-refactor-mode

This is a minor mode which provides convenient access to the refactoring
methods provided by
[php-refactoring-browser](https://github.com/QafooLabs/php-refactoring-browser).

## Keybindings

All keybindings will start with `C-c r`
All keybindings in php-refactor-mode start with 'C-c r' followed by some
mnemonic shortcut.

* `lv`: Convert a local variable to an instance variable
* `rv`: Rename a local variable
* `em`: Select a region and extract it to a new method
* `ou`: Optimize use statments for FQCNs

## Development

To fetch the test dependencies, install
[cask](https://github.com/rejeep/cask.el) if you haven't already, then:

$ cd /path/to/php-refactor-mode
$ cask

Run the tests with:

$ cask exec ecukes
55 changes: 0 additions & 55 deletions emacs-php-refactoring.el

This file was deleted.

94 changes: 94 additions & 0 deletions features/php-refactor.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Feature: PHP Refactor

Scenario: Convert Local to Instance Variable
When I open temp file "convert-local-to-instance-variable"
And I insert:
"""
<?php
class ConvertLocalToInstanceVariable
{
public function internalMethod()
{
$localVariable = "This needs to be an instance variable";
echo $localVariable;
}
}
"""
And I save the buffer
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I press "C-c r lv"
Then I should see "private $localVariable"
And I should not see pattern "^ *\\$localVariable"

Scenario: Rename Local Variable
When I open temp file "rename-local-variable"
And I insert:
"""
<?php
class ConvertLocalToInstanceVariable
{
public function internalMethod()
{
$localVariable = "This variable needs renamed.";
echo $localVariable;
}
}
"""
And I save the buffer
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I press "C-c r rv"
Then I should see "$renamed"
And I should not see "$localVariable"


Scenario: Extract a Method
When I open temp file "extract-a-method"
And I insert:
"""
<?php
class ConvertLocalToInstanceVariable
{
public function internalMethod()
{
$localVariable = "Hello";
$localVariable += " World";
echo $localVariable;
}
}
"""
And I save the buffer
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I set the mark
And I go to word "World"
And I press "C-c r em"
Then I should see:
"""
<?php
class ConvertLocalToInstanceVariable
{
public function internalMethod()
{
$localVariable = $this->newMethomdName();
echo $localVariable;
}
private function newMethomdName()
{
$localVariable = "Hello";
$localVariable += " World";
return $localVariable;
}
}
"""
3 changes: 3 additions & 0 deletions features/step-definitions/basic-steps.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(And "^I save the buffer$"
(lambda ()
(save-buffer)))
22 changes: 22 additions & 0 deletions features/support/env.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(let* ((current-directory (file-name-directory load-file-name))
(features-directory (expand-file-name ".." current-directory))
(project-directory (expand-file-name ".." features-directory)))
(setq php-refactor-root-path project-directory))

(add-to-list 'load-path php-refactor-root-path)

(require 'php-refactor-mode)
(require 'espuds)
(require 'ert)

(Before
(switch-to-buffer
(get-buffer-create "*php-refactor*"))
(erase-buffer)
(transient-mark-mode 1)
(cua-mode 0)
(php-refactor-mode 0)
(setq set-mark-default-inactive nil)
(deactivate-mark))

(After)
94 changes: 94 additions & 0 deletions php-refactor-mode.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
;;; package -- summary
;;; Commentary:

;;; Code:

(require 'thingatpt)
(require 'locate)

;; (defvar php-refactor-command "refactor.phar")
;; (defvar php-refactor-patch-command "patch -p1")

(defgroup php-refactor nil
"Quickly and safely perform common refactorings."
:group 'tools
:group 'convenience)

(defcustom php-refactor-command "refactor.phar"
"Define the command used for executing the refactoring."
:group 'php-refactor
:type 'symbol)

(defcustom php-refactor-patch-command "patch -p1"
"Define the command used for applying the patch."
:group 'php-refactor
:type 'symbol)

(defcustom php-refactor-keymap-prefix (kbd "C-c r")
"php-refactor keymap prefix."
:group 'php-refactor
:type 'string)

(defvar php-refactor-mode-map
(let ((map (make-sparse-keymap)))
(let ((prefix-map (make-sparse-keymap)))
(define-key prefix-map (kbd "lv") 'php-refactor--convert-local-to-instance-variable)
(define-key prefix-map (kbd "rv") 'php-refactor--rename-local-variable)
(define-key prefix-map (kbd "em") 'php-refactor--extract-method)
(define-key prefix-map (kbd "ou") 'php-refactor--optimize-use)
(define-key map php-refactor-keymap-prefix prefix-map))
map)
"Keymap for php-refactor mode.")

(define-minor-mode php-refactor-mode
"Minor more to quickly and safely perform common refactorings."
nil " Refactor" php-refactor-mode-map)

(defun php-refactor--convert-local-to-instance-variable ()
"Convert a local variable into an instance variable of the class."
(interactive)
(php-refactor--run-command
"convert-local-to-instance-variable"
(list (buffer-file-name) (number-to-string (locate-current-line-number)) (concat "'" (word-at-point) "'"))))

(defun php-refactor--optimize-use ()
"Optimizes the use of Fully qualified names in a file."
(interactive)
(php-refactor--run-command "optimize-use" (list (buffer-file-name))))

(defun php-refactor--extract-method (begin end)
"Extract the selected region into a separate method.
BEGIN is the starting position of the selected region.
END is the ending position of the selected region."
(interactive "r")
(let ((region-start (number-to-string (line-number-at-pos begin)))
(region-end (number-to-string (line-number-at-pos end))))
(php-refactor--run-command
"extract-method"
(list (buffer-file-name) (concat region-start "-" region-end) "newMethomdName"))))

(defun php-refactor--rename-local-variable ()
"Rename an existing local variable to the specified new name."
(interactive)
(php-refactor--run-command
"rename-local-variable"
(list (buffer-file-name) (number-to-string (locate-current-line-number)) (concat "'" (word-at-point) "'") "renamed")))

(defun php-refactor--run-command (method args)
"Execute the given refactoring command and apply the resulting patch.
METHOD specifies the refactoring method to run.
ARGS contains a list of flags required for the specific method to run."

;; TODO We need to make sure the file is saved first. Otherwise, we can't do this.
(shell-command
(mapconcat 'identity
(list php-refactor-command method
(mapconcat 'identity args " ") "| tee ~/patch |" php-refactor-patch-command) " ")
nil)
(revert-buffer nil t))

(provide 'php-refactor-mode)

;;; emacs-php-refactor ends here

0 comments on commit db2bff4

Please sign in to comment.