重构:如何用函数代码替换函数调用
Refactoring: How to replace function call with function code
我想要实现的是类似于 PhpStorm 的 Code -> Refactor -> Extract -> Method
功能,反之亦然。
我想调用一些特定的函数并将其替换为该函数的代码。
例如我有:
function main()
{
$test = "test";
module1($test);
module2($test);
}
function module1($text)
{
echo "Some text:" . $text;
}
function module2($text)
{
echo "Another text:" . $text;
}
然后我想收到下一个结果:
function main()
{
$test = "test";
echo "Some text:" . $test;
echo "Another text:" . $test;
}
我不需要完全递归,例如如果 module1()
函数包含另一个函数调用 - 我不需要深入研究它。就让它在 1 级吧。
对于我来说,如何使用 PhpStorm、另一个 IDE 或另一个脚本来实现并不重要。
在 PhpStorm 中使用 Refactor | Inline...
(在 function/method 定义中调用它)。
<?php
function aa($hello)
{
return "Hello $hello";
}
echo aa('Yo!');
该简单代码的最终结果:
<?php
echo "Hello 'Yo!'";
您可能会看到它有点不正确(变量内容两边的单引号),因此请确保事后检查您的代码。
代码示例的最终结果(在每个函数上使用之后):
我想要实现的是类似于 PhpStorm 的 Code -> Refactor -> Extract -> Method
功能,反之亦然。
我想调用一些特定的函数并将其替换为该函数的代码。
例如我有:
function main()
{
$test = "test";
module1($test);
module2($test);
}
function module1($text)
{
echo "Some text:" . $text;
}
function module2($text)
{
echo "Another text:" . $text;
}
然后我想收到下一个结果:
function main()
{
$test = "test";
echo "Some text:" . $test;
echo "Another text:" . $test;
}
我不需要完全递归,例如如果 module1()
函数包含另一个函数调用 - 我不需要深入研究它。就让它在 1 级吧。
对于我来说,如何使用 PhpStorm、另一个 IDE 或另一个脚本来实现并不重要。
在 PhpStorm 中使用 Refactor | Inline...
(在 function/method 定义中调用它)。
<?php
function aa($hello)
{
return "Hello $hello";
}
echo aa('Yo!');
该简单代码的最终结果:
<?php
echo "Hello 'Yo!'";
您可能会看到它有点不正确(变量内容两边的单引号),因此请确保事后检查您的代码。
代码示例的最终结果(在每个函数上使用之后):