suggest-simplification
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Simplification Suggestions
代码简化建议
Analyze PHP code for simplification and refactoring opportunities.
分析PHP代码,寻找简化与重构的机会。
Simplification Patterns
简化模式
1. Extract Method
1. 提取方法
php
// BEFORE: Long inline code block
public function processOrder(Order $order): void
{
// Validate order (5 lines)
if ($order->getItems()->isEmpty()) {
throw new EmptyOrderException();
}
if ($order->getTotal()->isNegative()) {
throw new InvalidTotalException();
}
// Process payment (10 lines)
$payment = $this->paymentGateway->charge(
$order->getTotal(),
$order->getPaymentMethod()
);
if (!$payment->isSuccessful()) {
throw new PaymentFailedException();
}
// Send notifications (5 lines)
$this->mailer->send($order->getCustomer()->getEmail(), 'order_confirmed');
}
// AFTER: Extracted methods
public function processOrder(Order $order): void
{
$this->validateOrder($order);
$this->processPayment($order);
$this->sendConfirmation($order);
}
private function validateOrder(Order $order): void {}
private function processPayment(Order $order): Payment {}
private function sendConfirmation(Order $order): void {}php
// BEFORE: Long inline code block
public function processOrder(Order $order): void
{
// Validate order (5 lines)
if ($order->getItems()->isEmpty()) {
throw new EmptyOrderException();
}
if ($order->getTotal()->isNegative()) {
throw new InvalidTotalException();
}
// Process payment (10 lines)
$payment = $this->paymentGateway->charge(
$order->getTotal(),
$order->getPaymentMethod()
);
if (!$payment->isSuccessful()) {
throw new PaymentFailedException();
}
// Send notifications (5 lines)
$this->mailer->send($order->getCustomer()->getEmail(), 'order_confirmed');
}
// AFTER: Extracted methods
public function processOrder(Order $order): void
{
$this->validateOrder($order);
$this->processPayment($order);
$this->sendConfirmation($order);
}
private function validateOrder(Order $order): void {}
private function processPayment(Order $order): Payment {}
private function sendConfirmation(Order $order): void {}2. Introduce Explaining Variable
2. 引入解释性变量
php
// BEFORE: Complex expression
if ($user->getSubscription()?->isActive()
&& $user->getCreatedAt() < new DateTime('-30 days')
&& !$user->hasUsedTrial()
&& $user->getOrderCount() > 0) {
$this->offerUpgrade($user);
}
// AFTER: Named variables
$hasActiveSubscription = $user->getSubscription()?->isActive();
$isEstablishedUser = $user->getCreatedAt() < new DateTime('-30 days');
$eligibleForUpgrade = !$user->hasUsedTrial() && $user->getOrderCount() > 0;
if ($hasActiveSubscription && $isEstablishedUser && $eligibleForUpgrade) {
$this->offerUpgrade($user);
}
// EVEN BETTER: Extract to method
if ($user->isEligibleForUpgrade()) {
$this->offerUpgrade($user);
}php
// BEFORE: Complex expression
if ($user->getSubscription()?->isActive()
&& $user->getCreatedAt() < new DateTime('-30 days')
&& !$user->hasUsedTrial()
&& $user->getOrderCount() > 0) {
$this->offerUpgrade($user);
}
// AFTER: Named variables
$hasActiveSubscription = $user->getSubscription()?->isActive();
$isEstablishedUser = $user->getCreatedAt() < new DateTime('-30 days');
$eligibleForUpgrade = !$user->hasUsedTrial() && $user->getOrderCount() > 0;
if ($hasActiveSubscription && $isEstablishedUser && $eligibleForUpgrade) {
$this->offerUpgrade($user);
}
// EVEN BETTER: Extract to method
if ($user->isEligibleForUpgrade()) {
$this->offerUpgrade($user);
}3. Remove Redundant Code
3. 移除冗余代码
php
// BEFORE: Redundant checks
if ($value !== null) {
if ($value !== null) { // Duplicate check
$this->process($value);
}
}
// BEFORE: Unnecessary else
if ($condition) {
return $a;
} else {
return $b;
}
// AFTER: Simplified
if ($condition) {
return $a;
}
return $b;
// BEFORE: Redundant boolean
if ($condition === true) {}
return $value === true;
// AFTER: Simplified
if ($condition) {}
return $value;php
// BEFORE: Redundant checks
if ($value !== null) {
if ($value !== null) { // Duplicate check
$this->process($value);
}
}
// BEFORE: Unnecessary else
if ($condition) {
return $a;
} else {
return $b;
}
// AFTER: Simplified
if ($condition) {
return $a;
}
return $b;
// BEFORE: Redundant boolean
if ($condition === true) {}
return $value === true;
// AFTER: Simplified
if ($condition) {}
return $value;4. Simplify Conditionals
4. 简化条件语句
php
// BEFORE: Nested conditionals
if ($user !== null) {
if ($user->isActive()) {
if ($user->hasPermission('edit')) {
return true;
}
}
}
return false;
// AFTER: Combined condition
return $user !== null
&& $user->isActive()
&& $user->hasPermission('edit');
// BEFORE: Negative condition
if (!$items->isEmpty()) {
$this->process($items);
}
// AFTER: Positive condition
if ($items->isNotEmpty()) {
$this->process($items);
}php
// BEFORE: Nested conditionals
if ($user !== null) {
if ($user->isActive()) {
if ($user->hasPermission('edit')) {
return true;
}
}
}
return false;
// AFTER: Combined condition
return $user !== null
&& $user->isActive()
&& $user->hasPermission('edit');
// BEFORE: Negative condition
if (!$items->isEmpty()) {
$this->process($items);
}
// AFTER: Positive condition
if ($items->isNotEmpty()) {
$this->process($items);
}5. Replace Temp with Query
5. 用查询替换临时变量
php
// BEFORE: Temporary variable used once
$basePrice = $order->getBasePrice();
$discount = $basePrice * 0.1;
return $basePrice - $discount;
// AFTER: Inline or method
return $order->getBasePrice() * 0.9;
// Or if complex:
return $order->getBasePrice() - $this->calculateDiscount($order);php
// BEFORE: Temporary variable used once
$basePrice = $order->getBasePrice();
$discount = $basePrice * 0.1;
return $basePrice - $discount;
// AFTER: Inline or method
return $order->getBasePrice() * 0.9;
// Or if complex:
return $order->getBasePrice() - $this->calculateDiscount($order);6. Use Collection Methods
6. 使用集合方法
php
// BEFORE: Manual loop
$active = [];
foreach ($users as $user) {
if ($user->isActive()) {
$active[] = $user;
}
}
// AFTER: array_filter
$active = array_filter($users, fn($user) => $user->isActive());
// BEFORE: Manual map
$emails = [];
foreach ($users as $user) {
$emails[] = $user->getEmail();
}
// AFTER: array_map
$emails = array_map(fn($user) => $user->getEmail(), $users);
// BEFORE: Manual find
$found = null;
foreach ($items as $item) {
if ($item->getId() === $id) {
$found = $item;
break;
}
}
// AFTER: Collection method
$found = $collection->first(fn($item) => $item->getId() === $id);php
// BEFORE: Manual loop
$active = [];
foreach ($users as $user) {
if ($user->isActive()) {
$active[] = $user;
}
}
// AFTER: array_filter
$active = array_filter($users, fn($user) => $user->isActive());
// BEFORE: Manual map
$emails = [];
foreach ($users as $user) {
$emails[] = $user->getEmail();
}
// AFTER: array_map
$emails = array_map(fn($user) => $user->getEmail(), $users);
// BEFORE: Manual find
$found = null;
foreach ($items as $item) {
if ($item->getId() === $id) {
$found = $item;
break;
}
}
// AFTER: Collection method
$found = $collection->first(fn($item) => $item->getId() === $id);7. Replace Switch with Polymorphism
7. 用多态替换Switch语句
php
// BEFORE: Switch on type
public function calculateShipping(Order $order): Money
{
switch ($order->getShippingMethod()) {
case 'standard':
return $this->calculateStandardShipping($order);
case 'express':
return $this->calculateExpressShipping($order);
case 'overnight':
return $this->calculateOvernightShipping($order);
default:
throw new InvalidMethodException();
}
}
// AFTER: Strategy pattern
interface ShippingCalculator {
public function calculate(Order $order): Money;
}
class StandardShipping implements ShippingCalculator {}
class ExpressShipping implements ShippingCalculator {}
public function calculateShipping(Order $order): Money
{
return $this->shippingCalculators
->get($order->getShippingMethod())
->calculate($order);
}php
// BEFORE: Switch on type
public function calculateShipping(Order $order): Money
{
switch ($order->getShippingMethod()) {
case 'standard':
return $this->calculateStandardShipping($order);
case 'express':
return $this->calculateExpressShipping($order);
case 'overnight':
return $this->calculateOvernightShipping($order);
default:
throw new InvalidMethodException();
}
}
// AFTER: Strategy pattern
interface ShippingCalculator {
public function calculate(Order $order): Money;
}
class StandardShipping implements ShippingCalculator {}
class ExpressShipping implements ShippingCalculator {}
public function calculateShipping(Order $order): Money
{
return $this->shippingCalculators
->get($order->getShippingMethod())
->calculate($order);
}8. Null Object Pattern
8. 空对象模式
php
// BEFORE: Null checks everywhere
if ($user->getAddress() !== null) {
echo $user->getAddress()->getCity();
} else {
echo 'Unknown';
}
// AFTER: Null Object
class NullAddress implements AddressInterface
{
public function getCity(): string
{
return 'Unknown';
}
}
// Always safe to call
echo $user->getAddress()->getCity();php
// BEFORE: Null checks everywhere
if ($user->getAddress() !== null) {
echo $user->getAddress()->getCity();
} else {
echo 'Unknown';
}
// AFTER: Null Object
class NullAddress implements AddressInterface
{
public function getCity(): string
{
return 'Unknown';
}
}
// Always safe to call
echo $user->getAddress()->getCity();9. Guard Clauses
9. 卫语句
php
// BEFORE: Deeply nested
public function process(Request $request): Response
{
if ($request !== null) {
if ($request->isValid()) {
if ($this->canProcess($request)) {
return $this->doProcess($request);
} else {
return $this->error('Cannot process');
}
} else {
return $this->error('Invalid request');
}
} else {
return $this->error('No request');
}
}
// AFTER: Guard clauses
public function process(Request $request): Response
{
if ($request === null) {
return $this->error('No request');
}
if (!$request->isValid()) {
return $this->error('Invalid request');
}
if (!$this->canProcess($request)) {
return $this->error('Cannot process');
}
return $this->doProcess($request);
}php
// BEFORE: Deeply nested
public function process(Request $request): Response
{
if ($request !== null) {
if ($request->isValid()) {
if ($this->canProcess($request)) {
return $this->doProcess($request);
} else {
return $this->error('Cannot process');
}
} else {
return $this->error('Invalid request');
}
} else {
return $this->error('No request');
}
}
// AFTER: Guard clauses
public function process(Request $request): Response
{
if ($request === null) {
return $this->error('No request');
}
if (!$request->isValid()) {
return $this->error('Invalid request');
}
if (!$this->canProcess($request)) {
return $this->error('Cannot process');
}
return $this->doProcess($request);
}10. Use Modern PHP Features
10. 使用现代PHP特性
php
// BEFORE: Old syntax
$name = isset($data['name']) ? $data['name'] : 'default';
// AFTER: Null coalescing
$name = $data['name'] ?? 'default';
// BEFORE: Property assignment
$value = $object->getValue();
if ($value !== null) {
echo $value;
}
// AFTER: Nullsafe operator
echo $object?->getValue();
// BEFORE: Match as if/else
if ($status === 'active') {
$color = 'green';
} elseif ($status === 'pending') {
$color = 'yellow';
} else {
$color = 'red';
}
// AFTER: Match expression
$color = match($status) {
'active' => 'green',
'pending' => 'yellow',
default => 'red',
};php
// BEFORE: Old syntax
$name = isset($data['name']) ? $data['name'] : 'default';
// AFTER: Null coalescing
$name = $data['name'] ?? 'default';
// BEFORE: Property assignment
$value = $object->getValue();
if ($value !== null) {
echo $value;
}
// AFTER: Nullsafe operator
echo $object?->getValue();
// BEFORE: Match as if/else
if ($status === 'active') {
$color = 'green';
} elseif ($status === 'pending') {
$color = 'yellow';
} else {
$color = 'red';
}
// AFTER: Match expression
$color = match($status) {
'active' => 'green',
'pending' => 'yellow',
default => 'red',
};Severity Classification
严重程度分类
| Pattern | Severity |
|---|---|
| Deeply nested code | 🟠 Major |
| Repeated code blocks | 🟠 Major |
| Complex boolean expressions | 🟡 Minor |
| Old syntax available in modern PHP | 🟢 Suggestion |
| Verbose but clear code | 🟢 Suggestion |
| 模式 | 严重程度 |
|---|---|
| 深度嵌套代码 | 🟠 主要 |
| 重复代码块 | 🟠 主要 |
| 复杂布尔表达式 | 🟡 次要 |
| 可使用现代PHP特性的旧语法 | 🟢 建议 |
| 冗长但清晰的代码 | 🟢 建议 |
Output Format
输出格式
markdown
undefinedmarkdown
undefinedSimplification: [Description]
简化建议: [描述]
Severity: 🟠/🟡/🟢
Location:
Type: [Extract Method|Guard Clause|Collection Method|...]
file.php:lineIssue:
[Description of the complexity]
Current:
php
// Complex codeSuggested:
php
// Simplified codeBenefits:
- Improved readability
- Reduced cognitive load
- Easier testing
- Better reusability
undefined严重程度: 🟠/🟡/🟢
位置:
类型: [提取方法|卫语句|集合方法|...]
file.php:line问题:
[复杂度描述]
当前代码:
php
// 复杂代码建议代码:
php
// 简化后的代码优势:
- 提升可读性
- 降低认知负担
- 更易测试
- 更好的复用性
undefined