I am new to eclipse and am using version Version: Mars.1 Release (4.5.1)
I am using the refactoring tool of the "PHP Development Tools 3.6" plugin to rename e.g. classes.
Our class file names follow the simple PSR-0 convention that an underscore represents a subdir.
So e.g. class Class_Something is located in Class/Something.php
If I rename the class Class_Something to Class_Something2 it would be great if the file would be automatically moved to Class/Something2.php
Does anybody know if it is possible to automatically refactor not only the name and the references of the class but also the file name?
Thanks in advance for your help!
Ben
Handmade solution who looks like an Eclipse plugin
First, download and install PHP Parser from: php-parser-github a simple example will show you that you can get the class name from a given source code
Example of source code
require 'vendor/autoload.php';
use PhpParser\ParserFactory;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$stmts = $parser->parse('<?php class MyClass_SubDir {private $member;}');
var_dump($stmts);
}
catch (Error $e) {
echo 'Parse Error: ', $e->getMessage();
}
Running it from command line//Output
array(1) {
[0]=>
object(PhpParser\Node\Stmt\Class_)#8 (6) {
["type"]=>
int(0)
["extends"]=>
NULL
["implements"]=>
array(0) {
}
["name"]=>
string(14) "MyClass_SubDir"
["stmts"]=>
array(1) {
[0]=>
object(PhpParser\Node\Stmt\Property)#7 (3) {
...
}
and as you can see, we can get all class that are defined in the current source
--object(PhpParser\Node\Stmt\Class_
|
|___ name : MyClass_SubDir
The goals here are
Save your php code somewhere ... eclipse_plugin.php for example ..
The next step is to create a batch file that will be plugged with Eclipse
Create a bath file that it will receive 2 arguments from Eclipse
In the batch file put
php eclipse_plugin.php $1 $2
Note : php must be in the PATH
environment variables
PHP will find his arguments in $argv[0]
and $argv[1]
Configuring Eclipse
Done !, now when you rename your class in Eclipse, just click on Run Button :)
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With