Skip to content

Commit d06dabd

Browse files
committed
Variable refactoring dependent on cursor point
Summary: By using (thing-at-point 'symbol), the refactoring command becomes highly dependent on the placement of the point at the time the refactoring command is called. By using 'sexp, the placement of the point doesn't matter. The tests were never catching this because it only shows up when php-mode is actually enabled. The tests have been updated to cover this situation. I have also expanded them to verify that we never break snake case functionality as well. Fixes #4
1 parent de47bb7 commit d06dabd

7 files changed

+117
-9
lines changed

Cask

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
(development
66
(depends-on "ecukes")
7-
(depends-on "espuds"))
7+
(depends-on "espuds")
8+
(depends-on "php-mode"))

features/convert-local.feature

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,59 @@ Feature: Convert Local to Instance Variable
1616
}
1717
}
1818
"""
19+
And I turn on php-mode
1920
And I turn on php-refactor-mode
20-
And I go to word "$localVariable"
21+
And I place the cursor before "$localVariable"
2122
And I press "C-c r lv"
2223
Then I should see "private $localVariable"
2324
And I should not see pattern "^ *$localVariable"
2425

26+
Scenario: A local variable should be converted to an instance variable when point is in middle of variable
27+
When I open temp file "convert-local-to-instance-variable"
28+
And I insert:
29+
"""
30+
<?php
31+
32+
class ConvertLocalToInstanceVariable
33+
{
34+
public function internalMethod()
35+
{
36+
$localVariable = "This needs to be an instance variable";
37+
38+
echo $localVariable;
39+
}
40+
}
41+
"""
42+
And I turn on php-mode
43+
And I turn on php-refactor-mode
44+
And I place the cursor after "$localV"
45+
And I press "C-c r lv"
46+
Then I should see "private $localVariable"
47+
And I should not see pattern "^ *$localVariable"
48+
49+
Scenario: A local variable should be converted to an instance variable when using snake case
50+
When I open temp file "convert-local-to-instance-variable"
51+
And I insert:
52+
"""
53+
<?php
54+
55+
class ConvertLocalToInstanceVariable
56+
{
57+
public function internalMethod()
58+
{
59+
$local_variable = "This needs to be an instance variable";
60+
61+
echo $local_variable;
62+
}
63+
}
64+
"""
65+
And I turn on php-mode
66+
And I turn on php-refactor-mode
67+
And I place the cursor before "$local_variable"
68+
And I press "C-c r lv"
69+
Then I should see "private $local_variable"
70+
And I should not see pattern "^ *$local_variable"
71+
2572
Scenario: Converting local variable is an undoable operation
2673
When I open temp file "convert-local-to-instance-variable"
2774
And I insert:
@@ -38,8 +85,9 @@ Feature: Convert Local to Instance Variable
3885
}
3986
}
4087
"""
88+
And I turn on php-mode
4189
And I turn on php-refactor-mode
42-
And I go to word "$localVariable"
90+
And I place the cursor before "$localVariable"
4391
And I press "C-c r lv"
4492
And I press "C-_"
4593
Then I should not see "private $localVariable"

features/extract-method.feature

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ Feature: Extract a Method
1717
}
1818
}
1919
"""
20+
And I turn on php-mode
2021
And I turn on php-refactor-mode
21-
And I go to word "$localVariable"
22+
And I place the cursor before "$localVariable"
2223
And I set the mark
2324
And I go to word "World"
2425
And I start an action chain
@@ -65,8 +66,9 @@ Feature: Extract a Method
6566
}
6667
}
6768
"""
69+
And I turn on php-mode
6870
And I turn on php-refactor-mode
69-
And I go to word "$localVariable"
71+
And I place the cursor before "$localVariable"
7072
And I set the mark
7173
And I go to word "World"
7274
And I start an action chain

