check-nesting-depth

分析 PHP 代码的嵌套深度问题。检测超过 3 层的深层嵌套、复杂条件语句以及可通过提前返回简化的情况。

仓库最近推送
GitHub Stars
109
Forks
26
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "check-nesting-depth":https://skillpi.com/zh/skills/deepskill-366572ff1fd18ca7d8f8fb97df1983f4fe49f2bbcf149af88eca9ad6b667c313。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

分析 PHP 代码的嵌套深度问题。检测超过 3 层的深层嵌套、复杂条件语句以及可通过提前返回简化的情况。

0 下载

介绍来源: third_party · dykyi-roman · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
输入和输出
减记
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: check-nesting-depth
description: Analyzes PHP code for nesting depth issues. Detects deep nesting over 3 levels, complex conditionals, early return opportunities.

Nesting Depth Check

Analyze PHP code for excessive nesting depth and complexity.

Detection Thresholds

Depth Classification
1-2 ✅ Ideal
3 ⚠️ Acceptable
4 🟡 Deep - refactor recommended
5+ 🟠 Too deep - must refactor

Detection Patterns

1. Deep Nesting

// DEPTH 5: Arrow code / pyramid of doom
public function process(Request $request): Response
{
    if ($request->isValid()) {                           // 1
        if ($user = $request->getUser()) {               // 2
            if ($user->hasPermission('edit')) {          // 3
                foreach ($request->getItems() as $item) { // 4
                    if ($item->isActive()) {              // 5
                        $this->handle($item);
                    }
                }
            }
        }
    }
}

// DEPTH 2: Using early returns
public function process(Request $request): Response
{
    if (!$request->isValid()) {
        return $this->badRequest();
    }

    $user = $request->getUser();
    if (!$user) {
        return $this->unauthorized();
    }

    if (!$user->hasPermission('edit')) {
        return $this->forbidden();
    }

    $activeItems = array_filter(
        $request->getItems(),
        fn($item) => $item->isActive()
    );

    foreach ($activeItems as $item) {
        $this->handle($item);
    }
}

2. Complex Conditionals

// COMPLEX: Multiple nested conditions
if ($order->isPaid()) {
    if ($order->isShipped()) {
        if ($order->isDelivered()) {
            if (!$order->isReturned()) {
                $this->complete($order);
            }
        }
    }
}

// SIMPLIFIED: Combined condition
if ($order->canBeCompleted()) {
    $this->complete($order);
}

// Or with early return
if (!$order->isPaid()) return;
if (!$order->isShipped()) return;
if (!$order->isDelivered()) return;
if ($order->isReturned()) return;

$this->complete($order);

3. Nested Loops

// DEEP: Triple nested loop
foreach ($categories as $category) {
    foreach ($category->getProducts() as $product) {
        foreach ($product->getVariants() as $variant) {
            foreach ($variant->getOptions() as $option) {
                $this->process($option);
            }
        }
    }
}

// BETTER: Extract methods
foreach ($categories as $category) {
    $this->processCategory($category);
}

private function processCategory(Category $category): void
{
    foreach ($category->getProducts() as $product) {
        $this->processProduct($product);
    }
}

private function processProduct(Product $product): void
{
    foreach ($product->getVariants() as $variant) {
        $this->processVariant($variant);
    }
}

4. Callback Nesting (Callback Hell)

// DEEP: Nested callbacks
$this->service->fetch($id, function($result) {
    $this->processor->process($result, function($processed) {
        $this->sender->send($processed, function($sent) {
            $this->logger->log($sent, function($logged) {
                $this->cleanup($logged);
            });
        });
    });
});

// FLAT: Sequential or promise-based
$result = $this->service->fetch($id);
$processed = $this->processor->process($result);
$sent = $this->sender->send($processed);
$this->logger->log($sent);
$this->cleanup($sent);

5. Switch in If in Loop

// DEEP: Mixed control structures
foreach ($items as $item) {
    if ($item->isActive()) {
        switch ($item->getType()) {
            case 'A':
                if ($item->hasFeature()) {
                    $this->handleTypeA($item);
                }
                break;
            case 'B':
                if ($item->isValid()) {
                    $this->handleTypeB($item);
                }
                break;
        }
    }
}

