Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse - Using File Name Conventions on Refactoring php-Classes

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

like image 940
Ben Avatar asked Sep 26 '22 16:09

Ben


1 Answers

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

  • extract directory name from a class name, to do that you can use `regex` or `explode`
  • Move the current file to the desired directory, you can use `rename` function

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

  • **$1** : container location - the absolute path of the selected file in Eclipse
  • **$2** : resource location - the absolute path and name of the selected file in Eclipse (we will need only $2 :-) )

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

  • go to Run External Tools > External Tools Configuration > click on Program Section > Press on New Launch Configuration
  • in Tab Main, specify the location of your custom batch file
  • Also specify his working directory
  • in section Arguments, put these arguments ${container_loc} ${resource_loc}

Done !, now when you rename your class in Eclipse, just click on Run Button :)

HTH

like image 148
Halayem Anis Avatar answered Sep 29 '22 00:09

Halayem Anis