features/optimize-use.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Feature: Optimize Use
1818
}
1919
}
2020
"""
21+
And I turn on php-mode
2122
And I turn on php-refactor-mode
2223
And I press "C-c r ou"
2324
Then I should see "use Top\Level\Domain\Name"
@@ -42,6 +43,7 @@ Feature: Optimize Use
4243
}
4344
}
4445
"""
46+
And I turn on php-mode
4547
And I turn on php-refactor-mode
4648
And I press "C-c r ou"
4749
And I press "C-_"

features/rename-local.feature

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,68 @@ Feature: Rename Local Variable
1616
}
1717
}
1818
"""
19+
And I turn on php-mode
1920
And I turn on php-refactor-mode
20-
And I go to word "$localVariable"
21+
And I place the cursor before "$localVariable"
2122
And I start an action chain
2223
And I press "C-c r rv"
2324
And I type "renamedVariable"
2425
And I execute the action chain
2526
Then I should not see "$localVariable"
2627
And I should see "$renamedVariable"
2728

29+
Scenario: A variable should be renamed when point is in middle of variable
30+
When I open temp file "rename-local-variable"
31+
And I insert:
32+
"""
33+
<?php
34+
35+
class RenameLocalVariable
36+
{
37+
public function internalMethod()
38+
{
39+
$localVariable = "This variable needs renamed.";
40+
41+
echo $localVariable;
42+
}
43+
}
44+
"""
45+
And I turn on php-mode
46+
And I turn on php-refactor-mode
47+
And I place the cursor after "$localV"
48+
And I start an action chain
49+
And I press "C-c r rv"
50+
And I type "renamedVariable"
51+
And I execute the action chain
52+
Then I should not see "$localVariable"
53+
And I should see "$renamedVariable"
54+
55+
Scenario: A variable should be renamed when using snake case
56+
When I open temp file "rename-local-variable"
57+
And I insert:
58+
"""
59+
<?php
60+
61+
class RenameLocalVariable
62+
{
63+
public function internalMethod()
64+
{
65+
$local_variable = "This variable needs renamed.";
66+
67+
echo $local_variable;
68+
}
69+
}
70+
"""
71+
And I turn on php-mode
72+
And I turn on php-refactor-mode
73+
And I place the cursor before "$local_variable"
74+
And I start an action chain
75+
And I press "C-c r rv"
76+
And I type "renamed_variable"
77+
And I execute the action chain
78+
Then I should not see "$local_variable"
79+
And I should see "$renamed_variable"
80+
2881
Scenario: Renaming a variable is an undoable operation
2982
When I open temp file "rename-local-variable"
3083
And I insert:
@@ -41,8 +94,9 @@ Feature: Rename Local Variable
4194
}
4295
}
4396
"""
97+
And I turn on php-mode
4498
And I turn on php-refactor-mode
45-
And I go to word "$localVariable"
99+
And I place the cursor before "$localVariable"
46100
And I start an action chain
47101
And I press "C-c r rv"
48102
And I type "renamedVariable"

features/support/env.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

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

8+
(require 'php-mode)
89
(require 'php-refactor-mode)
910
(require 'espuds)
1011
(require 'ert)

php-refactor-mode.el

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"convert-local-to-instance-variable"
8080
(buffer-file-name)
8181
(php-refactor--get-effective-line-number-as-string)
82-
(thing-at-point 'symbol)))
82+
(thing-at-point 'sexp)))
8383

8484
(defun php-refactor--optimize-use ()
8585
"Optimizes the use of Fully qualified names in a file."
@@ -111,7 +111,7 @@ END is the ending position of the selected region."
111111
"rename-local-variable"
112112
(buffer-file-name)
113113
(php-refactor--get-effective-line-number-as-string)
114-
(thing-at-point 'symbol)
114+
(thing-at-point 'sexp)
115115
renamed)))
116116

117117
(defun php-refactor--get-effective-line-number-as-string ()

0 commit comments

Comments
 (0)