check-singleton-antipattern
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "check-singleton-antipattern":https://skillpi.com/zh/skills/deepskill-28bb122fbab8d307164a7aa804bc248d31098a2844c1158806ca0fe26fb7d161。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
检测 PHP 代码中的单例反模式,识别通过静态实例、隐藏依赖、紧耦合和可测试性问题导致的全局状态问题。
介绍来源: third_party · dykyi-roman · 0.0.1 参考来源
SkillPi 解读
- 能完成的任务
- 翻译待完成,请参阅来源文档。
- 输入和输出
- 减记
- 实测状态
- 未测试
文件浏览
Frontmatter
name: check-singleton-antipattern
description: Detects Singleton anti-pattern in PHP code. Identifies global state via static instances, hidden dependencies, tight coupling, and testability issues.Singleton Anti-Pattern Detection
Analyze PHP code for Singleton anti-pattern usage that introduces global state and tight coupling.
Detection Patterns
1. Classic Singleton Implementation
// ANTIPATTERN: Static instance with private constructor
final class DatabaseConnection
{
private static ?self $instance = null;
private function __construct(
private readonly PDO $pdo,
) {}
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self(new PDO('mysql:host=localhost'));
}
return self::$instance;
}
}
// Usage creates hidden dependency
class UserRepository
{
public function find(int $id): User
{
$db = DatabaseConnection::getInstance(); // Hidden dependency!
return $db->query("SELECT * FROM users WHERE id = ?", [$id]);
}
}
2. Registry / Service Locator (Singleton Variant)
// ANTIPATTERN: Global registry acting as singleton container
final class Registry
{
private static array $services = [];
public static function set(string $key, mixed $value): void
{
self::$services[$key] = $value;
}
public static function get(string $key): mixed
{
return self::$services[$key] ?? throw new RuntimeException("Not found: $key");
}
}
// Usage hides ALL dependencies
class OrderService
{
public function create(array $data): Order
{
$repo = Registry::get('orderRepository'); // Hidden dependency
$mailer = Registry::get('mailer'); // Hidden dependency
}
}
3. Static State Accumulation
// ANTIPATTERN: Mutable static state
class EventBus
{
private static array $listeners = [];
public static function subscribe(string $event, callable $listener): void
{
self::$listeners[$event][] = $listener;
}
public static function dispatch(string $event, mixed $data): void
{
foreach (self::$listeners[$event] ?? [] as $listener) {
$listener($data);
}
}
}
// Problem: State leaks between tests, no way to reset
4. Late Static Binding Singleton
// ANTIPATTERN: Singleton via late static binding
abstract class BaseSingleton
{
protected static array $instances = [];
public static function getInstance(): static
{
$class = static::class;
if (!isset(static::$instances[$class])) {
static::$instances[$class] = new static();
}
return static::$instances[$class];
}
}
5. Framework Anti-Patterns
// ANTIPATTERN: Laravel Facade misuse (global state access)
class OrderController
{
public function store(): Response
{
Cache::put('key', 'value'); // Static singleton access
Log::info('Order created'); // Static singleton access
Event::dispatch(new Created()); // Static singleton access
// All dependencies hidden — not in constructor
}
}
// CORRECT: Inject dependencies
final readonly class OrderController
{
public function __construct(
private CacheInterface $cache,
private LoggerInterface $logger,
private EventDispatcherInterface $events,
) {}
}
Grep Patterns
# Classic singleton
Grep: "static.*\$instance|getInstance\(\)|private function __construct" --glob "**/*.php"
# Static service access
Grep: "static function get|static function getInstance|static::getInstance" --glob "**/*.php"
# Global state via static arrays
Grep: "private static array|protected static array" --glob "**/*.php"
# Registry / Service Locator
Grep: "Registry::get|ServiceLocator::get|Container::get" --glob "**/*.php"
# Mutable static properties
Grep: "static \$[a-z]+ =" --glob "**/Domain/**/*.php"
Severity Classification
| Pattern | Severity |
|---|---|
| Singleton in Domain layer | 🔴 Critical |
| Service Locator pattern | 🔴 Critical |
| Static mutable state | 🟠 Major |
| Framework facade in Domain | 🟠 Major |
| Static helper/utility | 🟡 Minor |
Correct Alternatives
Use Dependency Injection
// CORRECT: DI container manages lifecycle
final readonly class UserRepository
{
public function __construct(
private PDO $connection, // Injected, not global
) {}
public function find(UserId $id): User
{
// Use injected dependency
$stmt = $this->connection->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id->value()]);
return $this->hydrate($stmt->fetch());
}
}
// Container configuration (single instance if needed)
$container->singleton(PDO::class, fn () => new PDO($dsn));
Use Factory for Complex Creation
// CORRECT: Factory instead of Singleton
final readonly class ConnectionFactory
{
public function __construct(
private DatabaseConfig $config,
) {}
public function create(): PDO
{
return new PDO(
$this->config->dsn(),
$this->config->username(),
$this->config->password(),
);
}
}
Output Format
### Singleton Anti-Pattern: [Description]
**Severity:** 🔴/🟠/🟡
**Location:** `file.php:line`
**Issue:**
[Description of the singleton/global state problem]
**Impact:**
- Hidden dependency — not visible in constructor
- Tight coupling — cannot substitute in tests
- Global state — shared mutable state between tests/requests
**Code:**
```php
// Current singleton usage
Fix:
// Refactored with dependency injection
当前收录版本
0.0.1
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 提供 PHP 单例反模式检测能力,面向开发者代码质量提升
- 覆盖多种常见反模式场景(经典单例、注册表、静态状态等)
- 具有明确的目标用户群体(PHP 开发者)和实用价值
亮点
- description 明确描述功能为检测 PHP 代码中的单例反模式
- 触发词精准覆盖静态实例、隐藏依赖、紧耦合等核心特征
- 边界清晰,不易与其他代码检测工具混淆
亮点
- 文档结构清晰,包含检测模式、Grep 模式、严重性分类、正确替代方案和输出格式
- 提供完整代码示例和修复建议,便于用户理解与执行
- frontmatter 完整,单文件内容组织良好
亮点
- 提供具体 Grep 命令用于检测,参数明确且可执行
- 覆盖主要异常场景并给出严重性分级(Critical/Major/Minor)
- 提供正确的依赖注入替代方案,指导用户修复问题
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。