check-encapsulation

分析 PHP 代码中的封装违规问题。检测公共可变状态、暴露的内部实现、违反“命令而非询问”原则、滥用 getter/setter 以及破坏信息隐藏的情况。

仓库最近推送
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-encapsulation":https://skillpi.com/zh/skills/deepskill-3a652009a4fc933bc631c69a94bb752483e0dfc2135f95abb1aaf241f89a7e7c。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

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

安装实测待补充

分析 PHP 代码中的封装违规问题。检测公共可变状态、暴露的内部实现、违反“命令而非询问”原则、滥用 getter/setter 以及破坏信息隐藏的情况。

0 下载

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

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
适合谁
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: check-encapsulation
description: Analyzes PHP code for encapsulation violations. Detects public mutable state, exposed internals, Tell Don't Ask violations, getter/setter abuse, and information hiding breaches.

Encapsulation Analyzer

Overview

This skill analyzes PHP codebases for encapsulation violations — situations where internal state is exposed, getters/setters replace behavior, or the "Tell, Don't Ask" principle is violated.

When to Use

  • Reviewing Domain layer entities and aggregates
  • Detecting anemic domain models
  • Checking for Law of Demeter violations
  • Auditing collection exposure patterns
  • Evaluating getter/behavior ratio in entities

Encapsulation Principles

Principle Description Violation Indicator
Information Hiding Internal state not exposed Public properties, many getters
Tell Don't Ask Objects perform actions, not expose data Getter chains, external decisions
Behavioral Richness Objects have behavior, not just data Anemic domain model
Invariant Protection State changes validate constraints Public setters without validation

Analysis Approach

  1. Phase 1 — Public Mutable State: Scan Domain entities for non-readonly public properties
  2. Phase 2 — Getter/Setter Abuse: Count getters vs behavior methods, flag anemic entities
  3. Phase 3 — Tell Don't Ask: Detect getter chains and external conditionals on object state
  4. Phase 4 — Collection Exposure: Find methods returning internal arrays/collections directly
  5. Phase 5 — Exposed Internals: Check for reflection usage, debug methods, mutable object returns
  6. Phase 6 — Constructor Issues: Flag too many dependencies, complex construction without factories

Detection Rules

ID Pattern Grep Target
ENC-01 Public mutable props public string\|public int\|public array in Domain (exclude readonly)
ENC-02 Setter methods public function set[A-Z] in Entity/Aggregate
ENC-03 Getter chains ->get[A-Z].*->get[A-Z].*->get[A-Z]
ENC-04 External conditionals if \(\$.*->get[A-Z].*===
ENC-05 Switch on state switch \(\$.*->get[A-Z]\|match \(\$.*->get[A-Z]
ENC-06 Collection return public function get.*\(\): array in Entity
ENC-07 Doctrine exposure public function get.*\(\): Collection in Domain
ENC-08 Reflection access ReflectionClass\|ReflectionProperty\|setAccessible
ENC-09 Debug exposure public function toArray\(\)\|dump\(\)\|debug\(\) in Domain
ENC-10 Complex construction new [A-Z].*Entity\(\|new [A-Z].*Aggregate\( in Application

Severity Classification

Issue Severity
Public mutable state in Entity Critical
Collection mutated externally Critical
Anemic entity (ratio > 5.0) Critical
Tell Don't Ask violation Major
Getter chain (3+ levels) Major
Internal array returned Major
Setter in Aggregate Minor
Reflection in non-test code Minor

Report Format

# Encapsulation Analysis Report

## Summary

| Issue Type | Critical | Warning | Info |
|------------|----------|---------|------|
| Public Mutable State | N | N | - |
| Getter/Setter Abuse | N | N | N |
| Tell Don't Ask | N | N | - |
| Collection Exposure | N | N | - |
| Exposed Internals | N | N | N |

**Encapsulation Score: X%**

## Findings

### [ID]: [Title]
- **File:** `path:line`
- **Issue:** [Description]
- **Code:** [Problematic snippet]
- **Expected:** [Correct pattern]
- **Skills:** [Related generator skills]

## Getter/Behavior Ratio

| Entity | Getters | Setters | Behavior | Ratio | Status |
|--------|---------|---------|----------|-------|--------|

**Target:** Ratio < 2.0

## Refactoring Recommendations

### Immediate
1. Make all entity properties private
2. Replace setters with behavior methods
3. Return collection copies, not references

### Short-term
4. Extract Value Objects for validated data
5. Add factory methods for complex construction
6. Remove getter chains (add shortcut methods)

### Long-term
7. Review anemic entities for missing behavior
8. Consider CQRS to separate read/write models

Integration

Works with:

  • detect-code-smells — Feature Envy, Anemic Model
  • structural-auditor — DDD compliance
  • create-entity — Generate rich entities
  • create-value-object — Encapsulated value types

References

  • references/patterns.md — detailed detection patterns with code examples, report format samples, metrics examples, and rich entity template
  • "Tell, Don't Ask" — Martin Fowler
  • "Anemic Domain Model" — Martin Fowler
  • "Object-Oriented Software Construction" (Bertrand Meyer)
  • "Elegant Objects" (Yegor Bugayenko)

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

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

场景价值68/100

亮点

  • 针对 PHP 领域模型封装问题的专业分析工具,解决贫血模型、信息泄露等明确痛点
  • 提供 10 条具体检测规则与严重性分级,具备工程实用性
触发精准85/100

亮点

  • 触发描述清晰,覆盖 Public Mutable State、Getter/Setter Abuse 等多种封装违规场景
  • Detection Rules 提供具体 grep 模式,边界定义明确
结构规范92/100

亮点

  • 结构完整,包含 6 阶段分析流程与端到端报告示例
  • frontmatter 规范,文档长度适中,排版清晰
执行可靠75/100

待提升项

  • 缺少误报处理机制说明 原报告严重程度:轻微

    未说明如何处理误报、是否支持配置文件排除特定路径或规则

    改进建议:建议补充误报处理策略,如添加配置文件支持规则排除或路径忽略

亮点

  • 严重性分类合理,Critical/Major/Minor 定义清晰
  • 报告格式包含具体修复建议与相关技能集成指引
证据可验 (前四项均值)80/100

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

正文静态检测

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

正文未命中当前规则。

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

权限与使用前检查

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

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

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

安全评级与访问范围

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

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