PHP 代码重构工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:11:44  来源:igfitidea点击:

Tools for PHP code refactoring

phprefactoring

提问by

Are there any tools which support refactoring PHP code (renaming variables, extracting methods, finding method references, ...)? Thank you.

是否有任何工具支持重构 PHP 代码(重命名变量、提取方法、查找方法引用等)?谢谢你。

回答by Ian Greenleaf Young

Scisris a simple, standalone refactoring tool for PHP.

Scisr是一个简单的、独立的 PHP 重构工具。

I know this is an old question, but since it's one of the best rundowns of PHP refactoring tools, I thought I would add my new project to the list.

我知道这是一个老问题,但由于它是 PHP 重构工具的最佳概要之一,我想我会将我的新项目添加到列表中。

A lot of my design goals sprung out of the inadequacies of other items mentioned here - they are tied into certain IDEs, or try to dictate your testing practices, or require a whole bunch of rote manual labor. I am hoping to create an overall better experience in Scisr. It's simple to install and run. It tries to be clever, but not tooclever. It does the tasks that I have found to lend themselves best to automation. Enjoy!

我的很多设计目标都源于这里提到的其他项目的不足之处——它们与某些 IDE 相关联,或者试图规定你的测试实践,或者需要一大堆死记硬背的体力劳动。我希望在 Scisr 中创造更好的整体体验。安装和运行都很简单。它试图变得聪明,但不要聪明。它执行我发现最适合自动化的任务。享受!

回答by troelskn

rephactoris an automatic refactoring tool. It is still a bit incomplete, but it has a few basic refactorings.

rephactor是一个自动重构工具。它仍然有点不完整,但它有一些基本的重构。

Else, a good test suite is really the best tool for refactoring, you can have.

否则,一个好的测试套件确实是重构的最佳工具,您可以拥有。

回答by Cedric

PHPStorm from Jetbrains support refactoring, http://www.jetbrains.com/phpstorm/index.html

Jetbrains 的 PHPStorm 支持重构, http://www.jetbrains.com/phpstorm/index.html

The Rename refactoring works for files, functions, constants, classes, properties, methods, parameters, and local and global variables. Also following refactorings are available:

Rename 重构适用于文件、函数、常量、类、属性、方法、参数以及局部和全局变量。还可以进行以下重构:

  • Introduce Variable
  • Introduce Constant
  • Introduce Field
  • Inline variable
  • 引入变量
  • 引入常数
  • 介绍领域
  • 内联变量

and they offer some free license to open source project as well !

他们还为开源项目提供一些免费许可!

回答by Ross

The NetBeans IDE has some refactoring capability but it doesn't always seem to work. I am however using 6.1 with early PHP access. I expect 6.5 to have better refactoring capabilities with PHP.

NetBeans IDE 具有一些重构功能,但似乎并不总是有效。然而,我使用 6.1 和早期的 PHP 访问。我希望 6.5 具有更好的 PHP 重构能力。

回答by Frambot

https://github.com/QafooLabs/php-refactoring-browser

https://github.com/QafooLabs/php-refactoring-browser

PHP Refactoring Browser is a more recent take on the Refactoring Browser described in the good book. It could use some help (please contribute), but it has support for several common refactorings.

PHP Refactoring Browser 是对好书中描述的 Refactoring Browser 的更新。它可以使用一些帮助(请贡献),但它支持几个常见的重构。

As of writing, the browser supports:

在撰写本文时,浏览器支持:

  • Extract Method
  • Rename Local Variable
  • Convert Local to Instance Variable
  • Rename Class and Namespaces
  • Optimize UseStatements (PHP-specific)
  • 提取方法
  • 重命名局部变量
  • 将本地变量转换为实例变量
  • 重命名类和命名空间
  • 优化Use语句(特定于 PHP)

There is a SublimeText plugin which actually works. When I'm looking at new code I'll apply some of these simple refactorings to help me get a handle on the codebase.

