php类 重构,IntelliJ IDEA:重构PHP

重构PHP

重构意味着更新源代码的结构而不改变应用程序的行为。重构可帮助您保持代码的稳定,简洁和易于维护。

更改签名在编辑器中,将光标放在要更改其签名的方法的名称中。

按下Ctrl+F6。或者,在主菜单或上下文菜单上选择:重构|更改签名。

在“更改签名”对话框中,对方法签名进行必要的更改,并指定所需的其他相关更改。例如,更改方法名称。为此,请在“名称” 字段中编辑文本 。您可以通过编辑“返回类型”字段的内容来更改返回类型。只能在PHP语言版本7.1及更高版本中设置方法返回类型。您可以在“设置/首选项”对话框(Ctrl+Alt+S)的PHP页面上指定PHP语言级别。

使用“参数”区域中的表和按钮管理方法参数。添加参数时,您可能希望将这些参数传播到调用当前方法的方法。

在PHP上下文中,当从类的构造函数调用更改签名重构时 ,可以将新参数初始化为类字段。为此,请使用“创建并初始化类属性”复选框:选中此复选框后,新添加的参数将初始化为字段。IntelliJ IDEA创建一个与此参数同名的字段,并添加一行,其中包含以下赋值:

$this-> = $; 清除该复选框后,将添加一个参数而不进行初始化。

在此示例中,我们在__construct()方法上调用“更改签名重构”,并添加一个新的$q参数。结果取决于是否选中了“创建和初始化类属性”复选框。使用默认可见性修饰符创建新字段,该修饰符在代码样式的“代码生成”选项卡上设置 。“设置/首选项”对话框(Ctrl+Alt+S)的PHP页面。

之前之后class ChangeSignatureNewParam {

function __construct() {

$a = "Constructor in ChangeSignatureNewParam";

print $a;

}

}选中“创建和初始化类属性”复选框:

class ChangeSignatureNewParam {

private $q;

function __construct($q) {

$a = "Constructor in ChangeSignatureNewParam";

print $a;

$this->q = $q;

}

}

清除“创建和初始化类属性”复选框:

class ChangeSignatureNewParam {

function __construct($q) {

$a = "Constructor in ChangeSignatureNewParam";

print $a;

}

}

您可以删除或重新排序添加的参数。要更改参数的名称或默认值,请在参数表中进行必要的更新(分别在“名称”和“默认值”字段中 )。

您可以沿调用当前方法的方法的层次结构传播新方法参数(如果有)。单击“传播参数”按钮(

fc07bd7257ae794a08025fcccefa7d9e.png)。

要立即执行重构,请单击“重构”。要在实际执行重构之前查看预期的更改并进行必要的调整,请单击“预览”。

示例

下表显示了执行相同“更改签名”重构的3种不同方法 。

在所有情况下,result()函数都重命名为generateResult(),并且向此函数添加了一个新的$b参数。

这些示例显示了函数调用,调用函数(showResult())和其他代码片段如何受到影响,具体取决于重构设置。之前之后// This is the function whose signature will be changed:

function result($a) {

// some code here

}

function showResult($a) {

// Here is the function call:

$this->result($a);

}

// Now we'll rename the function and

// add one parameter.// The function has been renamed to generateResult.

// The new parameter $b has been added.

function generateResult($a,$b) {

// some code here

}

function showResult($a,$b) {

// The function call has been changed accordingly:

$this->generateResult($a,$b);

}

// This is the function whose signature will be changed:

function result($a) {

// some code here

}

function showResult($a) {

// Here is the function call:

$this->result($a);

}

// Now we'll rename the function and add one parameter.

//We will also specify the default value 'new_param'

//for this new parameter// The function has been renamed to generateResult.

// The new parameter $b has been added.

function generateResult($a,$b) {

// some code here

}

function showResult($a) {

// The function call has been changed accordingly:

$this->generateResult($a,'new_param');

}

// When performing the refactoring, 'new_param' was specified as

// the default parameter value.

// This is the function whose signature will be changed:

function result($a) {

// some code here

}

function showResult($a) {

// Here is the function call:

$this->result($a);

}

// Now we'll rename the function and add one parameter.

//We will also propagate this new parameter

//through the showResult() calling function to the function call.// The function has been renamed to generateResult.

// The new parameter $b has been added.

function generateResult($a,$b) {

// some code here

}

// Note the new function parameter:

function showResult($a,$b) {

// The function call has been changed accordingly:

$this->generateResult($a,$b);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值