重构 = 在不改变外部行为的前提下,改善代码内部结构。
为什么要重构
重构时机
常见代码坏味道
1. 过长函数
// ❌ 坏代码:函数太长
functionprocessOrder($order){
// 验证订单... 50行
// 计算价格... 30行
// 检查库存... 40行
// 创建订单... 20行
// 发送通知... 30行
}
// ✅ 重构后:拆分成小函数
functionprocessOrder($order){
$this->validateOrder($order);
$total = $this->calculateTotal($order);
$this->checkInventory($order);
$orderId = $this->createOrder($order, $total);
$this->sendNotification($orderId);
return $orderId;
}
2. 重复代码
// ❌ 坏代码:重复逻辑
functiongetUserById($id){
$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
functiongetOrderById($id){
$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
// ✅ 重构后:提取公共方法
classBaseRepository{
protected PDO $pdo;
protected string $table;
publicfunctionfindById($id){
$stmt = $this->pdo->prepare(
"SELECT * FROM {$this->table} WHERE id = ?"
);
$stmt->execute([$id]);
return $stmt->fetch();
}
}
3. 过长参数列表
// ❌ 坏代码:参数太多
functioncreateUser($name, $email, $phone, $address, $city, $country, $zip){
// ...
}
// ✅ 重构后:使用对象
classCreateUserRequest{
public string $name;
public string $email;
public string $phone;
public Address $address;
}
functioncreateUser(CreateUserRequest $request){
// ...
}
4. 条件复杂度
// ❌ 坏代码:嵌套太深
functiongetDiscount($user, $order){
if ($user->isVip()) {
if ($order->total > 1000) {
if ($order->items > 5) {
return0.3;
} else {
return0.2;
}
} else {
return0.1;
}
} else {
if ($order->total > 500) {
return0.05;
}
return0;
}
}
// ✅ 重构后:提前返回 + 策略模式
functiongetDiscount($user, $order): float{
if (!$user->isVip()) {
return $order->total > 500 ? 0.05 : 0;
}
if ($order->total <= 1000) {
return0.1;
}
return $order->items > 5 ? 0.3 : 0.2;
}
5. 魔法数字
// ❌ 坏代码:魔法数字
if ($status == 1) {
// ...
} elseif ($status == 2) {
// ...
}
// ✅ 重构后:使用常量或枚举
classOrderStatus{
const PENDING = 1;
const PAID = 2;
const SHIPPED = 3;
const COMPLETED = 4;
}
if ($status == OrderStatus::PENDING) {
// ...
}
// PHP 8.1+ 枚举
enum OrderStatus: int {
case Pending = 1;
case Paid = 2;
case Shipped = 3;
}
重构手法
提取方法
// 重构前
functionprintOwing(){
$this->printBanner();
// 打印详情
echo"name: " . $this->name . "\n";
echo"amount: " . $this->getOutstanding() . "\n";
}
// 重构后
functionprintOwing(){
$this->printBanner();
$this->printDetails();
}
privatefunctionprintDetails(){
echo"name: " . $this->name . "\n";
echo"amount: " . $this->getOutstanding() . "\n";
}
内联方法
// 重构前:方法过于简单
functiongetRating(){
return$this->moreThanFiveLateDeliveries() ? 2 : 1;
}
functionmoreThanFiveLateDeliveries(){
return$this->numberOfLateDeliveries > 5;
}
// 重构后:内联
functiongetRating(){
return$this->numberOfLateDeliveries > 5 ? 2 : 1;
}
提取变量
// 重构前
if ($platform->toUpperCase()->indexOf("MAC") > -1 &&
$browser->toUpperCase()->indexOf("IE") > -1 &&
$this->wasInitialized() && $this->resize > 0) {
// ...
}
// 重构后
$isMacOs = $platform->toUpperCase()->indexOf("MAC") > -1;
$isIE = $browser->toUpperCase()->indexOf("IE") > -1;
$wasResized = $this->wasInitialized() && $this->resize > 0;
if ($isMacOs && $isIE && $wasResized) {
// ...
}
以查询取代临时变量
// 重构前
$basePrice = $this->quantity * $this->itemPrice;
if ($basePrice > 1000) {
return $basePrice * 0.95;
}
return $basePrice * 0.98;
// 重构后
if ($this->basePrice() > 1000) {
return$this->basePrice() * 0.95;
}
return$this->basePrice() * 0.98;
privatefunctionbasePrice(){
return$this->quantity * $this->itemPrice;
}
引入参数对象
// 重构前
functionamountInvoiced($startDate, $endDate){ }
functionamountReceived($startDate, $endDate){ }
functionamountOverdue($startDate, $endDate){ }
// 重构后
classDateRange{
publicfunction__construct(
public \DateTime $start,
public \DateTime $end
){}
}
functionamountInvoiced(DateRange $range){ }
functionamountReceived(DateRange $range){ }
functionamountOverdue(DateRange $range){ }
以多态取代条件
// 重构前
functiongetSpeed(){
return match ($this->type) {
'european' => $this->getBaseSpeed(),
'african' => $this->getBaseSpeed() - $this->getLoadFactor(),
'norwegian_blue' => $this->isNailed ? 0 : $this->getBaseSpeed(),
};
}
// 重构后
abstractclassBird{
abstractfunctiongetSpeed();
}
classEuropeanextendsBird{
functiongetSpeed(){
return$this->getBaseSpeed();
}
}
classAfricanextendsBird{
functiongetSpeed(){
return$this->getBaseSpeed() - $this->getLoadFactor();
}
}
重构实战案例
案例:订单处理
// 重构前:一个大方法
classOrderProcessor{
publicfunctionprocess($data){
// 验证
if (empty($data['user_id'])) {
thrownewException('用户ID不能为空');
}
if (empty($data['items'])) {
thrownewException('商品不能为空');
}
// 计算价格
$total = 0;
foreach ($data['items'] as $item) {
$product = Product::find($item['product_id']);
$total += $product->price * $item['quantity'];
}
// 应用折扣
$user = User::find($data['user_id']);
if ($user->vip_level >= 2) {
$total *= 0.9;
} elseif ($user->vip_level == 1) {
$total *= 0.95;
}
// 创建订单
$order = new Order();
$order->user_id = $data['user_id'];
$order->total = $total;
$order->save();
// 创建订单项
foreach ($data['items'] as $item) {
$orderItem = new OrderItem();
$orderItem->order_id = $order->id;
$orderItem->product_id = $item['product_id'];
$orderItem->quantity = $item['quantity'];
$orderItem->save();
}
// 发送通知
Mail::send($user->email, '订单创建成功', '...');
return $order;
}
}
// 重构后:职责分离
classOrderProcessor{
publicfunction__construct(
private OrderValidator $validator,
private PriceCalculator $calculator,
private OrderRepository $repository,
private NotificationService $notifier,
){}
publicfunctionprocess(array $data): Order{
$this->validator->validate($data);
$total = $this->calculator->calculate($data);
$order = $this->repository->create($data, $total);
$this->notifier->orderCreated($order);
return $order;
}
}
classOrderValidator{
publicfunctionvalidate(array $data): void{
if (empty($data['user_id'])) {
thrownew ValidationException('用户ID不能为空');
}
if (empty($data['items'])) {
thrownew ValidationException('商品不能为空');
}
}
}
classPriceCalculator{
publicfunctioncalculate(array $data): float{
$subtotal = $this->calculateSubtotal($data['items']);
$discount = $this->getDiscount($data['user_id']);
return $subtotal * (1 - $discount);
}
privatefunctioncalculateSubtotal(array $items): float{
return array_reduce($items, function($total, $item){
$product = Product::find($item['product_id']);
return $total + $product->price * $item['quantity'];
}, 0);
}
privatefunctiongetDiscount(int $userId): float{
$user = User::find($userId);
return match (true) {
$user->vip_level >= 2 => 0.1,
$user->vip_level == 1 => 0.05,
default => 0,
};
}
}
重构工具
Rector 自动重构
composer require rector/rector --dev
// rector.php
useRector\Config\RectorConfig;
useRector\Set\ValueObject\SetList;
returnstaticfunction(RectorConfig $config): void{
$config->paths([__DIR__ . '/src']);
$config->sets([
SetList::PHP_80,
SetList::PHP_81,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
]);
};
# 预览变更
vendor/bin/rector process --dry-run
# 执行重构
vendor/bin/rector process
重构原则
总结
重构目标:
- 提高可读性
- 降低复杂度
- 消除重复
- 便于扩展
常用手法:
- 提取方法/类
- 引入参数对象
- 以多态取代条件
- 提前返回
记住:重构是持续的过程,不是一次性任务。