有一个真正有效的 SublimeText 插件。当我查看新代码时,我将应用其中一些简单的重构来帮助我掌握代码库。

回答by reedwolf

I personally prefer PHPEclipseIDE as my primary development tool. But I really miss basic "replace everywhere in the project" refactoring there.

我个人更喜欢PHPEclipseIDE 作为我的主要开发工具。但我真的很怀念在那里重构的基本“替换项目中的任何地方”。

But very simple find/sedcommand can solve it easily:

但是很简单的find/ sed命令就可以轻松解决:

find . -type f -name "*Controller.php" -print0 \
         | xargs -0 sed -i 's/string1/string2/g'

So using this intelligently you can find references, renaming, removing and make any sort of manipulations with the source code.

因此,智能地使用它,您可以查找引用、重命名、删除和对源代码进行任何类型的操作。

BTW: you even can not remember all this parameters and attributes - just type it once and then call it through reverse-i-search.

顺便说一句:您甚至无法记住所有这些参数和属性 - 只需键入一次,然后通过reverse-i-search调用它。

Hope this helps.

希望这可以帮助。

回答by matthy

Zend Studio can Refactor a bit.....

Zend Studio 可以重构一下......

回答by SchizoDuckie

Still my favorite refactoring tool is good ol' EditPlusin combination with Total Commander's super fast file search. (Hint: Alt+F7) I totally agree with Mark Biek's reply that you want to have full control over what is replaced where and when.

我最喜欢的重构工具仍然是很好的 ol' EditPlusTotal Commander的超快速文件搜索相结合。(提示:Alt+F7)我完全同意 Mark Biek 的回答,即您希望完全控制更换的地点和时间。

Whenever i have to do a some refactoring, i create a list in total commander of the files i want to alter by either searching with the built-in function or just by selecting them, and then i drag them all to EditPlus

每当我必须进行一些重构时,我都会通过使用内置功能进行搜索或仅通过选择它们来创建我想要更改的文件的总指挥官列表,然后将它们全部拖到 EditPlus

Editplus can search & replace through the current file, a selection or all opened filesusing normal text matching, regex matching or multiline matching. It really gives you full control :)

Editplus 可以使用普通文本匹配、正则表达式匹配或多行匹配搜索和替换当前文件、选择或所有打开的文件。它真的让你完全控制:)

Also, what comes in handy for code refactoring is EditPlus's Macro Recorder (Ctrl + Q to start recording macro 1-9 and Alt + 1-9 to playback a macro).

此外,对于代码重构非常有用的是 EditPlus 的宏录制器(Ctrl + Q 开始录制宏 1-9,Alt + 1-9 播放宏)。

Once you get the hang of it, you know you can just search for a variable, ctrl + shift + (home || end || arrow keys) through your code, delete, copy/paste and use al that keyboard wizardry to like, for instance, convert a CSV file into a bunch of SQL queries within 30 seconds.

一旦你掌握了它,你就知道你可以通过你的代码搜索一个变量,ctrl + shift +(home || end || 箭头键),删除,复制/粘贴并使用所有键盘魔法喜欢,例如,在 30 秒内将 CSV 文件转换为一堆 SQL 查询。

回答by Mark Biek

Maybe it's just because I'm still getting comfortable with the concepts of refactoring but I don't like having a tool do it for me.

也许只是因为我仍然对重构的概念感到满意,但我不喜欢让工具为我做这件事。

I still prefer to do most refactoring manually because it forces me to slow down and really think about what I'm moving around and why.

我仍然更喜欢手动进行大多数重构,因为它迫使我放慢速度并真正思考我正在移动的内容以及原因。

回答by Shunyata Kharg

I read that the IDE Delphi for PHP will have refactoring capabilities in the release codenamed Crocodile, scheduled for early 2009. See this linkfor details.

我了解到 IDE Delphi for PHP 将在代号为 Crocodile 的发行版中具有重构功能,计划于 2009 年初发布。有关详细信息,请参阅此链接