Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 51206e1

Browse files
Tom Oramtomphp
Tom Oram
authored andcommitted
Some tidying to the fix class names code
1 parent 3ff0f7b commit 51206e1

File tree

2 files changed

+161
-95
lines changed

2 files changed

+161
-95
lines changed

features/fix_class_names.feature

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ Feature: Fix Class Names
7575
| dir | src/ |
7676
Then the PHP File "src/Foo.php" should be refactored:
7777
"""
78+
--- a/vfs://project/src/Foo.php
79+
+++ b/vfs://project/src/Foo.php
80+
@@ -1,2 +1,2 @@
81+
<?php
82+
-use Foo\Foo;
83+
+use Foo\Bar;
84+
7885
--- a/vfs://project/src/src/Foo/Bar.php
7986
+++ b/vfs://project/src/src/Foo/Bar.php
8087
@@ -1,6 +1,6 @@
@@ -85,13 +92,6 @@ Feature: Fix Class Names
8592
+class Bar
8693
{
8794
}
88-
89-
--- a/vfs://project/src/Foo.php
90-
+++ b/vfs://project/src/Foo.php
91-
@@ -1,2 +1,2 @@
92-
<?php
93-
-use Foo\Foo;
94-
+use Foo\Bar;
9595
"""
9696
Scenario: "Namespace moved changes use statements"
9797
Given a PHP File named "src/Foo/Bar.php" with:
@@ -113,6 +113,13 @@ Feature: Fix Class Names
113113
| dir | src/ |
114114
Then the PHP File "src/Foo.php" should be refactored:
115115
"""
116+
--- a/vfs://project/src/Foo.php
117+
+++ b/vfs://project/src/Foo.php
118+
@@ -1,2 +1,2 @@
119+
<?php
120+
-use Bar\Bar;
121+
+use Foo\Bar;
122+
116123
--- a/vfs://project/src/src/Foo/Bar.php
117124
+++ b/vfs://project/src/src/Foo/Bar.php
118125
@@ -1,5 +1,5 @@
@@ -122,13 +129,6 @@ Feature: Fix Class Names
122129
123130
class Bar
124131
{
125-
126-
--- a/vfs://project/src/Foo.php
127-
+++ b/vfs://project/src/Foo.php
128-
@@ -1,2 +1,2 @@
129-
<?php
130-
-use Bar\Bar;
131-
+use Foo\Bar;
132132
"""
133133

