sandysong/refactool

该软件包的最新版本(v0.1.2)没有提供许可证信息。

v0.1.2 2013-03-13 02:48 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:26:21 UTC


README

"refactool"是"refactoring tool"的缩写。它是一套代码重构工具。

安装

refactool 需要 php5.3 或更高版本

refactool 符合 PSR-0 规范,可以使用 Composer 安装。将 sandysong/refactool 添加到您的 composer.json

{
    "require": {
        "sandysong/refactool": "*"
    }
}

如果您是 Composer 新手...

目前,通过 Composer 安装是唯一支持的选项。

使用 refactool

重命名

您可以使用 bin/rname 进行一些重命名操作,例如

  • 将文件名重命名为符合 PSR-0 标准的格式
  • 通过 preg_replace 重命名类名或方法名

您可以通过输入 rname --help 来查看帮助信息

songqi@ubuntu:~/work/$ rname --help
/home/songqi/work/refactool/bin/rname

Usage: /home/songqi/work/refactool/bin/rname [OPTIONS] src dest

Change directory structure or class name to fit the standard.
This tool scan src dir for class definations and put them to a new dir, other files are left.
It does not support namespace yet

src
     srcRequired.  src directory of your code
dest
     destRequired.  dest directory to generate code

--help
     Show the help page for this command.

-i/--input <argument>
     Regex to match input files, default is '/\.php$/'

-p/--pattern <argument>
     pattern to match your class name or method name

-r/--replace <argument>
     replacement to replace your class or method name

-s/--standard <argument>
     naming standard, avalible standards are: psr0, yaf_controller

-t/--target <argument>
     if you want to rename class or method, specify target here: class, method

示例

重新组织目录/文件名以符合 PSR-0 标准的格式

$rname /path/to/your/code/source /path/to/put/things/in

将匹配 'Comm_(.*)' 的所有类重命名为 'Common_$1'

$rname -t class -p '/Comm_(.*)/' -r 'Common_$1' /path/to/your/code/source /path/to/put/things/in

这将更改以下位置的所有类/接口名称:

  • 类/接口定义: class Comm_Foo {}
  • 继承: class Foo extends Comm_Foo {}
  • 实现: class Foo implements Comm_Foo {}
  • 实例化: $obj = new Comm_Foo();
  • 静态调用: $res = Comm_Foo::hello();

将匹配 'run' 的所有方法重命名为 'indexAction'

$rname -t method -p 'run' -r 'indexAction' /path/to/your/code/source /path/to/put/things/in

这将更改以下位置的所有方法名称:

  • 方法定义: public function run() {}
  • 方法调用: $obj->run();
  • 静态方法调用: Comm_Foo::run();

重命名类/接口并使其符合 yaf_controller 的命名标准

$rname -t class -p '/Controller_(.*)/' -r '$1' -s yaf_controller /path/to/your/code/source /path/to/put/things/in

您应该发现 php-yaf 的控制器有另一种命名标准,该标准不与 PSR-0 一致

控制器名称的每一部分的首字母大写,其余部分小写

因此,这是一个正确的格式

Aoo_Boo_CooController

而这不是

AOO_BOO_COOController

控制器类名必须以 "Controller" 结尾,但文件名不是

控制器名称

Aoo_Boo_CooController

文件名

Aoo/Boo/Coo.php

因此,yaf_controller 标准将这样做

  • 在输出代码到文件时,从类名中移除 'Controller',然后生成文件名和路径。
  • 在匹配和替换类名时,将类名的每一部分转换为小写和大写,然后在类名末尾添加 'Controller'。