add backend coding standard
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
# 命名与编码规范
|
||||
|
||||
## 基本原则
|
||||
|
||||
项目严格遵循 [PSR-1](https://www.php-fig.org/psr/psr-1/)、[PSR-4](https://www.php-fig.org/psr/psr-4/)、[PSR-12](https://www.php-fig.org/psr/psr-12/) 规范。
|
||||
|
||||
## 命名规范
|
||||
|
||||
| 类型 | 规范 | 示例 |
|
||||
|------|------|------|
|
||||
| 类名 | PascalCase | `OrderConsumer` |
|
||||
| 方法名 | camelCase | `handleOrder()` |
|
||||
| 类属性 | camelCase | `$eventDispatcher` |
|
||||
| 类中数据库字段注解 | snake_case | `$platform_order_id` |
|
||||
| 方法参数 | snake_case | `$order_id` |
|
||||
| 方法内变量 | snake_case | `$total_amount` |
|
||||
| 常量 | UPPER_SNAKE_CASE | `MAX_RETRY_COUNT` |
|
||||
|
||||
## 示例
|
||||
|
||||
```php
|
||||
class OrderService
|
||||
{
|
||||
protected OrderRepository $orderRepository; // 类属性: camelCase
|
||||
|
||||
public function calculateTotal(array $order_items, float $discount_rate): float
|
||||
{
|
||||
$total_amount = 0; // 方法内变量: snake_case
|
||||
|
||||
foreach ($order_items as $item) {
|
||||
$item_price = $item['price'] * $item['quantity'];
|
||||
$total_amount += $item_price;
|
||||
}
|
||||
|
||||
return $total_amount * (1 - $discount_rate);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 类型签名
|
||||
|
||||
方法必须声明参数类型和返回类型:
|
||||
|
||||
```php
|
||||
public function findById(int $order_id): ?Order
|
||||
```
|
||||
Reference in New Issue
Block a user