134134
Scenario: "Rename class changes static occurances"
@@ -151,6 +151,13 @@ Feature: Fix Class Names
151151
| dir | src/ |
152152
Then the PHP File "src/Foo.php" should be refactored:
153153
"""
154+
--- a/vfs://project/src/Foo.php
155+
+++ b/vfs://project/src/Foo.php
156+
@@ -1,2 +1,2 @@
157+
<?php
158+
-Foo\Foo::bar();
159+
+Foo\Bar::bar();
160+
154161
--- a/vfs://project/src/src/Foo/Bar.php
155162
+++ b/vfs://project/src/src/Foo/Bar.php
156163
@@ -1,6 +1,6 @@
@@ -161,13 +168,6 @@ Feature: Fix Class Names
161168
+class Bar
162169
{
163170
}
164-
165-
--- a/vfs://project/src/Foo.php
166-
+++ b/vfs://project/src/Foo.php
167-
@@ -1,2 +1,2 @@
168-
<?php
169-
-Foo\Foo::bar();
170-
+Foo\Bar::bar();
171171
"""
172172
Scenario: "Rename class changes new instantiations"
173173
Given a PHP File named "src/Foo/Bar.php" with:
@@ -189,6 +189,13 @@ Feature: Fix Class Names
189189
| dir | src/ |
190190
Then the PHP File "src/Foo.php" should be refactored:
191191
"""
192+
--- a/vfs://project/src/Foo.php
193+
+++ b/vfs://project/src/Foo.php
194+
@@ -1,2 +1,2 @@
195+
<?php
196+
-new Foo\Foo();
197+
+new Foo\Bar();
198+
192199
--- a/vfs://project/src/src/Foo/Bar.php
193200
+++ b/vfs://project/src/src/Foo/Bar.php
194201
@@ -1,6 +1,6 @@
@@ -200,12 +207,6 @@ Feature: Fix Class Names
200207
{
201208
}
202209
203-
--- a/vfs://project/src/Foo.php
204-
+++ b/vfs://project/src/Foo.php
205-
@@ -1,2 +1,2 @@
206-
<?php
207-
-new Foo\Foo();
208-
+new Foo\Bar();
209210
"""
210211
Scenario: "Rename class changes that is extended"
211212
Given a PHP File named "src/Foo/Bar.php" with:
@@ -231,14 +232,14 @@ Feature: Fix Class Names
231232
| dir | src/ |
232233
Then the PHP File "src/Foo.php" should be refactored:
233234
"""
234-
--- a/vfs://project/src/src/Foo/Bar.php
235-
+++ b/vfs://project/src/src/Foo/Bar.php
235+
--- a/vfs://project/src/Foo/Baz.php
236+
+++ b/vfs://project/src/Foo/Baz.php
236237
@@ -1,6 +1,6 @@
237-
<?php
238-
namespace Foo;
238+
<?php
239+
namespace Foo;
239240
240-
-class Foo
241-
+class Bar
241+
-class Baz extends Foo
242+
+class Baz extends Bar
242243
{
243244
}
244245
@@ -253,14 +254,14 @@ Feature: Fix Class Names
253254
{
254255
}
255256
256-
--- a/vfs://project/src/Foo/Baz.php
257-
+++ b/vfs://project/src/Foo/Baz.php
257+
--- a/vfs://project/src/src/Foo/Bar.php
258+
+++ b/vfs://project/src/src/Foo/Bar.php
258259
@@ -1,6 +1,6 @@
259-
<?php
260-
namespace Foo;
260+
<?php
261+
namespace Foo;
261262
262-
-class Baz extends Foo
263-
+class Baz extends Bar
263+
-class Foo
264+
+class Bar
264265
{
265266
}
266267
@@ -285,9 +286,16 @@ Feature: Fix Class Names
285286
| dir | src/ |
286287
Then the PHP File "src/Foo.php" should be refactored:
287288
"""
289+
--- a/vfs://project/src/index.php
290+
+++ b/vfs://project/src/index.php
291+
@@ -1,2 +1,2 @@
292+
<?php
293+
-$foo = new \Foo\Foo\Foo();
294+
+$foo = new \Foo\Bar\Baz\Boing();
295+
288296
--- a/vfs://project/src/src/Foo/Bar/Baz/Boing.php
289297
+++ b/vfs://project/src/src/Foo/Bar/Baz/Boing.php
290-
@@ -1,6 +1,6 @@
298+
@@ -1,6 +1,6 @@
291299
<?php
292300
-namespace Foo\Foo;
293301
+namespace Foo\Bar\Baz;
@@ -296,13 +304,6 @@ Feature: Fix Class Names
296304
+class Boing
297305
{
298306
}
299-
300-
--- a/vfs://project/src/index.php
301-
+++ b/vfs://project/src/index.php
302-
@@ -1,2 +1,2 @@
303-
<?php
304-
-$foo = new \Foo\Foo\Foo();
305-
+$foo = new \Foo\Bar\Baz\Boing();
306307
"""
307308

308309
Scenario: "Removing a slice of a namespace"
@@ -325,6 +326,13 @@ Feature: Fix Class Names
325326
| dir | src/ |
326327
Then the PHP File "src/Foo.php" should be refactored:
327328
"""
329+
--- a/vfs://project/src/index.php
330+
+++ b/vfs://project/src/index.php
331+
@@ -1,2 +1,2 @@
332+
<?php
333+
-$foo = new \Foo\Foo\Foo();
334+
+$foo = new \Foo\Boing();
335+
328336
--- a/vfs://project/src/src/Foo/Boing.php
329337
+++ b/vfs://project/src/src/Foo/Boing.php
330338
@@ -1,6 +1,6 @@
@@ -336,12 +344,5 @@ Feature: Fix Class Names
336344
+class Boing
337345
{
338346
}
339-
340-
--- a/vfs://project/src/index.php
341-
+++ b/vfs://project/src/index.php
342-
@@ -1,2 +1,2 @@
343-
<?php
344-
-$foo = new \Foo\Foo\Foo();
345-
+$foo = new \Foo\Boing();
346347
"""
347348

src/main/QafooLabs/Refactoring/Application/FixClassNames.php

Lines changed: 106 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,143 @@
1919
use QafooLabs\Refactoring\Domain\Model\PhpName;
2020
use QafooLabs\Refactoring\Domain\Model\PhpNameChange;
2121
use QafooLabs\Refactoring\Domain\Model\PhpNames\NoImportedUsagesFilter;
22+
use QafooLabs\Refactoring\Domain\Services\CodeAnalysis;
23+
use QafooLabs\Refactoring\Domain\Services\Editor;
24+
use QafooLabs\Refactoring\Adapters\PHPParser\ParserPhpNameScanner;
25+
use QafooLabs\Refactoring\Domain\Model\PhpNameOccurance;
2226

2327
class FixClassNames
2428
{
29+
/**
30+
* @var CodeAnalysis
31+
*/
2532
private $codeAnalysis;
33+
34+
/**
35+
* @var Editor
36+
*/
2637
private $editor;
38+
39+
/**
40+
* @var ParserPhpNameScanner
41+
*/
2742
private $nameScanner;
2843

29-
public function __construct($codeAnalysis, $editor, $nameScanner)
44+
/**
45+
* @var Set
46+
*/
47+
private $renames;
48+
49+
50+
public function __construct(CodeAnalysis $codeAnalysis, Editor $editor, ParserPhpNameScanner $nameScanner)
3051
{
3152
$this->codeAnalysis = $codeAnalysis;
3253
$this->editor = $editor;
3354
$this->nameScanner = $nameScanner;
3455
}
3556

57+
3658
public function refactor(Directory $directory)
3759
{
3860
$phpFiles = $directory->findAllPhpFilesRecursivly();
3961

40-
$renames = new Set();
41-
$occurances = array();
42-
$noImportedUsages = new NoImportedUsagesFilter();
62+
$this->renames = new Set();
4363

4464
foreach ($phpFiles as $phpFile) {
45-
$classes = $this->codeAnalysis->findClasses($phpFile);
65+
$this->checkIfRenameIsRequired($phpFile);
66+
}
4667

47-
$occurances = array_merge(
48-
$noImportedUsages->filter($this->nameScanner->findNames($phpFile)),
49-
$occurances
50-
);
68+
$occurances = $this->findOccurances($phpFiles);
5169

52-
if (count($classes) !== 1) {
53-
continue;
54-
}
70+
foreach ($occurances as $occurance) {
71+
$this->performRename($occurance);
72+
}
5573

56-
$class = $classes[0];
57-
$currentClassName = $class->declarationName();
58-
$expectedClassName = $phpFile->extractPsr0ClassName();
74+
$this->editor->save();
75+
}
5976

60-
$buffer = $this->editor->openBuffer($phpFile); // This is weird to be required here
6177

62-
if ($expectedClassName->shortName() !== $currentClassName->shortName()) {
63-
$renames->add(new PhpNameChange($currentClassName, $expectedClassName));
64-
}
78+
private function checkIfRenameIsRequired(File $phpFile)
79+
{
80+
$classes = $this->codeAnalysis->findClasses($phpFile);
81+
82+
// Why skip for multiple classes in a file?
83+
if (count($classes) !== 1) {
84+
return;
85+
}
6586

66-
if (!$expectedClassName->namespaceName()->equals($currentClassName->namespaceName())) {
67-
$renames->add(new PhpNameChange($currentClassName->fullyQualified(), $expectedClassName->fullyQualified()));
87+
$class = $classes[0];
6888

69-
$buffer->replaceString(
70-
$class->namespaceDeclarationLine(),
71-
$currentClassName->namespaceName()->fullyQualifiedName(),
72-
$expectedClassName->namespaceName()->fullyQualifiedName()
73-
);
74-
}
89+
$currentClassName = $class->declarationName();
90+
$expectedClassName = $phpFile->extractPsr0ClassName();
91+
92+
if ($this->shortNameHasChanged($expectedClassName, $currentClassName)) {
93+
// Queue a rename to happen in the next loop
94+
$this->renames->add(new PhpNameChange($currentClassName, $expectedClassName));
7595
}
7696

77-
$occurances = array_filter($occurances, function ($occurance) {
78-
return $occurance->name()->type() !== PhpName::TYPE_NAMESPACE;
79-
});
97+
if ($this->namespaceHasChanged($expectedClassName, $currentClassName)) {
98+
$this->renames->add(new PhpNameChange($currentClassName->fullyQualified(), $expectedClassName->fullyQualified()));
99+
}
100+
}
80101

81-
foreach ($occurances as $occurance) {
82-
$name = $occurance->name();
83-
84-
foreach ($renames as $rename) {
85-
if ($rename->affects($name)) {
86-
$buffer = $this->editor->openBuffer($occurance->file());
87-
$buffer->replaceString($occurance->declarationLine(), $name->relativeName(), $rename->change($name)->relativeName());
88-
continue 2;
89-
}
102+
/**
103+
* @return boolean
104+
*/
105+
private function shortNameHasChanged(PhpName $expectedClassName, PhpName $currentClassName)
106+
{
107+
return $expectedClassName->shortName() !== $currentClassName->shortName();
108+
}
109+
110+
/**
111+
* @return boolean
112+
*/
113+
private function namespaceHasChanged(PhpName $expectedClassName, PhpName $currentClassName)
114+
{
115+
return !$expectedClassName->namespaceName()->equals($currentClassName->namespaceName());
116+
}
117+
118+
private function performRename(PhpNameOccurance $occurance)
119+
{
120+
$name = $occurance->name();
121+
122+
foreach ($this->renames as $rename) {
123+
if (!$rename->affects($name)) {
124+
continue;
90125
}
126+
127+
$buffer = $this->editor->openBuffer($occurance->file());
128+
129+
$buffer->replaceString(
130+
$occurance->declarationLine(),
131+
$name->relativeName(),
132+
$rename->change($name)->relativeName()
133+
);
134+
135+
// Why is a contnue required? Surely 2 renames can't apply
136+
// to the same occurance?
137+
break;
91138
}
139+
}
92140

93-
$this->editor->save();
141+
/**
142+
* @param File[] $phpFiles
143+
*
144+
* @return PhpNameOccurance[]
145+
*/
146+
private function findOccurances($phpFiles)
147+
{
148+
$occurances = array();
149+
150+
$noImportedUsages = new NoImportedUsagesFilter();
151+
152+
foreach ($phpFiles as $phpFile) {
153+
$occurances = array_merge(
154+
$noImportedUsages->filter($this->nameScanner->findNames($phpFile)),
155+
$occurances
156+
);
157+
}
158+
159+
return $occurances;
94160
}
95161
}
96-

0 commit comments

Comments
 (0)