/acc:check-dependency-injectionfrom acc
Analyzes PHP code for dependency injection issues including service locator antipattern, hard-coded `new` in business logic, static calls, missing interfaces, and hidden dependencies.
Ships in acc. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Analyze PHP code for proper dependency injection patterns.
// BAD: Hard-coded dependency
class OrderService
{
public function process(Order $order): void
{
$mailer = new Mailer(); // Can't mock this
$mailer->send($order->getCustomer(), 'confirmation');
}
}
// GOOD: Injected dependency
class OrderService
{
public function __construct(
private MailerInterface $mailer,
) {}
public function process(Order $order): void
{
$this->mailer->send($order->getCustomer(), 'confirmation');
}
}
// BAD: Service locator
class UserService
{
public function register(UserData $data): User
{
$hasher = Container::get(PasswordHasher::class);
$repository = Container::get(UserRepository::class);
$mailer = Container::get(Mailer::class);
// Dependencies are hidden
}
}
// GOOD: Constructor injection
class UserService
{
public function __construct(
private PasswordHasher $hasher,
private UserRepository $repository,
private Mailer $mailer,
) {}
// Dependencies are explicit
}
// BAD: Static calls can't be mocked
class ReportGenerator
{
public function generate(): Report
{
$data = Database::query('SELECT ...'); // Static
$date = Carbon::now(); // Static
$id = Uuid::uuid4(); // Static
return new Report($data, $date, $id);
}
}
// GOOD: Injectable services
class ReportGenerator
{
public function __construct(
private Connection $database,
private ClockInterface $clock,
private UuidGenerator $uuidGenerator,
) {}
public function generate(): Report
{
$data = $this->database->query('SELECT ...');
$date = $this->clock->now();
$id = $this->uuidGenerator->generate();
return new Report($data, $date, $id);
}
}
// BAD: Concrete class dependency
class PaymentProcessor
{
public function __construct(
private StripeGateway $gateway, // Concrete class
) {}
}
// GOOD: Interface dependency
class PaymentProcessor
{
public function __construct(
private PaymentGatewayInterface $gateway, // Interface
) {}
}
// BAD: Uses global/superglobal
class UserController
{
public function current(): User
{
$userId = $_SESSION['user_id']; // Hidden dependency
return $this->repository->find($userId);
}
}
// GOOD: Explicit dependency
class UserController
{
public function __construct(
private SessionInterface $session,
private UserRepository $repository,
) {}
public function current(): User
{
$userId = $this->session->get('user_id');
return $this->repository->find($userId);
}
}
// BAD: Optional setter injection
class OrderService
{
private ?Logger $logger = null;
public function setLogger(Logger $logger): void
{
$this->logger = $logger;
}
public function process(): void
{
$this->logger?->info('Processing'); // May be null
}
}
// GOOD: Constructor injection
class OrderService
{
public function __construct(
private LoggerInterface $logger,
) {}
public function process(): void
{
$this->logger->info('Processing');
}
}
// BAD: Factory logic in service
class NotificationService
{
public function send(string $type, string $message): void
{
$channel = match($type) {
'email' => new EmailChannel(),
'sms' => new SmsChannel(),
'push' => new PushChannel(),
};
$channel->send($message);
}
}
// GOOD: Inject factory
class NotificationService
{
public function __construct(
private ChannelFactory $channelFactory,
) {}
public function send(string $type, string $message): void
{
$channel = $this->channelFactory->create($type);
$channel->send($message);
}
}
// BAD: Direct environment access
class ApiClient
{
public function request(): Response
{
$key = getenv('API_KEY'); // Hidden dependency
// ...
}
}
// GOOD: Config injection
class ApiClient
{
public function __construct(
private string $apiKey, // Or ApiConfig object
) {}
}
# New keyword in methods
Grep: "new\s+[A-Z]\w+\(" --glob "**/*.php"
# Service locator
Grep: "Container::(get|make)|App::(make|resolve)" --glob "**/*.php"
# Static method calls
Grep: "[A-Z]\w+::\w+\(" --glob "**/*.php"
# Superglobals
Grep: "\$_(GET|POST|SESSION|COOKIE|ENV|SERVER)" --glob "**/*.php"
# getenv/putenv
Grep: "(getenv|putenv)\(" --glob "**/*.php"
// OK: Value objects and DTOs
new Money(100, 'USD');
new DateTime('now');
new OrderId($uuid);
// OK: Exceptions
throw new InvalidArgumentException();
// OK: In factories (that's their job)
class UserFactory {
public function create(): User {
return new User();
}
}
| Pattern | Severity |
|---|---|
| Service locator | 🟠 Major |
| New keyword for services | 🟠 Major |
| Static method calls | 🟠 Major |
| Superglobal access | 🟠 Major |
| Missing interface | 🟡 Minor |
| Setter injection | 🟡 Minor |
### DI Issue: [Description]
**Severity:** 🟠/🟡
**Location:** `file.php:line`
**Type:** [Service Locator|New Keyword|Static Call|...]
**Issue:**
[Description of the DI problem]
**Current:**
```php
$mailer = new Mailer();
Suggested:
public function __construct(
private MailerInterface $mailer,
) {}
Testing Impact: Cannot mock Mailer in unit tests. With injection, tests can use MockMailer.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub dykyi-roman/awesome-claude-code --plugin acc283 skills in this plugin
/access-control-knowledgeAccess Control knowledge base. Provides ACL, RBAC, ABAC, ReBAC models, multi-tenancy patterns, and PHP implementations (Symfony Voters, Laravel Gates) for security audits and generation./adr-knowledgeAction-Domain-Responder pattern knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for ADR (web-specific MVC alternative) audits./adr-templateGenerates Architecture Decision Records (ADR) for PHP projects. Creates structured decision documentation with context, decision, and consequences.Other plugins' skills for the same job