// BETTER: Strategy pattern + filter
$activeItems = array_filter($items, fn($i) => $i->isActive());
foreach ($activeItems as $item) {
    $handler = $this->handlerFactory->create($item->getType());
    $handler->handle($item);
}

Refactoring Techniques

Early Return / Guard Clauses

// Before
function process($data) {
    if ($data !== null) {
        if ($data->isValid()) {
            if ($data->canProcess()) {
                // actual logic
            }
        }
    }
}

// After
function process($data) {
    if ($data === null) return;
    if (!$data->isValid()) return;
    if (!$data->canProcess()) return;

    // actual logic (no nesting)
}

Extract Method

// Before
foreach ($orders as $order) {
    if ($order->isPaid()) {
        foreach ($order->getItems() as $item) {
            if ($item->needsShipping()) {
                $this->ship($item);
            }
        }
    }
}

// After
foreach ($orders as $order) {
    $this->processOrder($order);
}

private function processOrder(Order $order): void
{
    if (!$order->isPaid()) return;
    $this->shipItems($order->getItems());
}

private function shipItems(array $items): void
{
    foreach ($items as $item) {
        if ($item->needsShipping()) {
            $this->ship($item);
        }
    }
}

Replace Conditional with Polymorphism

// Before: Deep switch
switch ($type) {
    case 'email':
        if ($config['html']) {
            // ...
        } else {
            // ...
        }
        break;
    case 'sms':
        // ...
}

// After: Strategy pattern
interface NotificationChannel {
    public function send(Notification $notification): void;
}

class EmailChannel implements NotificationChannel { }
class SmsChannel implements NotificationChannel { }

Decompose Conditional

// Before
if ($date->before($SUMMER_START) || $date->after($SUMMER_END)) {
    $charge = $quantity * $winterRate + $winterServiceCharge;
} else {
    $charge = $quantity * $summerRate;
}

// After
if ($this->isWinter($date)) {
    $charge = $this->winterCharge($quantity);
} else {
    $charge = $this->summerCharge($quantity);
}

Grep Patterns

# Find nested if statements
Grep: "if\s*\([^)]+\)\s*\{[^}]*if\s*\(" --glob "**/*.php"

# Find nested foreach
Grep: "foreach.*foreach" --glob "**/*.php"

# Count indentation levels (manual analysis)

Severity Classification

Depth Severity
4 🟡 Minor
5 🟠 Major
6+ 🔴 Critical

Output Format

### Nesting Depth: [Description]

**Severity:** 🟠/🟡
**Location:** `file.php:line`
**Depth:** 5 levels

**Issue:**
Method `processOrder` has 5 levels of nesting, making it difficult to follow.

**Code Structure:**

if ($request->isValid()) // Level 1 if ($user = getUser()) // Level 2 if ($user->hasPermission()) // Level 3 foreach ($items) // Level 4 if ($item->isActive()) // Level 5


**Suggested Refactoring:**
```php
// Use early returns to flatten
if (!$request->isValid()) return;
if (!$user = getUser()) return;
if (!$user->hasPermission()) return;

$activeItems = $this->filterActiveItems($items);
foreach ($activeItems as $item) {
    $this->handle($item);
}

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

  • 提供 PHP 代码嵌套深度检测的完整解决方案
  • 覆盖多种常见嵌套问题场景(深层嵌套、复杂条件、回调地狱等)
  • 包含实用的重构技术和代码示例
触发精准90/100

亮点

  • 触发描述精准完整,明确限定为 PHP 代码嵌套深度分析
  • 覆盖多种变体场景,边界清晰不易误触
结构规范95/100

待提升项

  • 缺少 When to Use 章节 原报告严重程度:轻微

    文档结构完整但缺少明确的使用场景说明章节

    改进建议:建议补充 'When to Use' 或 '适用场景' 章节,说明何时应使用此 Skill

亮点

  • 文档结构清晰,分阶段组织内容(检测阈值→模式→重构→工具→输出)
  • 提供完整的 Before/After 代码对比示例
  • frontmatter 完整,单文件长度合理
执行可靠90/100

亮点

  • 提供可执行的 grep 模式用于实际检测
  • 重构建议具体可行,包含多种技术方案
  • 严重性分类明确,便于优先级判断
证据可验 (前四项均值)90/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。